常规模板参考文档

通过常规模板,您可以发送包含图片、文本和按钮的结构化消息。如果 elements 数组中所描述的常规模板具有多个模板,则该模板将能够发送可水平滚动的商品轮播广告,每个广告由图片、文本和按钮组成。如需获取完整的实现详情,请参阅常规模板

内容

请求 URI

https://graph.facebook.com/v19.0/me/messages?access_token=<PAGE_ACCESS_TOKEN>

请求示例

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{
    "id":"<PSID>"
  },
  "message":{
    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"generic",
        "elements":[
           {
            "title":"Welcome!",
            "image_url":"https://petersfancybrownhats.com/company_image.png",
            "subtitle":"We have the right hat for everyone.",
            "default_action": {
              "type": "web_url",
              "url": "https://petersfancybrownhats.com/view?item=103",
              "messenger_extensions": false,
              "webview_height_ratio": "tall",
              "fallback_url": "https://petersfancybrownhats.com/"
            },
            "buttons":[
              {
                "type":"web_url",
                "url":"https://petersfancybrownhats.com",
                "title":"View Website"
              },{
                "type":"postback",
                "title":"Start Chatting",
                "payload":"DEVELOPER_DEFINED_PAYLOAD"
              }              
            ]      
          }
        ]
      }
    }
  }
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"

响应示例

{
  "recipient_id": "1254477777772919",
  "message_id": "AG5Hz2Uq7tuwNEhXfYYKj8mJEM_QPpz5jdCK48PnKAjSdjfipqxqMvK8ma6AC8fplwlqLP_5cgXIbu7I3rBN0P"
}  

属性

recipient

对消息接收人的说明。所有请求都必须包含下列任一属性:idphone_numberuser_ref

属性类型说明

recipient.id

字符串

消息接收人的 PSIDphone_numberuser_ref

recipient.phone_number

字符串

可选。 收件人的手机号码,格式为:+1(212)555-2368


您的智能助手必须获准使用客户匹配,才能通过这种方式发送消息。

recipient.user_ref

字符串

可选。复选框插件提供的 user_ref

recipient.name

对象

可选。 仅在设置了 phone_number 的情况下使用。以如下格式指定用户姓名:


{"first_name":"John", "last_name":"Doe"}

提供姓名可提高匹配成功的几率。

message

Description of the message to be sent.
Property Type Description

message.attachment

Object

An object describing attachments to the message.

message.attachment

Property Type Description

type

String

Value must be template

payload

Object

模板的 payload

message.attachment.payload

属性名称 类型 描述

template_type

字符串

值必须为 generic

elements

数组 <element>

此数组包含 1 个描述消息中媒体的 element 对象。最多支持 1 个元素。

sharable

布尔值

可选。设置为 true,可为模板消息启用 Messenger 原生分享按钮。默认值为 false

message.attachment.payload.elements

常规模板的每个消息最多支持 10 个元素。除 title 之外,还必须设置至少一个属性。

属性名称 类型 描述

title

字符串

要在模板中显示的标题。不超过 80 个字符。

subtitle

字符串

可选。要在模板中显示的副标题。不超过 80 个字符。

image_url

字符串

可选。要在模板中显示图片的网址。

default_action

对象

可选。点击模板时执行的默认操作。接受像网址按钮的相同属性,title 除外。

buttons

数组 <button>

可选。要添加到模板的按钮的数组。每个元素最多支持 3 个按钮。