2017-02-18 50 views
0

我是新来的Twilio,我尝试发送,并通过蟒蛇接收短信。这里是我的代码HTTP检索错误,Twilio

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) 

我不断收到一个错误11200每次我的文字我的Twilio号。发生什么事?

回答

2

此错误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隧道。

+0

我明白ngrok是一种选择,但我不明白为什么这会导致一个错误 –

+0

@JeremyFisher,不知何故Twilio需要能够打的网址在您的应用程序。 ngrok是让这种事情发生的简单方法。还有其他的(例如,您可以使用端口转发,如果您没有静态IP地址,可能会与动态DNS工具一起使用)。你做过那样的事吗? – Chris

+0

@JeremyFisher,是你能得到这个工作? – Chris