python - HTTP 检索错误 Twilio

标签 python heroku flask twilio

我是 Twilio 的新手,我正在尝试通过 python 发送和接收短信。这是我的代码

import os
from twilio.rest import TwilioRestClient
from twilio import twiml
from flask import Flask, request, redirect

app = Flask(__name__)
port = int(os.environ.get('PORT', 5000))


# put your own credentials here
ACCOUNT_SID = "..."
AUTH_TOKEN = "..."

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

def respond (recipient, twilio_account, body):

    message = client.messages.create(
        to=recipient,
        from_=twilio_account,
        body=body,
    )

@app.route('/sms', methods=['POST'])
def receive_sms():
    number = request.form['From']
    body = request.form['Body']
    # print "Message received from {0} saying {1}".format(number, body)
    response_message = "Message received from {0} saying {1}".format(number, body)

    resp = twiml.Response()
    resp.message(response_message)
    return str(resp)

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=port)

每次我向 Twilio 号码发送短信时,我都会收到 11200 错误。发生了什么事?

最佳答案

此错误indicates that Twilio didn't receive a response from your code within 15 seconds .

我看不出您的代码运行缓慢的任何原因,但 Twilio 可能根本无法访问它。您是否已采取任何措施让来自 Internet 的请求进入您的本地网络?

一个有用的策略是使用类似 ngrok 的内容。当您仍处于开发过程中以隧道传输来自外部世界的流量时。安装ngrok后你can run

ngrok http 5000

将流量通过隧道传输到本地主机端口 5000。然后配置 Twilio 以连接到 ngrok 隧道。

关于python - HTTP 检索错误 Twilio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321844/

相关文章:

node.js - 将 Node js 应用程序部署到 Heroku

python - 如何向 Flask Restful 服务构造函数发送参数?

python - Xlsxwriter:是否可以在单元格上进行东亚语言竖排文本?

python - 如何读取行(从文件中)然后在python中附加+打印?

python - 如何在其余代码运行而不停止的情况下每隔 x 秒打印一次?

python - 批量发出请求时出现编码问题?

ruby-on-rails - Heroku asset_sync gem 没有上传到 s3

ruby - 英雄联盟 "Push rejected, failed to detect set buildpack heroku/ruby"

algorithm - Delta E (CIE Lab) 在 SQL 中计算和排序的性能

python - 关联对象处理、将子项附加到父项、隐藏 “middle” 表的使用