将 Flask、Twilio API 与 PythonAnywhere 结合使用时出现错误 11200 - HTTP 检索失败
Using Flask, Twilio API with PythonAnywhere gets error 11200 - HTTP retrieval failure
我制作了一个 flask 应用程序,它使用 Twilio Webhooks 检测在 whatsapp 上收到的消息。收到消息后,应用程序将消息发送回相同的 phone 号码。这可以完美地使用 Flask 和 Ngrok 来部署服务器并公开它。但是,一旦我将它部署到 PythonAnywhere,我就会在 Twilio 控制台中收到 11200 错误。这是代码。
from flask import Flask, request
import requests
from twilio.rest import Client
account_sid = 'xxxxx'
auth_token = 'xxxxx'
client = Client(account_sid, auth_token)
def mmsg(phono, body):
message = client.messages.create(
from_='whatsapp:+14155238886',
body=body,
to=phono,
)
app = Flask(__name__)
@app.route('/post', methods=['POST'])
def bot():
incoming_msg = request.values.get('Body', '').lower()
phono = request.values.get('From', "")
if incoming_msg == 'hi':
mmsg(phono, 'hello!')
if __name__ == '__main__':
app.run()
当我检查 PythonAnywhere 错误日志时,我得到了这个
2020-07-19 13:50:46,569: POST Request: https://api.twilio.com/2010-04-01/Accounts/AC84a8b5837227246efc0c6f9440b6e12c/Messages.json
2020-07-19 13:50:46,570: PAYLOAD: {'To': 'whatsapp:{myphonenumber}', 'From': 'whatsapp:+14155238886', 'Body': 'hello!'}
2020-07-19 13:50:49,576: Exception on /post [POST]
Traceback (most recent call last):
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.twilio.com', port=443): Max retries exceeded with url: /2010-04-01/Accounts/AC84a8b5837227246efc0c6f9440b6e12c/Messages.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fc0504362e8>: Failed to establish a new connection: [Errno 111] Connection refused',))
我试过像这样向 message = client.messages.create
添加另一个键和值。
message = client.messages.create(
from_='whatsapp:+14155238886',
body=item,
to=phono,
AC84a8b5837227246efc0c6f9440b6e12c='83ce0b901ff353f9b9a77222e001d71d'
)
当我尝试这样做时,我在 PythonAnywhere 上遇到了这个错误。
2020-07-19 14:09:51,030: Exception on /post [POST]
Traceback (most recent call last):
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/AbhayAysola/bot.py", line 46, in bot
mmsg(phono, ou)
File "/home/AbhayAysola/bot.py", line 24, in mmsg
AC84a8b5837227246efc0c6f9440b6e12c='83ce0b901ff353f9b9a77222e001d71d'
TypeError: create() got an unexpected keyword argument 'AC84a8b5837227246efc0c6f9440b6e12c'
PythonAnywhere 上的免费帐户限制了 Internet 访问;您只能访问一组特定的网站(listed here,但这是您可以访问的网站列表,而不是您无法访问的网站列表) ,并且您必须使用代理服务器才能访问它们。
对于大多数完全透明的库 -- 它们从系统环境中获取代理设置并使用它,而您无需执行任何额外操作。但是 Twilio 库需要一些额外的配置。有一个帮助页面解释了这是什么 here,但是对您的代码的具体更改是替换它:
client = Client(account_sid, auth_token)
...有了这个:
import os
from twilio.http.http_client import TwilioHttpClient
proxy_client = TwilioHttpClient(proxy={'http': os.environ['http_proxy'], 'https': os.environ['https_proxy']})
client = Client(account_sid, auth_token, http_client=proxy_client)
我制作了一个 flask 应用程序,它使用 Twilio Webhooks 检测在 whatsapp 上收到的消息。收到消息后,应用程序将消息发送回相同的 phone 号码。这可以完美地使用 Flask 和 Ngrok 来部署服务器并公开它。但是,一旦我将它部署到 PythonAnywhere,我就会在 Twilio 控制台中收到 11200 错误。这是代码。
from flask import Flask, request
import requests
from twilio.rest import Client
account_sid = 'xxxxx'
auth_token = 'xxxxx'
client = Client(account_sid, auth_token)
def mmsg(phono, body):
message = client.messages.create(
from_='whatsapp:+14155238886',
body=body,
to=phono,
)
app = Flask(__name__)
@app.route('/post', methods=['POST'])
def bot():
incoming_msg = request.values.get('Body', '').lower()
phono = request.values.get('From', "")
if incoming_msg == 'hi':
mmsg(phono, 'hello!')
if __name__ == '__main__':
app.run()
当我检查 PythonAnywhere 错误日志时,我得到了这个
2020-07-19 13:50:46,569: POST Request: https://api.twilio.com/2010-04-01/Accounts/AC84a8b5837227246efc0c6f9440b6e12c/Messages.json
2020-07-19 13:50:46,570: PAYLOAD: {'To': 'whatsapp:{myphonenumber}', 'From': 'whatsapp:+14155238886', 'Body': 'hello!'}
2020-07-19 13:50:49,576: Exception on /post [POST]
Traceback (most recent call last):
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.twilio.com', port=443): Max retries exceeded with url: /2010-04-01/Accounts/AC84a8b5837227246efc0c6f9440b6e12c/Messages.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fc0504362e8>: Failed to establish a new connection: [Errno 111] Connection refused',))
我试过像这样向 message = client.messages.create
添加另一个键和值。
message = client.messages.create(
from_='whatsapp:+14155238886',
body=item,
to=phono,
AC84a8b5837227246efc0c6f9440b6e12c='83ce0b901ff353f9b9a77222e001d71d'
)
当我尝试这样做时,我在 PythonAnywhere 上遇到了这个错误。
2020-07-19 14:09:51,030: Exception on /post [POST]
Traceback (most recent call last):
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/AbhayAysola/bot.py", line 46, in bot
mmsg(phono, ou)
File "/home/AbhayAysola/bot.py", line 24, in mmsg
AC84a8b5837227246efc0c6f9440b6e12c='83ce0b901ff353f9b9a77222e001d71d'
TypeError: create() got an unexpected keyword argument 'AC84a8b5837227246efc0c6f9440b6e12c'
PythonAnywhere 上的免费帐户限制了 Internet 访问;您只能访问一组特定的网站(listed here,但这是您可以访问的网站列表,而不是您无法访问的网站列表) ,并且您必须使用代理服务器才能访问它们。
对于大多数完全透明的库 -- 它们从系统环境中获取代理设置并使用它,而您无需执行任何额外操作。但是 Twilio 库需要一些额外的配置。有一个帮助页面解释了这是什么 here,但是对您的代码的具体更改是替换它:
client = Client(account_sid, auth_token)
...有了这个:
import os
from twilio.http.http_client import TwilioHttpClient
proxy_client = TwilioHttpClient(proxy={'http': os.environ['http_proxy'], 'https': os.environ['https_proxy']})
client = Client(account_sid, auth_token, http_client=proxy_client)