[英]Twilio/Django not receiving response SMS
I want to text a twilio number and start a series of questions for a user. 我想输入一个twilio号码,并向用户提出一系列问题。 If it is their first time texting, a new "Caller" should be created. 如果这是他们第一次发短信,则应创建一个新的“呼叫者”。 If they have played before, I'd like to look up the "last_question", we asked them and ask them the appropriate question. 如果他们以前玩过,我想查询“ last_question”,我们问他们并问他们适当的问题。 My code below yields no SMS response and a Twilio error "HTTP retrieval failure." 我的下面代码没有产生SMS响应,并且没有Twilio错误“ HTTP检索失败”。
In models.py I have 在models.py我有
class Caller(models.Model):
body = models.CharField(max_length=200)
from_number = models.CharField(max_length=20)
last_question = models.CharField(max_length=2, default="0")
def __unicode__(self):
return self.body
In views.py 在views.py中
def hello_there(request):
body = request.REQUEST.get('Body', None)
from_number = request.REQUEST.get('From', None)
try:
caller = Caller.objects.get(from_number = from_number)
except Caller.DoesNotExist:
caller = None
if caller:
if caller.last_question == "0":
if body == "Password":
message = "Welcome to the game. What is 3 + 4?"
caller.last_question = "1"
else:
message = "What is the password?"
else:
message = "you broke me"
else:
new_caller = Caller(body=body, from_number=from_number, last_question="0")
new_caller.save()
message = "New user created"
resp = twilio.twiml.Reponse()
resp.sms(message)
return HttpResponse(str(resp))
Twilio employee here - the issue could be because you're not providing a csrf_exempt
decorator around this view. Twilio员工在这里-问题可能是因为您没有在此视图周围提供csrf_exempt
装饰器。 Django will trigger a security error because it is receiving a HTTP POST
request from twilio.com. Django将收到来自twilio.com的HTTP POST
请求,从而触发安全错误。 Django will not accept any HTTP POST
request without a csrf token unless you make it exempt. 除非您将其免除,否则Django将不接受没有csrf令牌的任何HTTP POST
请求。
Have you thought about using the django-twilio package for Django? 您是否考虑过将django-twilio软件包用于Django? It will make your life much easier when developing with twilio. 使用twilio进行开发时,它将使您的生活更加轻松。 This is what your view will look like with django-twilio: 这就是django-twilio的视图:
from django_twilio.decorators import twilio_view
@twilio_view
def hello_there(request):
body = request.REQUEST.get('Body', None)
from_number = request.REQUEST.get('From', None)
try:
caller = Caller.objects.get(from_number=from_number)
except Caller.DoesNotExist:
caller = None
if caller:
if caller.last_question == "0":
if body == "Password":
message = "Welcome to the game. What is 3 + 4?"
caller.last_question = "1"
else:
message = "What is the password?"
else:
message = "you broke me"
else:
new_caller = Caller(body=body, from_number=from_number, last_question="0")
new_caller.save()
message = "New user created"
resp = twilio.twiml.Reponse()
resp.sms(message)
return resp
The twilio_view
decorator will provide csrf exemption as well as ensuring all your requests are genuine and from twilio.com. twilio_view
装饰器将提供csrf豁免,并确保所有请求都是真实的并且来自twilio.com。
Check out the installation instructions to get started. 请查看安装说明以开始使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.