Alexa Skill Development using flask-ask and ngrok

烈酒焚心 提交于 2019-12-04 19:17:34

问题


I'm trying to begin developing a skill for alexa using flask-ask and ngrok in python. Following is my code:

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)

The code runs fine on my machine and returns the correct output if I print it out. But the skill gives a HTTP 500 internal error when I deploy it on amazon using ngrok. I get the same 500 internal error both in the text as well as json simulator in the development console.

This is my intent schema:

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

I get the following error in my python prompt: AttributeError: module 'lib' has no attribute 'X509V3_EXT_get

The stacktrace is as follows:

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 output:

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

I've tried running it on both python 2.7 and python 3.6. Any help is appreciated


回答1:


Ran into the same issue, you can fix it by downgrading cryptography to anything less than 2.2 for me.

pip install 'cryptography<2.2'

rpg711 gets all the credit (see comments the on original post)




回答2:


I can confirm that this works with cryptography 2.1.4, not with 2.5 or 2.6 on Python 3.7 and Mac OS High Sierra. However on Mac OS there are other issues to get over first.

What I found is that installing crypotgraphy 2.1.4 ends with an error (as below). I hit this error at the very start of my flask-ask project and had to do a manual install of requirements before I started coding. When I finally started trying alexa, I got the same 500 error (as above) with cryptography 2.5 or 2.6. So reading that it has to be 2.1.4 I always got this error when trying to install that specific version:

    #include <openssl/opensslv.h>
             ^~~~~~~~~~~~~~~~~~~~
    1 error generated.
    error: command 'clang' failed with exit status 1

Having tried many things I tried the specific recommendation in this post (https://github.com/pyca/cryptography/issues/3489). Trying the export CPPFLAGS and LDFLAGS didn't seem to work, however the following did

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"

I am afraid I cannot say whether the things I tried before i.e. brew link openssl and setting the CPPFLAGS and LDFLAGS had an impact on the final result. I did not however do an update of openssl as in the post. I hope that helps, but I was not operating from a position of knowledge and was not sure whether I had the skills do a manual install of opsenssl as indicated further in the post.

I hope this helps as I had almost given up.

BTW: using ngrok's web interface/inspector I found really handy, i.e. being able to replay the amazon request time and time again was very, very handy for me as I made other errors before the cryptography issue.




回答3:


Referring to this link helped me solve the problem. https://github.com/pyca/cryptography/issues/3489

Basically, by linking openssl in mac by $ brew link openssland installing cryptography==2.1.4, the problem was resolved.



来源:https://stackoverflow.com/questions/49375054/alexa-skill-development-using-flask-ask-and-ngrok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!