【问题标题】:Twilio SMS Python - Simple send a messageTwilio SMS Python - 简单发送消息
【发布时间】:2018-07-04 08:58:13
【问题描述】:

Twilio 的新手,已成功跟踪 SMS Python Quickstart guide,并将两个位组合在一起,但现在有一些冗余代码,我似乎无法在不出错的情况下摆脱它们。

我有 Python 代码,它从文本消息中获取坐标,将其转换为 Google 地图链接,然后将该链接发送到不同的电话号码。

但是,目前它也在向原始发件人电话号码发送此回复,因为这是您设置的原始指南。

我只希望它把消息发送到指定的号码,而不是回复原发件人。

运行.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)

每当我删除我认为与向发件人回复消息有关的行时,它似乎破坏了我仍然需要的其他一些行。

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

【问题讨论】:

    标签: python http sms twilio


    【解决方案1】:

    这是因为您在 sms_reply 函数中的输出正在返回一个发送消息的 TwiML。我认为从服务中获得一些反馈是正常的。如果您不想使用 mapURL 进行回复,您可以说“谢谢”之类的想法。

    否则,您可以查看TwiML documentation 以了解您可以执行哪些其他操作。

    【讨论】:

      【解决方案2】:

      这里是 Twilio 开发者宣传员。

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

      作为额外的胜利,您不必调用 API 来获取最后发送的消息的正文。这将在端点的 POST 请求参数中可用。您可以通过 request.form 访问这些参数,因此 Body 参数将在 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多