我正在尝试为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 并且使该工作顺利进行。我将在下面发布我的代码以及运行它后得到的响应。我具有默认位置设置的方式是否正确,可以处理来自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
答案 0 :(得分:0)
您应该尝试使用from flask_ask import Ask, statement, question