确保 Twilio 按顺序发送消息链的最可靠和最有效的方法是什么?
What is the most reliable and efficient way to ensure Twilio sends a chain of messages in order?
我正在尝试使用 Twilio API 及其 Python 包装器为相对简单的消息传递系统奠定基础。用户应该能够输入关键字并收到一串回复。
问题是,使用 Twilio 文档中建议的 HttpResponse() 包,当我遍历消息以将它们添加到响应链时,它们以块的形式命中 Twilio,并发送服务以看似随机顺序,这确实损害了块的可读性。
这是我 运行 在 Django 中创建响应的循环的简化版本:
@csrf_exempt
def inbox(request)
if request.method == "POST":
# Interpret the keyword in the incoming message
data = request.POST
messageBody = data['Body'].lower()
# Check if it matches a valid keyword
targetTrigger = Keyword.objects.get(trigger_word=messageBody)
# Pull the appropriate responses
messageChain = Message.objects.filter(keyword_answers=targetTrigger)
# Create the messaging response
resp = MessagingResponse()
# Populate it with messages
for sms in messageChain:
resp.message(sms)
# Send them to Twilio
return HttpResponse(str(resp))
为了便于阅读,我忽略了尝试和错误捕获。就像我说的那样,这会以看似随机的顺序发送消息,更短的消息似乎会首先发送到我的 iPhone。并非总是如此,但足以让我需要重新考虑这种方法。
奇怪的是,文档中几乎没有关于在 SMS HttpResponse 中发送多条消息的信息,尽管我认为这是一个常见的用例。我正在考虑的替代方案是发回一个空白的 HttpResponse 以简单地向 Twilio 确认消息已成功接收,然后将其常规的一对一发送方法与我的 for 循环一起使用。这看起来效率有点低,但我需要消息发送顺序的准确性。
有什么建议吗? Twilio 开发人员布道者,我知道你们都在这里。
根据 Twilio 的说法,this cannot be done. 因为他们根本无法控制交付过程。
Twilio cannot guarantee that SMS messages sent from your Twilio phone number will arrive in order. While we will send the SMS messages you pass to us in the order that you’ve queued them, the SMS messages are delivered individually with no association to each other. The order of delivery depends on the carrier.
To help your users understand the order of your messages, we recommend
that you append a page reference following each message, for example
1/3, 2/3, 3/3.
在 SMS 中,最短的消息通常发送速度最快。您可能会将消息设计为按大小排序。这可能会减少错误的频率。
另一种选择可能是将它们全部捆绑成一条 1600 字符的大消息,并让运营商处理将其拆分的问题。这将取决于您的客户群中的运营商是否可靠地支持串联 SMS。这可能会处理订单问题。
我正在尝试使用 Twilio API 及其 Python 包装器为相对简单的消息传递系统奠定基础。用户应该能够输入关键字并收到一串回复。
问题是,使用 Twilio 文档中建议的 HttpResponse() 包,当我遍历消息以将它们添加到响应链时,它们以块的形式命中 Twilio,并发送服务以看似随机顺序,这确实损害了块的可读性。
这是我 运行 在 Django 中创建响应的循环的简化版本:
@csrf_exempt
def inbox(request)
if request.method == "POST":
# Interpret the keyword in the incoming message
data = request.POST
messageBody = data['Body'].lower()
# Check if it matches a valid keyword
targetTrigger = Keyword.objects.get(trigger_word=messageBody)
# Pull the appropriate responses
messageChain = Message.objects.filter(keyword_answers=targetTrigger)
# Create the messaging response
resp = MessagingResponse()
# Populate it with messages
for sms in messageChain:
resp.message(sms)
# Send them to Twilio
return HttpResponse(str(resp))
为了便于阅读,我忽略了尝试和错误捕获。就像我说的那样,这会以看似随机的顺序发送消息,更短的消息似乎会首先发送到我的 iPhone。并非总是如此,但足以让我需要重新考虑这种方法。
奇怪的是,文档中几乎没有关于在 SMS HttpResponse 中发送多条消息的信息,尽管我认为这是一个常见的用例。我正在考虑的替代方案是发回一个空白的 HttpResponse 以简单地向 Twilio 确认消息已成功接收,然后将其常规的一对一发送方法与我的 for 循环一起使用。这看起来效率有点低,但我需要消息发送顺序的准确性。
有什么建议吗? Twilio 开发人员布道者,我知道你们都在这里。
根据 Twilio 的说法,this cannot be done. 因为他们根本无法控制交付过程。
Twilio cannot guarantee that SMS messages sent from your Twilio phone number will arrive in order. While we will send the SMS messages you pass to us in the order that you’ve queued them, the SMS messages are delivered individually with no association to each other. The order of delivery depends on the carrier.
To help your users understand the order of your messages, we recommend that you append a page reference following each message, for example 1/3, 2/3, 3/3.
在 SMS 中,最短的消息通常发送速度最快。您可能会将消息设计为按大小排序。这可能会减少错误的频率。
另一种选择可能是将它们全部捆绑成一条 1600 字符的大消息,并让运营商处理将其拆分的问题。这将取决于您的客户群中的运营商是否可靠地支持串联 SMS。这可能会处理订单问题。