Twilio WhatsApp 消息处于 'queued' 状态

Twilio WhatsApp messages are in 'queued' status

好的,我得到了 WhatsApp 和 Twilio 的批准(在 Facebook 业务验证后)使用 WhatsApp API 向我的客户发送约会提醒。我配置了消息模板,它们也获得了批准。检查下图:

我在 Python 中编写了一段代码,我从托管在云上的 PostgreSQL 服务器(使用 psycopg2)中选取我的数据,然后它向使用查询获取的 phone 号码发送消息.这是代码:

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)

每当我 运行 此代码时,返回的消息状态总是 'queued'。我检查过我没有使用 'Test Credentials' 而是 'Live Credentials'.

我还检查了 error_code 和 error_message,其中 returns 为 NULL。所以没有错误,但没有发送消息。我该如何更改?

如有任何帮助,我们将不胜感激。

另请注意,上述代码中使用的消息正文已批准 作为 WhatsApp 的模板。

这里是 Twilio 开发人员布道者。

在您发出 API 发送消息的请求时,状态将被 return 编码为 "queued"。这就是您的代码中的这一点:

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)

等待 10 秒,然后从您创建消息时 return 编辑的消息对象中读取状态仍然 return "queued".

如果您想在 10 秒后获取消息状态以查看是否已发送,则需要再次调用 messages API,如下所示:

time.sleep(10)
latest_message = client.messages(message.sid).fetch()
print(latest_message.status)

如需更有效地跟踪邮件状态的方法,请查看此 tutorial on receiving webhooks for message status updates

让我知道是否有帮助。