我是 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/