Twilio-在收到短信后将短信发送到号码(Python)

时间:2018-07-04 10:30:51

标签: python sms twilio

Twilio的新手,成功地遵循了SMS Python Quickstart guide,并且将两位组合在一起,但是现在有了一些冗余代码,如果没有错误,我似乎无法摆脱它们。

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

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

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

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

senderNo = '+442030954971'
receiverNo = '+447496070859'

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

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def sms_reply():

    messages = client.messages.list()

    print("Message received: " + messages[0].body)

    coord = messages[0].body

    lat,lon = coord.split(":")

    mapURL = "https://www.google.com/maps/search/?api=1&query=" + lat + "," + lon

    print("Sending message")

    message = client.messages.create(
        body=mapURL,
        from_=senderNo,
        to=receiverNo
    )

    """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)

每当我删除与发送给发件人的回复邮件有关的行时,似乎都会破坏我仍然需要的其他行。

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

1 个答案:

答案 0 :(得分:0)

如果我正确理解,您有3个数字,即发送者,接收者和final_destination。所以您需要定义两条消息

  1. message_received:从接收方到发送方
  2. message_map:从接收者到最终目的地

每个都有自己的Messagingresponce对象