Twilio SMS Python - Simple send a message

邮差的信 提交于 2019-12-01 13:32:21

It's because your output in your sms_reply function is returning a TwiML that sends a message. I think it's normal to have some feedback from the service. If you don't want to reply with the mapURL you can just say somethink like "Thank you".

Otherwise, you can take a look to TwiML documentation to see what other actions you can do.

Twilio developer evangelist here.

You don't need to reply back to the incoming message and you can avoid this by returning an empty TwiML response.

As an extra win here, you don't have to call the API to get the body of the last message that was sent. That will be available in the POST request parameters to the endpoint. You can access those via request.form so the Body parameter will be available at request.form['Body'].

Try something like this:

# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse


# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'account_sid'
auth_token = 'auth_token'
client = Client(account_sid, auth_token)

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def sms_reply():
    coord = request.form['Body']
    lat,lon = coord.split(":")
    mapURL = "https://www.google.com/maps/search/?api=1&query=" + lat + "," + lon

    message = client.messages.create(
        body=mapURL,
        from_='+442030954643',
        to='+447445678954'
    )

    # Start our empty response
    resp = MessagingResponse()
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!