I'm using Twilio and Django for an SMS application (I started learning Python yesterday, be gentle).
我正在使用Twilio和Django作为一个SMS应用程序(我昨天开始学习Python,要温和一些)。
Here's what I need help with: When Twilio sends an incoming SMS message to my URL, I want my app to automatically add the incoming phone number, date/time, and incoming message to some lists. How do I do that?
我需要帮助的是:当Twilio向我的URL发送一条短信时,我希望我的应用程序自动地将收到的电话号码、日期/时间和短信添加到一些列表中。我该怎么做呢?
Thanks!
谢谢!
2 个解决方案
#1
2
Twilio sends it as the 'From' parameter when it requests your URL. The documentation is at: http://www.twilio.com/docs/api/twiml/sms/message
Twilio在请求您的URL时将其作为“From”参数发送。文档在:http://www.twilio.com/docs/api/twiml/sms/message
It will be similar to this code, I wrote this in webapp with python (not Django).
它将类似于这段代码,我在webapp中用python(不是Django)编写了这段代码。
class MainHandler(webapp.RequestHandler):
def get(self):
model = Storage()
from = self.request.get("From")
if from is not '':
model.sms_from = self.request.get("From")
model.sms_body = self.request.get("Body")
model.put()
models = Storage.all()
for i in models:
self.response.out.write(i.sms_from + ' ' + i.sms_body +'<br>')
UPDATE:
更新:
When I get my phone and send a text to xxx-xxx-xxxx, twilio will receive that text and then make a request to the URL I configured.
当我得到我的手机并发送一个文本到xxxx -xxxx时,twilio将接收到该文本,然后对我配置的URL发出请求。
From that point it looks exactly the same as a request from a web browser.
从那时起,它看起来与来自web浏览器的请求完全相同。
This question will help you with the specifics with Django Capturing url parameters in request.GET
这个问题将帮助您了解Django捕获request.GET中的url参数的细节
There should be all the parameters you need from the sender.
应该有从发送方需要的所有参数。
#2
0
I just wrote a sample app using Twilio and Django on GitHub, but here's the specific answer you're looking for.
我刚刚在GitHub上用Twilio和Django编写了一个示例应用程序,但这里是您要寻找的具体答案。
For this example I'm using the '/reply/' URL on my server (and have told Twilio that's where to POST to)
在本例中,我在服务器上使用“/reply/”URL
In urls.py you're just going to do a standard mapping: url(r'^reply', 'twilio_sms.views.sms_reply'),
在url。py你只是要做一个标准的映射:url(r ^回复,“twilio_sms.views.sms_reply”),
Here's a really basic response to the user, responding with what the Twilio server expects. def sms_reply(request): if request.method == 'POST': params = request.POST phone = re.sub('+1','',params['From'])
这是对用户的基本响应,使用Twilio服务器的期望进行响应。def sms_reply(请求):如果请求。方法= 'POST': params =请求。后电话= re.sub(“+ 1”,“params['从'])
response_message = '<?xml version="1.0" encoding="UTF-8">'
response_message += '<Response><Sms>Thanks for your text!</Sms></Response>'
return HttpResponse(response_message)
In my code I actually build the XML response using a library, which I suggest in general, but for a code sample it's too much information.
在我的代码中,我实际上是使用一个库构建XML响应,我建议一般来说,但是对于一个代码示例来说,它是太多的信息。
#1
2
Twilio sends it as the 'From' parameter when it requests your URL. The documentation is at: http://www.twilio.com/docs/api/twiml/sms/message
Twilio在请求您的URL时将其作为“From”参数发送。文档在:http://www.twilio.com/docs/api/twiml/sms/message
It will be similar to this code, I wrote this in webapp with python (not Django).
它将类似于这段代码,我在webapp中用python(不是Django)编写了这段代码。
class MainHandler(webapp.RequestHandler):
def get(self):
model = Storage()
from = self.request.get("From")
if from is not '':
model.sms_from = self.request.get("From")
model.sms_body = self.request.get("Body")
model.put()
models = Storage.all()
for i in models:
self.response.out.write(i.sms_from + ' ' + i.sms_body +'<br>')
UPDATE:
更新:
When I get my phone and send a text to xxx-xxx-xxxx, twilio will receive that text and then make a request to the URL I configured.
当我得到我的手机并发送一个文本到xxxx -xxxx时,twilio将接收到该文本,然后对我配置的URL发出请求。
From that point it looks exactly the same as a request from a web browser.
从那时起,它看起来与来自web浏览器的请求完全相同。
This question will help you with the specifics with Django Capturing url parameters in request.GET
这个问题将帮助您了解Django捕获request.GET中的url参数的细节
There should be all the parameters you need from the sender.
应该有从发送方需要的所有参数。
#2
0
I just wrote a sample app using Twilio and Django on GitHub, but here's the specific answer you're looking for.
我刚刚在GitHub上用Twilio和Django编写了一个示例应用程序,但这里是您要寻找的具体答案。
For this example I'm using the '/reply/' URL on my server (and have told Twilio that's where to POST to)
在本例中,我在服务器上使用“/reply/”URL
In urls.py you're just going to do a standard mapping: url(r'^reply', 'twilio_sms.views.sms_reply'),
在url。py你只是要做一个标准的映射:url(r ^回复,“twilio_sms.views.sms_reply”),
Here's a really basic response to the user, responding with what the Twilio server expects. def sms_reply(request): if request.method == 'POST': params = request.POST phone = re.sub('+1','',params['From'])
这是对用户的基本响应,使用Twilio服务器的期望进行响应。def sms_reply(请求):如果请求。方法= 'POST': params =请求。后电话= re.sub(“+ 1”,“params['从'])
response_message = '<?xml version="1.0" encoding="UTF-8">'
response_message += '<Response><Sms>Thanks for your text!</Sms></Response>'
return HttpResponse(response_message)
In my code I actually build the XML response using a library, which I suggest in general, but for a code sample it's too much information.
在我的代码中,我实际上是使用一个库构建XML响应,我建议一般来说,但是对于一个代码示例来说,它是太多的信息。