LANG SELRCT

コードを書く場所

2019年4月16日火曜日

LiveChatのwebhookを使ってみる(チャット終了後にタグを更新する)


chat_ended のあとで tag を追加して Webhook の URL に送りたい

リファレンスを見ると chat_changed でできそう

https://developers.livechatinc.com/docs/rest-api/#create-a-new-webhook
PropertyDescription
chat_startedwhen the chat is started
chat_endedwhen the chat is ended
chat_changedwhen the chat is tagged
visitor_queuedwhen the visitor enters the queue before a chat
ticket_createdwhen a new ticket is created
canned_response_changedwhen a canned response is created, modified or deleted



Webhook の設定方法は別の記事に書きました
LiveChatのwebhookを使ってみる


チャットが終わったあとに

support と positive feedback のタグを追加してみる


その時に送信されるデータを受け取るコード


コード.gs
function doPost(e) {
  var contents = e.postData.contents
  console.log(contents);
}
意訳
この機能がやること
受け取ったデータのcontentsを取得して
ログに出す



Webhook の URL に送信されたデータのログを見てみる


chat ends
{
  "event_type": "chat_ended",
  "event_unique_id": "6ddc4",
  "token": "d8f7c",
  "license_id": "1079",
  "lc_version": "2",
  "chat": {
    "id": "PQ808",
    "started_timestamp": 1555361971,
    "ended_timestamp": 1555362040,
    "url": "URL",
    "referer": "",
    "messages": [{
      "user_type": "agent",
      "author_name": "NAME",
      "agent_id": "name@googlegroups.com",
      "text": "Hello. How may I help you?",
      "json": "",
      "timestamp": 1555361971
    }, {
      "user_type": "visitor",
      "author_name": "NAME",
      "text": "hello! how's it going?",
      "json": "",
      "timestamp": 1555362007
    }, {
      "user_type": "agent",
      "author_name": "NAME",
      "agent_id": "name@googlegroups.com",
      "text": "Hi, good.",
      "json": "{}",
      "timestamp": 1555362035
    }],
    "attachments": [],
    "events": [{
      "user_type": "visitor",
      "text": "NAME closed the chat.",
      "timestamp": 1555362040,
      "type": "closed"
    }],
    "agents": [{
      "name": "NAME",
      "login": "name@googlegroups.com"
    }],
    "tags": [],
    "groups": [0]
  },
  "visitor": {
    "id": "S1555",
    "name": "NAME",
    "email": "NAME@gmail.com",
    "country": "Japan",
    "city": "city",
    "language": "en",
    "page_current": "URL",
    "timezone": "Asia/Tokyo"
  },
  "pre_chat_survey": [{
    "id": "2001",
    "type": "name",
    "label": "Name:",
    "answer": "NAME"
  }, {
    "id": "2002",
    "type": "email",
    "label": "E-mail:",
    "answer": "name@gmail.com"
  }]
}
chat changed
{
  "event_type": "chat_changed",
  "token": "c8efe",
  "license_id": "1079",
  "chat": {
    "agents": [{
      "name": "NAME",
      "login": "name@googlegroups.com"
    }],
    "ended_timestamp": 1555362040,
    "events": [{
      "text": "NAME closed the chat.",
      "timestamp": 1555362040,
      "user_type": "visitor"
    }, {
      "text": "NAME rated this chat as good.",
      "timestamp": 1555362049,
      "user_type": "visitor"
    }],
    "groups": [0],
    "id": "PQ808",
    "messages": [{
      "agent_id": "name@googlegroups.com",
      "author_name": "NAME",
      "text": "Hello. How may I help you?",
      "timestamp": 1555361971,
      "user_type": "agent"
    }, {
      "author_name": "NAME",
      "text": "hello! how's it going?",
      "timestamp": 1555362007,
      "user_type": "visitor"
    }, {
      "agent_id": "name@googlegroups.com",
      "author_name": "NAME",
      "text": "Hi, good.",
      "timestamp": 1555362035,
      "user_type": "agent"
    }],
    "started_timestamp": 1555361971,
    "tags": ["support", "positive feedback"],
    "url": "URL"
  },
  "visitor": {
    "email": "name@gmail.com",
    "id": "S1555",
    "name": "NAME",
    "custom_variables": []
  }
}





















補足

chat changed は tag の変更時に Webhook の URL にデータが飛ぶようです
なのでチャットが終わってなくてもデータが送信されます

今回はチャットが終わってから tag を更新しました
チャット中にタグを更新して、チャット終了後にも tag を更新する場合にどういうデータが飛ぶかも知りたいので、次の記事で書きます。