python - 确保 Twilio 按顺序发送消息链的最可靠、最有效的方法是什么?

标签 python django twilio twilio-api

我正在尝试使用 Twilio API 及其 Python 包装器为相对简单的消息传递系统奠定基础。用户应该能够发送关键字并收到一串回复。

问题是,使用 Twilio 文档中建议的 HttpResponse() 包,当我循环遍历消息以将它们添加到响应链时,它们会在一个 block 中命中 Twilio,并发送服务以看似随机的顺序发送它们,这确实损害了 block 的可读性。

这是我在 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 个字符的大消息中,然后让运营商将其分解。这取决于您的客户群中的运营商是否可靠地支持串联短信。这可能会解决订单问题。

关于python - 确保 Twilio 按顺序发送消息链的最可靠、最有效的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58880468/

相关文章:

python - 如何让 Sprite 旋转面向鼠标?

Python QuTip : Plot full lines in Bloch sphere

django modelformset_factory - 管理表单数据丢失

python - Django REST Framework - 每个方法的单独权限

特维里奥 "Access Token has expired or expiration date is invalid"

用于可编程短信状态回调 URL 的 Twilio HTTP 身份验证

python - 我无法让 OpenID 工作(openid.consumer.consumer.ProtocolError : Parameter next not in return_to URL)

python - 在特定位置插入xml节点

python - 在 Django 的自定义管理器中捕获 DoesNotExist 异常

ios - 从 videoView、Twilio 视频中清除一帧