Twilio SMS Python-简单地发送消息 [英] Twilio SMS Python - Simple send a message

查看:95
本文介绍了Twilio SMS Python-简单地发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Twilio的新手,已成功遵循 SMS Python快速入门指南,并且已经将两位组合在一起,但是现在有了一些冗余代码,如果没有错误,我似乎无法摆脱这些冗余代码.

New to Twilio, have followed the SMS Python Quickstart guide successfully, and have combined two bits together, but now have some redundant code that I can't seem to get rid of without getting errors.

我有Python代码,该代码从短信中获取坐标,然后将其转换为Google Maps链接,然后将该链接发送到其他电话号码.

I have Python code that takes in coordinates from a text message, converts this into a Google Maps link, and sends that link to a different phone number.

但是,当前它还会将此回复发送到原始发件人的电话号码,因为这是您设置的原始指南.

However, currently it is also sending this reply to the original sender phone number, as that is what the original guide has you set up.

我只希望它将邮件发送到指定的号码,而不是回复原始发件人.

I only want it to send the message to the specified number, not reply to the original sender.

run.py:

# /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():

    messages = client.messages.list()

    print(messages[0].body)

    coord = messages[0].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'
    )

    """Respond to incoming messages with a friendly SMS."""
    # Start our response
    resp = MessagingResponse()

    # Add a message
    resp.message(mapURL)

    return str(resp)

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

每当我删除与发送给发件人的回复邮件有关的行时,似乎都中断了我仍然需要的其他一些行.

Whenever I take out the lines I think are to do with the replying message to sender, it seems to break some of the other lines that I still need.

非常感谢任何帮助,谢谢!

Any help much appreciated, thanks!

推荐答案

此处是Twilio开发人员的福音.

Twilio developer evangelist here.

您不需要回复传入的消息,可以通过返回一个空的TwiML响应来避免这种情况.

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

作为一个额外的胜利,您不必调用API即可获取发送的最后一条消息的正文.这将在端点的POST请求参数中可用.您可以通过request.form访问它们,因此Body参数将在request.form['Body']处可用.

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'].

尝试这样的事情:

# /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)

这篇关于Twilio SMS Python-简单地发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