IntercomのAPIから顧客・リード・管理者を取得する(ついでにメールを送ってみる)
Intercomと連携させることがありそうな気配がしたので、とりあえず触ってみました。 Access Tokenを取得する IntercomのAPIを叩くにはAccess Tokenが必要です。Developer Hub […]
広告ここから
広告ここまで
目次
Intercomと連携させることがありそうな気配がしたので、とりあえず触ってみました。
Access Tokenを取得する
IntercomのAPIを叩くにはAccess Tokenが必要です。Developer Hubから[Your Apps]をクリックしてログインし、Access Tokenを発行しましょう。
curlでユーザー一覧を取得する
Intercomで関連付けされてるユーザーの一覧は、https://api.intercom.io/userから取得できます。
$ curl https://api.intercom.io/users \
-H 'Authorization:Bearer xxxx' \
-H 'Accept: application/json'
戻り値は以下のような形です。
{
  "type": "user.list",
  "pages": {
    "type": "pages",
    "next": null,
    "page": 1,
    "per_page": 50,
    "total_pages": 1
  },
  "users": [...],
  "total_count": 29,
  "limited": false
}
usersの中には、以下のようなオブジェクトが配列として入ります。
{
  "type": "user",
  "id": "5bb2f45a1e099bf2d553a2d5",
  "user_id": "john_doe",
  "anonymous": false,
  "email": "[email protected],
  "phone": null,
  "name": null,
  "pseudonym": null,
  "avatar": {
    "type": "avatar",
    "image_url": null
  },
  "app_id": "bdguin94",
  "companies": {
    "type": "company.list",
    "companies": []
  },
  "location_data": {
    "type": "location_data",
    "city_name": "Hachioji",
    "continent_code": "AS",
    "country_name": "Japan",
    "latitude": 99.99,
    "longitude": 100.00,
    "postal_code": "100-0000",
    "region_name": "Tōkyo",
    "timezone": "Asia/Tokyo",
    "country_code": "JPN"
  },
  "last_request_at": 1538536378,
  "last_seen_ip": "127.0.0.0",
  "created_at": 1538454618,
  "remote_created_at": 1538454618,
  "signed_up_at": 1538454618,
  "updated_at": 1538536378,
  "session_count": 3,
  "social_profiles": {
    "type": "social_profile.list",
    "social_profiles": []
  },
  "unsubscribed_from_emails": false,
  "marked_email_as_spam": false,
  "has_hard_bounced": false,
  "user_agent_data": "Mozilla/5.0 ~",
  "tags": {
    "type": "tag.list",
    "tags": []
  },
  "segments": {
    "type": "segment.list",
    "segments": []
  },
  "custom_attributes": {},
  "referrer": "https://localhost:3000/admin",
  "utm_campaign": null,
  "utm_content": null,
  "utm_medium": null,
  "utm_source": null,
  "utm_term": null
}
location_dataなどはトラッキングコードのほうで収集している様子ですね。
リードを一覧取得する
リードだけ取得することも可能です。こちらはhttps://api.intercom.io/contactsを利用します。
$ curl https://api.intercom.io/contacts \
-H 'Authorization:Bearer xxxx' \
-H 'Accept: application/json'
戻り値です。
{
  "type": "user.list",
  "pages": {
    "type": "pages",
    "next": null,
    "page": 1,
    "per_page": 50,
    "total_pages": 1
  },
  "contacts": [...],
  "total_count": 29,
  "limited": false
}
contactsもほぼユーザーと同じようなオブジェクトです。
{
  "type": "contact",
  "id": "5b43338aa93f6ac0409c4a12",
  "user_id": "547eaa08-6ee0-4f9b-97b5-2afa199e84e2",
  "anonymous": true,
  "email": null,
  "phone": null,
  "name": null,
  "pseudonym": "Mint Trailer from Hachioji",
  "avatar": {
    "type": "avatar",
    "image_url": "https://static.intercomassets.com/app/pseudonym_avatars_2018/mint-trailer.png"
  },
  "app_id": "bdguin94",
  "companies": {
    "type": "company.list",
    "companies": []
  },
  "location_data": {
    "type": "location_data",
    "city_name": "Hachioji",
    "continent_code": "AS",
    "country_name": "Japan",
    "latitude": 99.99,
    "longitude": 100.00,
    "postal_code": "100-0000",
    "region_name": "Tōkyo",
    "timezone": "Asia/Tokyo",
    "country_code": "JPN"
  },
  "last_request_at": 1531130807,
  "last_seen_ip": "126.28.87.98",
  "created_at": 1531130762,
  "remote_created_at": null,
  "signed_up_at": null,
  "updated_at": 1531130807,
  "session_count": 0,
  "social_profiles": {
    "type": "social_profile.list",
    "social_profiles": []
  },
  "unsubscribed_from_emails": false,
  "marked_email_as_spam": false,
  "has_hard_bounced": false,
  "user_agent_data": "Mozilla/5.0 ~",
  "tags": {
    "type": "tag.list",
    "tags": []
  },
  "segments": {
    "type": "segment.list",
    "segments": []
  },
  "custom_attributes": {},
  "referrer": "https://localhost:3000/admin,
  "utm_campaign": null,
  "utm_content": null,
  "utm_medium": null,
  "utm_source": null,
  "utm_term": null
}
管理者(admin)を取得する
ほぼ同じような形で管理者リストも取れます。
$ curl https://api.intercom.io/admins \
-H 'Authorization:Bearer xxxx' \
-H 'Accept: application/json'
{
  "type": "admin.list",
  "admins": [
    {
      "type": "admin",
      "email": "[email protected]",
      "id": "123456",
      "name": "John doe",
      "away_mode_enabled": false,
      "away_mode_reassign": false,
      "team_ids": [
        9999999
      ]
    }
  ]
}
顧客にメールを送信する
管理者のIDとユーザーのuser_idまたはidなどがあればメッセージも送信できます。
curl https://api.intercom.io/messages \
-XPOST \
-H 'Authorization:Bearer XXXXXXXXX' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' -d'
{
  "message_type": "email",
  "subject": "Hey",
  "body": "How are you ?",
  "template": "plain",
  "from": {
    "type": "admin",
    "id": "1234567"
  },
  "to": {
    "type": "user",
    "user_id": "john_doe"
  }
}'
{
  "type": "admin_message",
  "id": "263505501",
  "created_at": 1538570984,
  "subject": "Hey",
  "body": "How are you ?",
  "template": "plain",
  "message_type": "email",
  "owner": {
    "type": "admin",
    "id": "1234567"
    "name": "John doe",
    "email": "[email protected]",
    "away_mode_enabled": false,
    "away_mode_reassign": false,
    "avatar": {
      "image_url": "https://static.intercomassets.com/avatars/1206187/square_128/image.png?1234"
    }
  }
}
使い所とか
外部の処理きっかけでメールを送りたい時、一連のAPIを叩き回ることになりそうではあります。Adminについては送信用ユーザーを作って、環境変数とかでそのID決めうちでいい気はしますね。
メールについてもテンプレート機能などを使えばいろいろカスタマイズできそうなので、また触るモチベーションがわいた時にレポートします。