我正在使用recastai编写FB机器人,我在跟踪此示例https://recast.ai/docs/api-reference/#request-text,并编写了此代码来分析文本

from flask import Flask
import recastai

app = Flask(__name__)

@app.route('/')
def home():
    request = recastai.Request('TOKEN')
    response = request.analyse_text('Hello')
    return 'hello'


输出页面显示“内部服务器错误”,错误日志显示导入错误,但是仅在添加响应行后才会显示

2017-12-03 05:41:16,767:

***************************************************
2017-12-03 06:48:14,534: Error running WSGI application
2017-12-03 06:48:14,555: requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.recast.ai', port=443): Max retries exceeded with url: /v2/request (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden',)))
2017-12-03 06:48:14,555:   File "/var/www/tarekhassan_pythonanywhere_com_wsgi.py", line 16, in <module>
2017-12-03 06:48:14,556:     from hello import app as application
2017-12-03 06:48:14,556:
2017-12-03 06:48:14,556:   File "/home/tarekhassan/bot/hello.py", line 7, in <module>
2017-12-03 06:48:14,556:     def home():
2017-12-03 06:48:14,556:
2017-12-03 06:48:14,556:   File "/home/tarekhassan/.local/lib/python3.5/site-packages/recastai/apis/request/analyse.py", line 27, in analyse_text
2017-12-03 06:48:14,556:     headers={'Authorization': "Token {}".format(token)}
2017-12-03 06:48:14,556:
2017-12-03 06:48:14,556:   File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 111, in post
2017-12-03 06:48:14,557:     return request('post', url, data=data, json=json, **kwargs)
2017-12-03 06:48:14,557:
2017-12-03 06:48:14,557:   File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 57, in request
2017-12-03 06:48:14,557:     return session.request(method=method, url=url, **kwargs)
2017-12-03 06:48:14,557:
2017-12-03 06:48:14,557:   File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 475, in request
2017-12-03 06:48:14,557:     resp = self.send(prep, **send_kwargs)
2017-12-03 06:48:14,558:
2017-12-03 06:48:14,558:   File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 585, in send
2017-12-03 06:48:14,558:     r = adapter.send(request, **kwargs)
2017-12-03 06:48:14,558:
2017-12-03 06:48:14,558:   File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 465, in send
2017-12-03 06:48:14,558:     raise ProxyError(e, request=request)
2017-12-03 06:48:14,559: ***************************************************
2017-12-03 06:48:14,559: If you're seeing an import error and don't know why,
2017-12-03 06:48:14,559: we have a dedicated help page to help you debug:
2017-12-03 06:48:14,559: https://help.pythonanywhere.com/pages/DebuggingImportError/

最佳答案

初始化客户端时,实际上可以使用SDK设置代理:

# coding: utf-8

from .apis import Connect, Request, Build


class Client():   def __init__(self, token=None, language=None, proxy=None):
    for api in [Connect, Request, Build]:
      setattr(self, api.__name__.lower(), api(token=token, language=language, proxy=proxy))


可以在以下URL找到完整文件:https://github.com/RecastAI/SDK-python/blob/master/recastai/client.py

07-26 09:38