Flask-Ask 自定义 Alexa 技能 Python

Flask-Ask Custom Alexa Skill In Python

我正在尝试为 Amazon Alexa 制作自定义技能并且我已经制作了该技能,使用 Flask-Ask 制作了我的 python 程序并尝试 运行 它使用 ngrok 作为端点为了技能。我已成功遵循此处的指南: https://developer.amazon.com/blogs/post/Tx14R0IYYGH3SKT/Flask-Ask-A-New-Python-Framework-for-Rapid-Alexa-Skills-Kit-Development 并使这项工作没有任何问题。我将在下面 post 我的代码以及 运行 之后我得到的响应。我的 def position 设置是否正确以处理来自 Alexa Skill 的输入?

这是我制作的包含 Flask-Ask 的代码:

#Documentation links for Flask Ask Alexa
#https://developer.amazon.com/blogs/post/Tx14R0IYYGH3SKT/Flask-Ask-A-New-Python-Framework-for-Rapid-Alexa-Skills-Kit-Development 
#http://flask-ask.readthedocs.io/en/latest/

#Setup:
#alsamixer usb pnp, adjust capture
#sudo raspi-config choose 3.5mm audio output

#Flask Application Template
from flask import Flask, render_template
from flask_ask import Ask, statement
from time import sleep #Sleep Delay 
import RPi.GPIO as GPIO
GPIO.setwarnings(False)

#Stepper motor GPIO Pins
DIR = 20   # Direction GPIO Pin
STEP = 21  # Step GPIO Pin
CW = 1     # Clockwise Rotation
CCW = 0    # Counterclockwise Rotation

#Set mode and pins as input/output
GPIO.setmode(GPIO.BCM)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.output(DIR, CW)

#Motor Variables and function
delay = .009 #Motor rotational speed 
previous_position = 0 #Varaible for previous position
total_step_count = 4980 #Time motor stays running; 830 per position

#Step count per position
pos1 = 0
pos2 = 830
pos3 = 1660
pos4 = 2490
pos5 = 3320
pos6 = 4150
pos7 = 4980

#Flask Application Template Continued
app = Flask(__name__)
ask = Ask(app, "/")

def turn_motor():
    for x in range(step_count):
            GPIO.output(STEP, GPIO.HIGH)
            sleep(delay)
            GPIO.output(STEP, GPIO.LOW)
            sleep(delay)

#Ask Alexa to open My Blinds, states welcome message
@ask.launch
def start_skill():
    welcome_message = "What position would you like to set your blinds?"
    return question(welcome_message)

#Position intent, this is our answer to the welcome message
@ask.intent('PositionIntent', convert ={'one': int, 'two': int, 'three': int,
                                        'four': int, 'five': int, 'six': int,
                                        'seven': int})

def position():

    if [1]:
            if (1 < previous_position):  
                step_count = abs(pos1 - previous_position)
                turn_motor()
                previous_position = pos1
            else:
                GPIO.output(DIR, CCW)
                step_count = abs(pos1 - previous_position)
                turn_motor()
                previous_position = pos1
            GPIO.cleanup()

    elif [2]:
            if (2 < previous_position):
                turn_motor()
                step_count = abs(pos2 - previous_position)
                previous_position = pos2
            else:
                GPIO.output(DIR, CCW)
                turn_motor()
                step_count = abs(pos2 - previous_position)
                previous_position = pos2
            GPIO.cleanup()

    elif [3]:
            if (3 < previous_position):
                turn_motor()
                step_count = abs(pos3 - previous_position)
                previous_position = pos3
            else:
                GPIO.output(DIR, CCW)
                turn_motor()
                step_count = abs(pos2 - previous_position)
                previous_position = pos3
            GPIO.cleanup()
    elif [4]:
            if (4 < previous_position):
                turn_motor()
                step_count = abs(pos4 - previous_position)
                previous_position = pos4
            else:
                GPIO.output(DIR, CCW)
                turn_motor()
                step_count = abs(pos4 - previous_position)
                previous_position = pos4
            GPIO.cleanup()

    elif [5]:
            if (5 < previous_position):
                turn_motor()
                step_count = abs(pos5 - previous_position)
                previous_position = pos5
            else:
                GPIO.output(DIR, CCW)
                turn_motor()
                step_count = abs(pos5 - previous_position)
                previous_position = pos5
            GPIO.cleanup()

    elif [6]:
            if (6 < previous_position):
                turn_motor()
                step_count = abs(pos6 - previous_position)
                previous_position = pos6
            else:
                GPIO.output(DIR, CCW)
                turn_motor()
                step_count = abs(pos6 - previous_position)
                previous_position = pos6
            GPIO.cleanup()

    elif [7]:
            if (7 < previous_position):
                turn_motor()
                step_count = abs(pos7 - previous_position)
                previous_position = pos7
            else:
                GPIO.output(DIR, CCW)
                turn_motor()
                step_count = abs(pos7 - previous_position)
                previous_position = pos7
            GPIO.cleanup()

    else:
        msg = "Position not valid, choose a position between 1 and 7"
        return statement(msg)

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

这是我在要求 Alexa 启动我的自定义技能后得到的回复:

127.0.0.1 - - [24/Jun/2018 14:24:21] "POST / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python2.7/dist-packages/flask_ask/core.py", line 760, in _flask_view_func
    result = self._launch_view_func()
  File "/home/pi/Alexa_Skill_Code.py", line 57, in start_skill
    return question(welcome_message)
NameError: global name 'question' is not defined
127.0.0.1 - - [24/Jun/2018 14:24:21] "POST / HTTP/1.1" 200 -

这是我从 Ngrok 得到的回复:

POST/      200 OK
POST/      500 INTERNAL SERVER ERROR

你应该尝试使用 from flask_ask import Ask, statement, question