WhatsApp发送模板消息

分为三步:

  • 先在Facebook后台申请WhatsApp模板消息
  • 在Bothub后台创建WhatsApp模板卡片的内容块
  • 构建Api参数发送内容给用户

一、先在Facebook后台申请WhatsApp模板消息

模版消息针对处于24小时窗口期外的用户,此类消息有不一样的收费标准,并且需要在Facebook BM上申请,用于商户主动发送的提醒等。Facebook官网申请模板文档地址:https://www.facebook.com/business/help/2055875911147364?id=2129163877102343

1.1 创建模板

在下图界面,点击“Create Message Template”,创建新的模板。

1.2 模板基本信息配置

依次选择模板消息类别、输入模板名称、选择模板的语言,点击右上角的“Continue”,进入到下一界面进行模板内容设置。

1.3 模板内容配置

在下图界面,进行模板内容的配置。

Header可以选择纯文本和媒体文件两种形式,头部媒体消息支持图片、文档、视频
Body只能创建纯文本
Footer只能创建纯文本

可以在编辑界面右边看到模板的预览效果,如下图:

1.4 为模板添加按钮

在下图界面为模板添加按钮,按钮可分为两种:
第一种,Call To Action: 可创建一个手机号链接和一个网址链接

第二种,Quick Reply: 可创建三个按钮

二、在Bothub后台创建WhatsApp模板卡片内容

2.1 将waba_id存入bot后台获取WhatsApp模板信息

(1)获取waba_id
在Facebook Businesss后台中,如下图所示,在“Business Setting>>Accounts>>WhatsApp Accounts”界面,找到对应的WhatsApp账号,将红框位置的WhatsApp business account id即waba_id复制下来。

(2)存入Bothub后台
如下图所示,在Bothub后台“设置>>基本设置>>WhatsApp模板设置”中填入刚才获取的waba_id,并点击“保存”。

2.2 内容块中添加模板

“内容管理>>自定义内容”中新建内容组和内容块,点击“+Whatsapp模板卡片”

选择一个模板,点击“确定”

三、构建Api参数发送内容块给用户

如下图所示,点击模板上的{...},可替换和查看当前模板中的可变参数

⚠️⚠️⚠️ 注意:可变参数是在申请模板的时候就已经设定好了的。

3.1 可变参数命名规则

3.1.1 指定模板语言

