互動式訊息範本讓您可向傳送對象傳送更多內容;您將可向他們傳送標準訊息範本和媒體範本類型之外的內容,例如使用 components
物件來將互動式按鈕加入其中。
我們提供了兩種預先定義的按鈕:
此類按鈕可附加至文字訊息或媒體訊息。您建立的互動式訊息範本獲得批准後,即可在通知訊息和顧客服務訊息中使用。
您需要完成以下事項:
當訊息範本獲得批准後,您便可以使用 API 來傳送訊息。
/messages
發出 POST
要求POST /v1/messages { "to": "recipient_wa_id", "type": "template", "template": { "namespace": "your-namespace", "language": { "policy": "deterministic", "code": "your-language-and-locale-code" }, "name": "your-template-name", "components": [ { "type" : "header", "parameters": [ { "type": "text", "text": "replacement_text" } ] # end header }, { "type" : "body", "parameters": [ { "type": "text", "text": "replacement_text" }, { "type": "currency", "currency" : { "fallback_value": "$100.99", "code": "USD", "amount_1000": 100990 } }, { "type": "date_time", "date_time" : { "fallback_value": "February 25, 1977", "day_of_week": 5, "day_of_month": 25, "year": 1977, "month": 2, "hour": 15, "minute": 33, #OR "timestamp": 1485470276 } }, { ... # Any additional template parameters } ] # end body }, # The following part of this code example includes several possible button types, # not all are required for an interactive message template API call. { "type": "button", "sub_type" : "quick_reply", "index": "0", "parameters": [ { "type": "payload", # Business Developer-defined payload "payload":"aGlzIHRoaXMgaXMgY29vZHNhc2phZHdpcXdlMGZoIGFTIEZISUQgV1FEV0RT" } ] }, { "type": "button", "sub_type" : "url", "index": "1", "parameters": [ { "type": "text", # Business Developer-defined dynamic URL suffix "text": "9rwnB8RbYmPF5t2Mn09x4h" } ] }, { "type": "button", "sub_type" : "url", "index": "2", "parameters": [ { "type": "text", # Business Developer-defined dynamic URL suffix "text": "ticket.pdf" } ] } ] } }
成功回應包含帶有 id
的 messages
物件。
{ "messages": [{ "id": "gBEGkYiEB1VXAglK1ZEqA1YKPrU" }] }
不成功的回應會包含錯誤物件,當中含有錯誤字串、錯誤代碼和其他資訊。
如果您向無法接收範本的帳戶傳送範本,則系統會向已配置 Webhook 伺服器的錯誤物件傳送 1026 (ReceiverIncapable)
錯誤。
如需更多有關錯誤的資訊,請參閱錯誤和狀態代碼。
當用戶點擊快速回覆按鈕時,系統會將回覆傳回企業。請參閱透過點擊快速回覆按鈕傳送回呼,了解相關資訊。用戶亦可選擇不點擊按鈕,純粹向您傳送格式不限的訊息。
{ "contacts": [ { "profile": { "name": "Kerry Fisher" }, "wa_id": "16505551234" } ], "messages": [ { "button": { "payload": "No-Button-Payload", "text": "No" }, "context": { "from": "16315558007", "id": "gBGGFmkiWVVPAgkgQkwi7IORac0" }, "from": "16505551234", "id": "ABGGFmkiWVVPAgo-sKD87hgxPHdF", "timestamp": "1591210827", "type": "button" } ] # If there are any errors, an errors field (array) will be present "errors": [ { ... } ] }
這些範例將會說明設定互動式訊息範本的過程,包括在企業管理平台建立範本,以及使用 API 呼叫向 messages
端點傳送訊息範本。
此範例會示範如何建立帶有快速回覆按鈕的互動式媒體訊息範本。
messages
API 呼叫加入參數資訊。POST /v1/messages { "to": "your-test-recipient-wa-id", "recipient_type": "individual", "type": "template", "template": { "namespace": "88b39973_f0d5_54e1_29cf_e80f1e3da4f2", "name": "upcoming_trip_reminder", "language": { "code": "en", "policy": "deterministic" }, "components": [ { "type": "header", "parameters": [ { "type": "text", "text": "12/26" } ] }, { "type": "body", "parameters": [ { "type": "text", "text": "*Ski Trip*" }, { "type": "date_time", "date_time" : { "fallback_value": "29th July 2019, 8:00am", "day_of_month": "29", "year": "2019", "month": "7", "hour": "8", "minute": "00" } }, { "type": "text", "text": "*Squaw Valley Ski Resort, Tahoe*" } ] }, { "type": "button", "sub_type": "quick_reply", "index": 0, "parameters": [ { "type": "payload", "payload": "Yes-Button-Payload" } ] }, { "type": "button", "sub_type": "quick_reply", "index": 1, "parameters": [ { "type": "payload", "payload": "No-Button-Payload" } ] } ] } }
此範例會示範如何建立帶有網址和電話號碼按鈕的互動式媒體訊息範本。
messages
API 呼叫加入參數資訊。POST /v1/messages { "to": "your-test-recipient-wa-id", "recipient_type": "individual", "type": "template", "template": { "namespace": "88b39973_f0d5_54e1_29cf_e80f1e3da4f2", "name": "oculus_shipment_update", "language": { "code": "en", "policy": "deterministic" }, "components": [ { "type": "header", "parameters": [{ "type": "image", "image": { "link": "link-to-your-image" } }] }, { "type": "body", "parameters": [ { "type": "text", "text": "Anand" }, { "type": "text", "text": "Quest" }, { "type": "text", "text": "113-0921387" }, { "type": "date_time", "date_time" : { "fallback_value": "23rd Nov 2019", "day_of_month": "20", "year": "2019", "month": "9" } } ] }, { "type": "button", "index": "0", "sub_type": "url", "parameters": [ { "type": "text", "text": "1Z999AA10123456784" } ] } ] } }