【问题标题】:How to respond to text message with Django/Twilio如何使用 Django/Twilio 回复短信
【发布时间】:2013-07-31 22:23:13
【问题描述】:

我设置了一个页面“example.com/firstsms”以通过 Twilio 发送 SMS 并重定向到主页。如果我随后使用电话回复我想回复确认的消息。到目前为止,什么都没有发生。

在 urls.py 我有:

(r'^hellomonkey/$', 'crusher.views.HelloMonkey'),

在views.py中我尝试修改他们的Flask example:

def HelloMonkey(request):
    """Respond to incoming calls with a simple text message."""

    resp = twilio.twiml.Response()
    resp.sms("Hello, Mobile Monkey") 
    return HttpResponseRedirect(str(resp), content_type="text/plain")

绞尽脑汁!谢谢

【问题讨论】:

    标签: python django twilio


    【解决方案1】:

    您正在返回一个HttpResponseRedirect,因此它会尝试重定向到某个页面,当然它不起作用。你应该改用HttpResponse

    from django.http import HttpResponse
    
    def HelloMonkey(request):
        """Respond to incoming calls with a simple text message."""
    
        resp = twilio.twiml.Response()
        resp.sms("Hello, Mobile Monkey") 
        return HttpResponse(str(resp))
    

    【讨论】:

    • 谢谢!我试图创建条件响应并开始另一个问题here
    【解决方案2】:

    来自 Twilio 的 Devangelist,我一直在维护 Django-twilio 库,并且有一个更简单的解决方案来使用 Twilio 响应 SMS 和语音。

    首先,pip install django-twilio and get it set up using the instructions here

    然后在您的视图中,您可以像这样使用twilio_view 装饰器:

    from django_twilio.decorators import twilio_view
    from twilio.twiml import Response    
    
    @twilio_view
    def my_view(request):
        r = Response()
        r.message('Hello, world!')
        return r
    

    Django-twilio 将为您处理正确的 HTTPResponse 内容,并确保请求来自真正的 twilio.com 源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多