{"key": "template_param-language" , "value" : "en_us”}

官方文档语言代码参考:https://developers.facebook.com/docs/whatsapp/message-templates/creation
同一个模板,不同语言只需要传递不同的语言代码就行,例如:

{ "key": "template_param-language", "value": "pt_BR" }
{ "key": "template_param-language", "value": "es_MX" }

header参数

{"key": "template_param-header_text" , "value" : ""},//头部文本可变参数
{"key": "template_param-header_type" , "value" : "image"},//头部媒体类型: document文档, image图片, video视频
{"key": "template_param-header_mutlimedia_url" , "value" : "" },//头部媒体地址

body参数

{"key": "template_param-body_text_1" , "value" : ""},//body文本可变参数1
{"key": "template_param-body_text_2” , "value" : ""},//body文本可变参数2

button参数

{"key": "template_param-weburl" , "value" : ""}//网址可变参数
{"key": "template_param-payload_1" , "value" :""}// 第一个按钮的回调
{"key": "template_param-payload_2" , "value" :""}// 第二个按钮的回调

⚠️⚠️⚠️ 注意:以上介绍可变参数的定义,如果模板中不存在可变参数,则不需要写params参数。
weburl和payload是同一个级别,属于两种按钮类型,有weburl就不会有payload的。 payload是当按钮不使用bothub的内容块回调的时候,在按钮中传递自定义的参数,在value中写什么,当用户点击按钮的时候,就会将value的值传递回来。
payload可能会有payload1、payload2、payload3,但是weburl只会有一个。

3.1.2 示例代码

{
    "recipient": {
        "id": "8617356956126" //接收的WhatsApp手机号WhatsApp模板消息只支持发送手机号
    },
    "message": {
        "block_name": "testtemplat",//需要发送的内容块名称
        "params": [
            {
                "key": "template_param-language",//模板语言
                "value": "en"
            },
            {
                "key": "template_param-body_text_1",//模板可变参数1
                "value": "mf"
            },
            {
                "key": "template_param-body_text_2",//模板可变参数2

                "value": "test1"
            },
            {
                "key": "template_param-body_text_3",//模板可变参数3

                "value": "test1"
            },
            {
                "key": "template_param-body_text_4",
                "value": "4"
            },
            {
                "key": "template_param-body_text_5",
                "value": "5"
            }
        ]
    },
    "request": {
        "method": "send_block",//固定的方法,不作修改
        "id": "test_12345f",//请求的id,每请求一次就需要一个新的请求id
        "sync": true, //固定的方式,不作修改
        "meta": ""
    },
    "messaging_type": "RESPONSE",//固定的方式,不作修改
    "message_tag": ""//固定的方式,不作修改
}

3.1.3 实际接收效果

3.1.4 其他参数

可参考下面这个链接:https://assets.bothub.ai/docs/advanced-tool/api/template-message.html

3.2 按钮回调参数设置

如果存在按钮的回调设置的内容块,则可以提前设置回调的内容块的可变参数。

3.2.1按钮回调模板参数

第1个按钮回调的模板参数

{"key": "button_1-template_language " , "value" : en模板语言},
{"key": "button_1-template_param-header_text " , "value" : 头部文本可变参数},
{"key": "button_1-template_param-header_type" , "value" : "image"}//头部媒体类型,document文档,image图 片,video视频
{"key": "button_1-template_param-header_mutlimedia_url " , "value" : 头部媒体参数地址},
{"key": "button_1-template_param-body_text_1 " , "value" : body文本可变参数1},
{"key": "button_1-template_param-body_mutlimedia_url" , "value" : 媒体地址},
{"key": "button_1-template_param-weburl" , "value" : 网址可变参数},

第2个按钮回调的模板参数

{"key": "button_2-template_language " , "value" : en模板语言},
{"key": "button_2-template_param-header_text " , "value" : 头部文本可变参数},
{"key": "button_2-template_param-header_type" , "value" : "image"}//头部媒体类型,document文档,image图 片,video视频
{"key": "button_2-template_param-header_mutlimedia_url " , "value" : 头部媒体参数地址},
{"key": "button_2-template_param-body_text_1 " , "value" : body文本可变参数1},
{"key": "button_2-template_param-body_mutlimedia_url" , "value" : 媒体地址},
{"key": "button_2-template_param-weburl " , "value" : 网址可变参数},

第3个按钮回调的模板参数

{"key": "button_3-template_param-header_text " , "value" : 头部文本可变参数},
{"key": "button_3-template_param-header_type" , "value" : "image"}//头部媒体类型,document文档,image图 片,video视频
{"key": "button_3-template_param-header_mutlimedia_url " , "value" : 头部媒体参数地址},
{"key": "button_3-template_param-body_text_1 " , "value" : body文本可变参数1},
{"key": "button_3-template_param-body_mutlimedia_url" , "value" : 媒体地址},
{"key": "button_3-template_param-weburl " , "value" : 网址可变参数},

3.2.2示例:发送带按钮的内容块

需要发送的内容块名称test-a

按钮1回调的内容块名称是1

按钮2回调的内容块名称是2

按钮3回调的内容块名称是3

3.2.3示例代码

{
    "recipient": {
        "id": "8617356956126"
    },
    "message": {
        "block_name": "test-a",
        "params": [
            {
                "key": "template_param-language",
                "value": "en"
            },
            {
                "key": "button_1-template_param-language",
                "value": "en"
            },
            {
                "key": "button_1-template_param-body_text_1",
                "value": "1111"
            },
            {
                "key": "button_1-template_param-body_text_2",
                "value": "222"
            },
            {
                "key": "button_1-template_param-body_text_3",
                "value": "3"
            },
            {
                "key": "button_1-template_param-body_text_4",
                "value": "4"
            },
            {
                "key": "button_1-template_param-body_text_5",
                "value": "5"
            },
            {
                "key": "button_2-template_param-language",
                "value": "en"
            },
            {
                "key": "button_2-template_param-body_text_1",
                "value": "6"
            },
            {
                "key": "button_2-template_param-body_text_2",
                "value": "7"
            }
        ]
    },
    "request": {
        "method": "send_block",
        "id": "test_12345fsdfczxcswereaasdasads4545dwqweqwqdaseaf6",
        "sync": true,
        "meta": ""
    },
    "messaging_type": "RESPONSE",
    "message_tag": ""
}

3.2.4 实际接收效果

3.3 发送weburl带可变参数的模板

3.3.1 api代码调整

只需要在api的参数中加上下面的代码:

 "params": [
            {
                "key": "template_param-weburl",
                "value": "test"
            }
          ]

3.3.2 示例代码

{
    "recipient": {
        "id": "8617356956126"
    },
    "message": {
        "block_name": "4",
        "params": [
            {
                "key": "template_param-weburl",
                "value": "test"
            }
        ]
    },
    "request": {
        "method": "send_block",
        "id": "test_12345fsdfczxcswerew3545sadasdwqewqfsddaseaf6",
        "sync": true,
        "meta": ""
    },
    "messaging_type": "RESPONSE",
    "message_tag": ""
}

3.3.3 实际接收效果

点击“Check your account”后的效果

3.4 发送不带weburl可变参数的模板

3.4.1 带可替换图片、不带weburl可变参数的模板

示例代码

{
    "recipient": {
        "id": "8617356956126"
    },
    "message": {
        "block_name": "moto_service",
        "params": [
            {
                "key": "template_param-header_mutlimedia_url",
                "value": "https://assets.bothub.ai/bothub/admin-fe/assets/images/placeholder/image_horizontal.png"
            }
        ]
    },
    "request": {
        "method": "send_block",
        "id": "test_12345fsdfczxcswerew352122sdwqewqfsddasdsseaf6",
        "sync": true,
        "meta": ""
    },
    "messaging_type": "RESPONSE",
    "message_tag": ""
}

实际接收效果

点击“Details”

3.4.2 带固定图片、不带weburl可变参数的模板

可以直接发送bothub后台设置的图片,api中可以不需要传递图片的示例代码,只需要传递需要发送的内容块的名称。

示例代码

{
    "recipient": {
        "id": "8617356956126"
    },
    "message": {
        "block_name": "moto_service"
    },
    "request": {
        "method": "send_block",
        "id": "test_12345fsdfczxcswerew352122sdssdwqewqfsddasdsseaf6",
        "sync": true,
        "meta": ""
    },
    "messaging_type": "RESPONSE",
    "message_tag": ""
}

实际接收效果

点击“Details”

3.4.3 不带weburl可变参数的模板

例如要发送带网址跳转按钮,但是按钮中的网址链接里不放可变参数。

示例代码

{
    "recipient": {
        "id": "8617356956126"
    },
    "message": {
        "block_name": "bonus_points_en_G1G2G3"
    },
    "request": {
        "method": "send_block",
        "id": "test_123456",
        "sync": true,
        "meta": ""
    },
    "messaging_type": "RESPONSE",
    "message_tag": ""
}

接收到的效果

点击“Bonus Points”的实际效果

results matching ""

    No results matching ""