简体   繁体   English

使用Python中的Twilio接收和处理SMS

[英]Receiving and processing an SMS with Twilio in Python

I'm learning python and as a project I'm trying to create a program that will recieve an SMS message, process it, and then depending on what is in that message, send back information. 我正在学习python,作为一个项目,我正在尝试创建一个程序来接收SMS消息,处理它,然后根据该消息中的内容,发回信息。

I am using Twilio in python with Flask and ngrok to do all the SMS stuff, but I am still not sure how to actually receive an SMS as data that I can read and process, as there is no documentation that I can find on it. 我在使用Flask和ngrok的python中使用Twilio来完成所有短信的工作,但我仍然不确定如何实际接收短信作为我可以阅读和处理的数据,因为没有我可以在其上找到的文档。 It would be great if someone could help me with this. 如果有人可以帮助我,这将是很好的。

I already know how to receive and send SMS with Twilio, I just need to know how to get the precise message that was sent to my Twilio number. 我已经知道如何使用Twilio接收和发送短信,我只需要知道如何获得发送到我的Twilio号码的准确信息。

I believe you already know how to configure Twilio to hit your endpoint when a message comes in. If you configure at Twilio for a POST request, then the data passed to you from Twilio will be in request.form . 我相信您已经知道如何配置Twilio以在消息进入时命中您的端点。如果您在Twilio配置POST请求,那么从Twilio传递给您的数据将在request.form

If you take a look at Twilio's example here: 如果你看看Twilio的例子:
( https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-python ) https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-python
indeed the example makes no use of the data that is coming in. 事实上,这个例子没有使用即将发布的数据。

The modified code below shows some data that is available from the request (and you can write your code depending on what you'd like to do with it). 下面修改后的代码显示了请求中可用的一些数据(您可以根据自己想要的代码编写代码)。

  • the number from where the message was sent request.form['From'] , 消息发送的号码request.form['From']
  • your Twilio number request.form['To'] 你的Twilio号码request.form['To']
  • and the body of the message request.form['Body'] 和消息的主体request.form['Body']


    from flask import Flask, request, redirect
    from twilio.twiml.messaging_response import MessagingResponse

    app = Flask(__name__)

    @app.route("/sms", methods=['POST'])
    def sms_reply():
        """Respond to incoming calls with a simple text message."""

        # Use this data in your application logic
        from_number = request.form['From']
        to_number = request.form['To']
        body = request.form['Body']

        # Start our TwiML response
        resp = MessagingResponse()

        # Add a message
        resp.message("The Robots are coming! Head for the hills!")

        return str(resp)

    if __name__ == "__main__":
        app.run(debug=True)


Some other parameters are also available in the request: 请求中还提供了一些其他参数:

  • MessageSid
  • SmsSid
  • AccountSid
  • MessagingServiceSid
  • From
  • To
  • Body
  • NumMedia

Docs: ( https://www.twilio.com/docs/sms/twiml#request-parameters ) 文件:https://www.twilio.com/docs/sms/twiml#request-parameters

Also you can find more examples if you google for "twilio blog python flask" 如果你谷歌为"twilio blog python flask"你也可以找到更多的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM