LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Saturday, March 9, 2019

LiveChatのwebhookを使ってみる


LiveChatのwebhookについて調べたときの備忘録

LiveChat
https://www.livechatinc.com/jp/


この記事では、WebhookのTargetURLをGoogle Apps Script で作ったWebアプリのURLで試します。



  1. Webhookでデータを受け取るWebAppを作ります
  2. LiveChat側でWebhookの設定をします


1. Webhookでデータを受け取るWebAppを作ります

Google Apps Scriptのエディタに以下のコード.gsを書いて保存します。



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



公開 > ウェブアプリケーションとして導入...を選択します

  1. バージョンの説明を入力して
  2. アプリケーションの実行ユーザは「自分」
  3. アクセスはできるのは「全員(匿名ユーザーを含む)」
で「導入」をクリックします。
(LiveChat側からこのアプリにアクセスできて実行できるように2, 3の設定は必須)

現在のウェブアプリケーションのURLのhttps://〜をコピーします


2. Webhook側の設定をします

Settings > Integrations > Webhooks を開きます

いつ=どのEventが起きた時に
どのデータ=Data type
どこに=Target URL(STEP1でコピーしたURL)
を設定して「Add a webhook」をクリックします

すると下部に以下のようなエリアが追加されます

これで設定したEventが起きた時にTarget URLにデータが飛んできます


Eventの chat starts と chat ends を試したときに取得したデータのメモ


chat startsのwebhook
{
  "event_type": "chat_started",
  "event_unique_id": "7ddaabb",
  "token": "31aae354",
  "license_id": "1067",
  "lc_version": "2",
  "chat": {
    "id": "PO13",
    "started_timestamp": 1552126412,
    "url": "URL",
    "referer": "",
    "messages": [{
      "user_type": "agent",
      "author_name": "NAME",
      "agent_id": "name@gmail.com",
      "text": "Hello. How may I help you?",
      "json": "",
      "timestamp": 1552126412
    }],
    "attachments": [],
    "events": [],
    "agents": [{
      "name": "AGENT NAME",
      "login": "name@gmail.com"
    }],
    "tags": [],
    "groups": [0]
  },
  "visitor": {
    "id": "S1551",
    "name": "NAME",
    "email": "name@gmail.com",
    "country": "Japan",
    "city": "",
    "language": "en",
    "page_current": "URL",
    "timezone": ""
  },
  "pre_chat_survey": [{
    "id": "2001",
    "type": "name",
    "label": "Name:",
    "answer": "NAME"
  }, {
    "id": "2002",
    "type": "email",
    "label": "E-mail:",
    "answer": "name@gmail.com"
  }]
}




















chat endsのwebhook
{
  "event_type": "chat_ended",
  "event_unique_id": "349a820",
  "token": "f791db42",
  "license_id": "1067",
  "lc_version": "2",
  "chat": {
    "id": "PO13",
    "started_timestamp": 1552126412,
    "ended_timestamp": 1552126503,
    "url": "URL",
    "referer": "",
    "messages": [{
      "user_type": "agent",
      "author_name": "NAME",
      "agent_id": "name@gmail.com",
      "text": "Hello. How may I help you?",
      "json": "",
      "timestamp": 1552126412
    }, {
      "user_type": "visitor",
      "author_name": "NAME",
      "text": "hello! how's it going?",
      "json": "",
      "timestamp": 1552126451
    }, {
      "user_type": "agent",
      "author_name": "AGENT NAME",
      "agent_id": "name@gmail.com",
      "text": "Hi, good.",
      "json": "{}",
      "timestamp": 1552126490
    }],
    "attachments": [],
    "events": [{
      "user_type": "visitor",
      "text": "NAME closed the chat.",
      "timestamp": 1552126503,
      "type": "closed"
    }],
    "agents": [{
      "name": "NAME",
      "login": "name@gmail.com"
    }],
    "tags": [],
    "groups": [0]
  },
  "visitor": {
    "id": "S1551",
    "name": "NAME",
    "email": "name@gmail.com",
    "country": "Japan",
    "city": "",
    "language": "en",
    "page_current": "URL",
    "timezone": ""
  },
  "pre_chat_survey": [{
    "id": "2001",
    "type": "name",
    "label": "Name:",
    "answer": "NAME"
  }, {
    "id": "2002",
    "type": "email",
    "label": "E-mail:",
    "answer":"name@gmail.com"
  }]
}



Latest post

スプレッドシートA列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...