使用flask-ask和ngrok开发Alexa技能

11

我正在尝试使用Python中的flask-ask和ngrok开发与Alexa相关的技能。以下是我的代码:

from flask import Flask
from flask_ask import Ask, statement, question, session
import json
import requests
import time
import unidecode

app = Flask(__name__)
ask = Ask(app, "/reddit_reader")

def get_headlines():

    titles = 'is this working'
    return titles  

@app.route('/')
def homepage():
    return "hi there, how ya doin?"

@ask.launch
def start_skill():
    welcome_message = 'Hello there, would you like the news?'
    return question(welcome_message)

@ask.intent("YesIntent")
def share_headlines():
    headlines = get_headlines()
    headline_msg = 'The current world news headlines are 
{}'.format(headlines)
    return statement(headline_msg)

@ask.intent("NoIntent")
def no_intent():
    bye_text = 'I am not sure why you asked me to run then, but okay... bye'
    return statement(bye_text)

if __name__ == '__main__':
    app.run(debug=True)

代码在我的电脑上运行良好,如果我将其打印出来,它会返回正确的输出。但是当我使用ngrok在亚马逊上部署时,该技能会产生HTTP 500内部错误。在开发控制台中的文本和JSON模拟器中,我都得到了相同的500个内部错误。

这是我的意图模式:

{
  "intents": [
    {
      "intent": "YesIntent"
    },
    {
      "intent": "NoIntent"
    }
  ]
}

我在Python提示符中遇到以下错误:

AttributeError: 模块“lib”没有属性“X509V3_EXT_get”

堆栈跟踪如下:

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python36\lib\site-packages\flask_ask\core.py", line 728, in _flask_view_func
    ask_payload = self._alexa_request(verify=self.ask_verify_requests)
  File "C:\Python36\lib\site-packages\flask_ask\core.py", line 662, in _alexa_request
    cert = verifier.load_certificate(cert_url)
  File "C:\Python36\lib\site-packages\flask_ask\verifier.py", line 21, in load_certificate
    if not _valid_certificate(cert):
  File "C:\Python36\lib\site-packages\flask_ask\verifier.py", line 63, in _valid_certificate
    value = str(extension)
  File "C:\Python36\lib\site-packages\OpenSSL\crypto.py", line 779, in __str__
    return self._subjectAltNameString()
  File "C:\Python36\lib\site-packages\OpenSSL\crypto.py", line 740, in _subjectAltNameString
    method = _lib.X509V3_EXT_get(self._extension)
AttributeError: module 'lib' has no attribute 'X509V3_EXT_get'

Pip freeze输出:

aniso8601==1.2.0
asn1crypto==0.24.0
certifi==2018.1.18
cffi==1.11.5
chardet==3.0.4
click==6.7
cryptography==2.2
Flask==0.12.1
Flask-Ask==0.9.8
idna==2.6
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pycparser==2.18
pyOpenSSL==17.0.0
python-dateutil==2.7.0
PyYAML==3.12
requests==2.18.4
six==1.11.0
Unidecode==1.0.22
urllib3==1.22
Werkzeug==0.14.1

我已尝试在Python 2.7和Python 3.6上运行它。 感谢任何帮助。


堆栈跟踪是什么? - TTT
added it to the question - RogerThat
你能执行 pip freeze 命令并将其输出复制粘贴吗? - TTT
添加了 pip freeze 输出 - RogerThat
5
尝试卸载并将cryptography降级至版本1.7,看看是否可以解决这个问题。pyOpenSSL要求至少使用1.7版本,新版的cryptography模块中可能删除了旧版v17.0.0 pyOpenSSL所依赖的一些内容。 - TTT
成功了!我降级到了2.0(尝试了1.7,遇到了一些Visual C++错误)。非常感谢! - RogerThat
4个回答

25

我遇到了相同的问题,你可以通过将cryptography降级到低于2.2来解决它。

pip install 'cryptography<2.2'

rpg711获得了所有的功劳(请参见原帖中的评论)


voglster 是正确的,这个方法也解决了我的问题。顺便说一下,目前最新可用的版本是 2.1.4。 - Dan Heaford
这只适用于Python 2吗?我安装了cryptography 2.1.4、1.7,但仍然出现错误。 - Phillip Y.
1
在我的电脑上运行正常(使用 Python 3.6 和 cryptography 2.1.4)。 - Stvad

1
我可以确认这适用于cryptography 2.1.4,但不适用于Python 3.7和Mac OS High Sierra的2.5或2.6版本。然而,在Mac OS上,首先需要解决其他问题。
我发现安装cryptography 2.1.4会出现错误(如下所示)。在我的flask-ask项目的开始处,我遇到了这个错误,并且必须在开始编码之前手动安装要求。当我最终开始尝试alexa时,使用cryptography 2.5或2.6时我得到了相同的500错误(如上所述)。因此,阅读它必须是2.1.4,我总是在尝试安装该特定版本时遇到此错误:
    #include <openssl/opensslv.h>
             ^~~~~~~~~~~~~~~~~~~~
    1 error generated.
    error: command 'clang' failed with exit status 1

尝试了许多方法后,我尝试了此帖子中的具体建议 (https://github.com/pyca/cryptography/issues/3489)。尝试导出 CPPFLAGS 和 LDFLAGS 似乎没有起作用,但以下操作成功了: pip3 install cryptography==2.1.4 --global-option=build_ext --global-option="-L/usr/local/opt/openssl/lib" --global-option="-I/usr/local/opt/openssl/include" 很抱歉,我无法确定之前尝试的方法,例如使用 brew 链接 openssl 和设置 CPPFLAGS 和 LDFLAGS 是否对最终结果产生影响。然而,我没有像帖子中那样更新 openssl。我希望这可以帮到您,但我并不是从知识的角度出发,并不确定我是否有能力按照帖子中进一步指示手动安装 opsenssl。
顺便说一句:使用 ngrok 的 Web 接口 / 检查器非常方便,即能够反复重放 Amazon 请求对我非常有用,因为在解决密码问题之前,我犯了其他错误。

0

0
在Debian Linux上,尝试使用命令行降级Cryptography模块。
pip install 'cryptography<2.2'

这导致无法卸载旧模块的错误(我想我拥有版本2.6.1)。 我通过删除/usr/lib/python3/dist-packages中的密码术文件夹以及*-.egg文件来手动卸载它。

然后,当我尝试安装较旧的密码术模块时,我又遇到了一个“无法构建轮子”的错误,因为我缺少一些头文件。根据 cryptography module docs ,一旦我运行,

sudo apt-get install build-essential libssl-dev libffi-dev python3-dev

我最终成功安装了密码学模块版本2.1.4,我的Alexa技能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接