【发布时间】:2020-06-26 20:16:14
【问题描述】:
好的,所以我得到了 WhatsApp 和 Twilio 的批准(在 Facebook Business 验证之后)使用 WhatsApp API 向我的客户发送约会提醒。我配置了消息模板,它们也获得了批准。查看下图:
我用 Python 编写了一个代码,我从托管在云上的 PostgreSQL 服务器中选择我的数据(使用 psycopg2),然后它将消息发送到使用查询获取的电话号码。代码如下:
from twilio.rest import Client
import psycopg2
import time
account_sid = 'AC54xxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'f1384yyyyyyyyyyyyyyyyyyyyyyyyyyy'
connection_string = ""
conn = psycopg2.connect(user = "xxxx",
password = "yyyyyy",
host = "zzzzzzzzzzzzzzz.zzzzzzzzzzzz",
port = "ABCD",
database = "some_db")
cur = conn.cursor()
cur.execute("""query to pick data""")
rows = cur.fetchall()
client_phone_list = []
phone_list_not_received = []
session_date_list = []
session_time_list = []
client_first_name_list = []
for row in rows:
session_date_list.append(row[0])
session_time_list.append(row[1])
client_first_name_list.append(row[2])
client_phone_list.append(row[3])
cur.close()
conn.close()
client = Client(account_sid, auth_token)
message_reminder_template = """Hello {},
This is a reminder about your session today at {}. Please be on time to utilize the full length of
the session and avoid distress :)
We look forward to taking care of you!"""
for i in range(len(client_phone_list)):
first_name = client_first_name_list[i]
appointment_time = session_time_list[i]
message_body = message_reminder_template.format(first_name, appointment_time)
print(message_body)
message = client.messages.create(body = str(message_body),
from_ = 'whatsapp:+1(mytwilionumber)',
to = 'whatsapp:+91'+client_phone_list[i])
time.sleep(10)
text_status = message.status
print(text_status)
每当我运行此代码时,返回的消息状态总是“排队”。我检查过我使用的不是“测试凭据”而是“实时凭据”。
我还检查了返回为 NULL 的 error_code 和 error_message。所以没有错误,但没有发送消息。我该如何改变呢?
任何帮助将不胜感激。
另请注意,上述代码中使用的消息正文已批准作为来自 WhatsApp 的模板。
【问题讨论】:
-
您是否查看过控制台中的 Twilio 调试器,看看是否有任何事件记录在那里,twilio.com/docs/usage/troubleshooting/…?另外,您能否发送一条单独的消息(使用不同的批准模板),看看结果是什么,按照这里的代码 sn-ps,twilio.com/docs/sms/whatsapp/quickstart。如果两者都没有阐明,请在 help@twilio.com 上开一张票,并附上您原始帖子的详细信息,下一步将是上述结果。
-
在这种情况下,我建议发送电子邮件至 help@twilio.com(或从您的 Twilio 控制台中打开票证),以便他们深入研究问题。如果您有完全匹配的模板,它不应该拒绝该消息。
-
我已通过提出请求联系了 Twilio 支持。希望尽快得到解决方案。一旦有人遇到同样的问题,我会尽快发布解决方案。
标签: twilio twilio-api