如何从 python 客户端指定 Twilio Whatsapp 模板?

问题描述

我想使用带有自定义模板的 Twilio Whatsapp 消息。我正在尝试使用 Python 客户端发送消息。我们有不同类型的消息,我们希望从 Twilio 后端管理它们。我们创建了几个模板,但我不知道如何在代码中指定模板。

官方文档中没有提到这一点。我查看了客户端的源代码,这里是 MessageList 类中的代码,用于创建消息:

    def create(self,to,status_callback=values.unset,application_sid=values.unset,max_price=values.unset,provide_Feedback=values.unset,attempt=values.unset,validity_period=values.unset,force_delivery=values.unset,content_retention=values.unset,address_retention=values.unset,smart_encoded=values.unset,persistent_action=values.unset,from_=values.unset,messaging_service_sid=values.unset,body=values.unset,media_url=values.unset):

函数参数中没有与模板名称相似的内容

是否有可能以编程方式指定模板?

解决方法

from twilio.rest import Client

client = Client() # this is the Twilio sandbox testing number

from_whatsapp_number = 'whatsapp:+14155238886'

to_whatsapp_number = 'whatsapp:+15005550006'
client.messages.create(body='Ahoy,world!',from_=from_whatsapp_number,to=to_whatsapp_number)
,

这里是 Twilio 开发者布道者。

当您想使用模板发送消息时,您要做的就是确保您发送的消息正文与模板匹配。例如,如果您的模板是:

Your login code for Your login code for Twilio is 12345.
 is {{2}}.

然后发送消息:

{{1}}

匹配该模板并将成功发送。因此,您无需以编程方式指定模板,只需使用您的消息内容即可。

您可以看到有关 sending template messages with the Twilio API for WhatsApp here 的更多信息。