如何在 API Channel中發送訊息
由於API Channel不支援隱式建立聯絡人。Livedesk 使用 API Channel,分為以下三步:
- 建立聯絡人
- 基於聯絡人資訊建立會話
- 傳送訊息
建立聯絡人
呼叫地址
https://livedesk-api.engagelab.com/api/v2/accounts/contacts
請求範例
curl -X POST https://livedesk-api.engagelab.com/api/v2/accounts/contacts \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64(api_key:api_secret)' \
{
"inbox_id": 407,
"name": "Test", // 選填
"email": "alice@acme.inc",
"phone_number": "+123456789",
"avatar_url": "https://example.com/avatar.png",
"identifier": "1234567890",
}
curl -X POST https://livedesk-api.engagelab.com/api/v2/accounts/contacts \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64(api_key:api_secret)' \
{
"inbox_id": 407,
"name": "Test", // 選填
"email": "alice@acme.inc",
"phone_number": "+123456789",
"avatar_url": "https://example.com/avatar.png",
"identifier": "1234567890",
}
此代碼塊在浮窗中顯示
請求頭
| 欄位 | 類型 | 描述 |
|---|---|---|
| Authorization | string | 使用 Authorization: Basic base64(API Key:API Secret) 進行身份驗證。請前往 API 金鑰頁面 獲取 API Key 和 API Secret,並將兩者以冒號連接後進行 Base64 編碼。 |
| Content-Type | application/json | 資料類型,普通文字訊息使用 application/json。 |
請求體參數
| 欄位 | 類型 | 必填 | 描述 |
|---|---|---|---|
| inbox_id | String | 是 | channel ID.對應「專案設定-Channels-具體channel-設定」裡面的Channel ID. |
| name | String | 否 | 聯絡人名稱。 |
| String | 否 | 郵件地址。 | |
| phone_number | String | 否 | 聯絡方式。 |
| avatar_url | String | 否 | 附件連結。 |
| identifier | String | 否 | 使用者標識。 |
回應範例
{
"payload": {
"contact": {
"additional_attributes": {},
"availability_status": "offline",
"email": null,
"id": 2219259,
"name": "Test",
"phone_number": null,
"blocked": false,
"identifier": null,
"thumbnail": "",
"custom_attributes": {},
"created_at": 1766571703,
"contact_inboxes": []
},
"contact_inbox": {
"inbox": null,
"source_id": null
}
}
}
{
"payload": {
"contact": {
"additional_attributes": {},
"availability_status": "offline",
"email": null,
"id": 2219259,
"name": "Test",
"phone_number": null,
"blocked": false,
"identifier": null,
"thumbnail": "",
"custom_attributes": {},
"created_at": 1766571703,
"contact_inboxes": []
},
"contact_inbox": {
"inbox": null,
"source_id": null
}
}
}
此代碼塊在浮窗中顯示
回應參數
| 欄位 | 類型 | 必填 | 描述 |
|---|---|---|---|
| additional_attributes | Object | 否 | 附件屬性。 |
| availability_status | String | 否 | 在線狀態,預設為offline。 |
| String | 否 | 郵件地址。 | |
| id | String | 否 | 聯絡人ID。 |
| name | String | 否 | 名稱 |
| phone_number | String | 否 | 手機號碼。 |
| blocked | String | 否 | 是否鎖定,預設falese。 |
| identifier | String | 否 | 使用者標識。 |
| thumbnail | String | 否 | 使用者標識。 |
| custom_attributes | String | 否 | 客戶屬性。 |
| created_at | String | 否 | 建立時間戳。 |
| contact_inboxes | Array | 否 | 使用者標識。 |
| inbox | String | 否 | channel通道。 |
| source_id | String | 否 | 來源ID。 |
建立會話
呼叫地址
https://livedesk-api.engagelab.com/api/v2/accounts/conversations
請求範例
curl -X POST https://livedesk-api.engagelab.com/api/v2/accounts/conversations \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64(api_key:api_secret)' \
{
"inbox_id": 407, // 必傳
"contact_id": 2219256, // contact_id、source_id 可選其中一個傳,但必須保證能正確標識發送者
"source_id": "123456789"
}
curl -X POST https://livedesk-api.engagelab.com/api/v2/accounts/conversations \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64(api_key:api_secret)' \
{
"inbox_id": 407, // 必傳
"contact_id": 2219256, // contact_id、source_id 可選其中一個傳,但必須保證能正確標識發送者
"source_id": "123456789"
}
此代碼塊在浮窗中顯示
請求頭
| 欄位 | 類型 | 描述 |
|---|---|---|
| Authorization | string | 使用 Authorization: Basic base64(API Key:API Secret) 進行身份驗證。請前往 API 金鑰頁面 獲取 API Key 和 API Secret,並將兩者以冒號連接後進行 Base64 編碼。 |
| Content-Type | application/json | 資料類型,普通文字訊息使用 application/json。 |
請求體參數
| 欄位 | 類型 | 必填 | 描述 |
|---|---|---|---|
| inbox_id | String | 是 | channel ID。與建立聯絡人介面使用的ID一致。 |
| contact_id | String | 否 | 聯絡人ID。建立聯絡人介面回應中的id |
| source_id | String | 否 | 來源ID。 |
回應範例
{
"meta": {
"sender": {
"additional_attributes": {},
"availability_status": "offline",
"email": null,
"id": 2219256,
"name": "wjTest",
"phone_number": null,
"blocked": false,
"identifier": null,
"thumbnail": "",
"custom_attributes": {},
"created_at": 1766571624
},
"channel": "Channel::Api",
"hmac_verified": false
},
"id": 40,
"messages": [],
"account_id": 17623000010928,
"uuid": "660ce36f-46bb-4153-948c-8a26e6dab634",
"inbox_id": 407,
"additional_attributes": {},
"agent_last_seen_at": 0,
"assignee_last_seen_at": 0,
"can_reply": true,
"contact_last_seen_at": 0,
"custom_attributes": {},
"labels": [],
"muted": false,
"snoozed_until": null,
"status": "open",
"priority": null,
"conversation_category": "chat",
"chat_mode": "private_chat",
"is_forum": false,
"created_at": 1766572062,
"updated_at": 1766572062.5803902,
"timestamp": 1766572062,
"first_reply_created_at": 0,
"unread_count": 0,
"last_non_activity_message": null,
"last_activity_at": 1766572062,
"waiting_since": 1766572062,
"sla_policy_id": null,
"ticket_id": null,
"over_limit": false,
"content_preview": {
"content": null,
"message_type": null,
"created_at": null
},
"platform": {
"channel_type": "Channel::Api",
"inbox_name": "JennyApi",
"inbox_id": 407
},
"account_info": {
"contact_name": "wjTest",
"contact_email": null,
"contact_phone": null,
"contact_id": 2219256,
"contact_avatar": ""
},
"assignment": {
"assignee_id": null,
"assignee_name": null,
"assignee_email": null,
"team_id": null,
"team_name": null
}
}
{
"meta": {
"sender": {
"additional_attributes": {},
"availability_status": "offline",
"email": null,
"id": 2219256,
"name": "wjTest",
"phone_number": null,
"blocked": false,
"identifier": null,
"thumbnail": "",
"custom_attributes": {},
"created_at": 1766571624
},
"channel": "Channel::Api",
"hmac_verified": false
},
"id": 40,
"messages": [],
"account_id": 17623000010928,
"uuid": "660ce36f-46bb-4153-948c-8a26e6dab634",
"inbox_id": 407,
"additional_attributes": {},
"agent_last_seen_at": 0,
"assignee_last_seen_at": 0,
"can_reply": true,
"contact_last_seen_at": 0,
"custom_attributes": {},
"labels": [],
"muted": false,
"snoozed_until": null,
"status": "open",
"priority": null,
"conversation_category": "chat",
"chat_mode": "private_chat",
"is_forum": false,
"created_at": 1766572062,
"updated_at": 1766572062.5803902,
"timestamp": 1766572062,
"first_reply_created_at": 0,
"unread_count": 0,
"last_non_activity_message": null,
"last_activity_at": 1766572062,
"waiting_since": 1766572062,
"sla_policy_id": null,
"ticket_id": null,
"over_limit": false,
"content_preview": {
"content": null,
"message_type": null,
"created_at": null
},
"platform": {
"channel_type": "Channel::Api",
"inbox_name": "JennyApi",
"inbox_id": 407
},
"account_info": {
"contact_name": "wjTest",
"contact_email": null,
"contact_phone": null,
"contact_id": 2219256,
"contact_avatar": ""
},
"assignment": {
"assignee_id": null,
"assignee_name": null,
"assignee_email": null,
"team_id": null,
"team_name": null
}
}
此代碼塊在浮窗中顯示
回應參數
| 欄位 | 類型 | 必填 | 描述 |
|---|---|---|---|
| meta | Object | 否 | 元資料物件。 |
| sender | Object | 否 | 發送者資訊。 |
| additional_attributes | Object | 否 | 額外屬性。 |
| availability_status | String | 否 | 可用性狀態。 |
| Null | 否 | 郵箱 。 | |
| id | Number | 否 | 聯絡人ID。 |
| name | String | 否 | 發送者名稱。 |
| phone_number | Null | 否 | 電話號碼。 |
| blocked | Boolean | 否 | 是否被阻止。 |
| identifier | Null | 否 | 使用者屬性標識。 |
| thumbnail | String | 否 | 縮圖。 |
| custom_attributes | Object | 否 | 自訂屬性。 |
| created_at | Number | 否 | 會話建立時間戳。 |
| channel | String | 否 | 渠道類型。 |
| hmac_verified | Boolean | 否 | HMAC驗證狀態。 |
| id | Number | 否 | 會話ID。 |
| messages | Array | 否 | 訊息陣列。 |
| account_id | Number | 否 | 專案ID |
| uuid | String | 否 | 唯一標識符。 |
| inbox_id | Number | 否 | 通道ID。 |
| additional_attributes | Object | 否 | 額外屬性。 |
| agent_last_seen_at | Number | 否 | 代理最後處理時間。 |
| assignee_last_seen_at | Number | 否 | 被分配最後處理時間。 |
| can_reply | Boolean | 否 | 是否可以回覆。 |
| contact_last_seen_at | Number | 否 | 聯絡人最後seen時間。 |
| custom_attributes | Object | 否 | 自訂屬性。 |
| labels | Array | 否 | 標籤陣列。 |
| muted | Boolean | 否 | 是否靜音。 |
| snoozed_until | Null | 否 | 暫停到何時。 |
| status | String | 否 | 狀態。 |
| priority | Null | 否 | 優先級。 |
| conversation_category | String | 否 | 對話類別。 |
| chat_mode | String | 否 | 聊天模式。 |
| is_forum | Boolean | 否 | 是否為論壇。 |
| created_at | Number | 否 | 建立時間戳。 |
| updated_at | Number | 否 | 更新時間戳 。 |
| timestamp | Number | 否 | 時間戳。 |
| first_reply_created_at | Number | 否 | 首次回覆建立時間。 |
| unread_count | Number | 否 | 未讀計數。 |
| last_non_activity_message | Null | 否 | 最後非活動訊息。 |
| last_activity_at | Number | 否 | 最後活動時間。 |
| waiting_since | Number | 否 | 等待開始時間。 |
| sla_policy_id | Null | 否 | SLA策略ID。 |
| ticket_id | Null | 否 | 工單ID。 |
| over_limit | Boolean | 否 | 是否超限。 |
| content_preview | Object | 否 | 內容預覽。 |
| content_preview.content | Null | 否 | 預覽內容。 |
| content_preview.message_type | Null | 否 | 訊息類型。 |
| content_preview.created_at | Null | 否 | 建立時間。 |
| platform | Object | 否 | 平台資訊。 |
| channel_type | String | 否 | 渠道類型。 |
| inbox_name | String | 否 | 收件箱名稱。 |
| inbox_id | Number | 否 | 收件箱ID。 |
| account_info | Object | 否 | 帳戶資訊。 |
| contact_name | String | 否 | 聯絡人名称。 |
| contact_email | Null | 否 | 聯絡人郵箱。 |
| contact_phone | Null | 否 | 聯絡人電話。 |
| contact_id | Number | 否 | 聯絡人ID。 |
| contact_avatar | String | 否 | 聯絡人頭像。 |
| assignment | Object | 否 | 分配資訊。 |
| assignee_id | Null | 否 | 受理人ID。 |
| assignee_name | Null | 否 | 受理人名称。 |
| assignee_email | Null | 否 | 受理人郵箱。 |
| team_id | Null | 否 | 團隊ID。 |
| team_name | Null | 否 | 團隊名称。 |
傳送訊息
呼叫地址
https://livedesk-api.engagelab.com/api/v2/accounts/conversations/:conversation_id/messages
請求範例
curl -X POST 'https://livedesk-api.engagelab.com/api/v2/accounts/conversations/:conversation_id/messages' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64(api_key:api_secret)' \
-d '{
"content": "客服發送訊息,正常嗎",
"private": false,
"message_type": "incoming",
"content_attributes": {
"in_reply_to": 29
}
}'
curl -X POST 'https://livedesk-api.engagelab.com/api/v2/accounts/conversations/:conversation_id/messages' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64(api_key:api_secret)' \
-d '{
"content": "客服發送訊息,正常嗎",
"private": false,
"message_type": "incoming",
"content_attributes": {
"in_reply_to": 29
}
}'
此代碼塊在浮窗中顯示
請求頭
| 欄位 | 類型 | 描述 |
|---|---|---|
| Authorization | string | 使用 Authorization: Basic base64(API Key:API Secret) 進行身份驗證。請前往 API 金鑰頁面 獲取 API Key 和 API Secret,並將兩者以冒號連接後進行 Base64 編碼。 |
| Content-Type | application/json | 資料類型,普通文字訊息使用 application/json。 |
請求體參數
| 欄位 | 類型 | 必填 | 描述 |
|---|---|---|---|
| content | String | 否 | channel ID.對應「專案設定-Channels-具體channel-設定」裡面的Channel ID. |
| private | String | 否 | 聯絡人名称。 |
| message_type | String | 否 | 訊息類型,outgoing、incoming。分別表示客服發送、使用者發送。不傳預設為outgoing。 |
| content_attributes | String | 否 | 內容屬性。 |
| in_reply_to | String | 否 | 回覆內容。 |

