如何解决使用 Alexa 从 Raspberry Pi 打开警报时如何使两个 Intent 工作
我开发了一个 Alexa 技能,我可以控制 GPIO 引脚来打开 LED 灯,或者在我的情况下切换我的警报面板来布防系统。
这就是我所做的
(Alexa 开发者帐户) 我创建了两个 Intent
- 报警意图
- 代码意图
然后是两种插槽类型
- 状态
- 代码
AlarmIntent 的示例语句是
alarm {status}
{status} alarm
CodeIntent 的示例语句是
{code} code
code {code}
然后将其添加到各自的插槽类型中。
对于第一部分,AlarmIntent
效果很好。当我说 Alexa 警报时,Alexa 会回应说“欢迎使用您的警报”,然后您可以通过说“警报开启”来回应,然后打开 LED 灯!太棒了!
对于第二部分 CodeIntent
,这就是我陷入困境的地方。
我从 Alexa 开发者帐户(Json 编辑器选项卡)获得的代码
{
"interactionModel": {
"languageModel": {
"invocationName": "alarm","modelConfiguration": {
"fallbackIntentSensitivity": {
"level": "LOW"
}
},"intents": [
{
"name": "AMAZON.CancelIntent","samples": []
},{
"name": "AMAZON.HelpIntent",{
"name": "AMAZON.StopIntent",{
"name": "HelloWorldIntent","slots": [],"samples": [
"hello","how are you","say hi world","say hi","hi","say hello world","say hello"
]
},{
"name": "AMAZON.NavigateHomeIntent",{
"name": "AMAZON.FallbackIntent",{
"name": "AlarmIntent","slots": [
{
"name": "status","type": "STATUS"
}
],"samples": [
"alarm {status}","{status} alarm"
]
},{
"name": "CodeIntent","slots": [
{
"name": "code","type": "CODE"
}
],"samples": [
"{code} code","code {code}"
]
}
],"types": [
{
"name": "STATUS","values": [
{
"id": "on","name": {
"value": "off","synonyms": [
"alarm on","enable","activate","turn on"
]
}
},{
"id": "off","name": {
"value": "on","synonyms": [
"alarm off","deactivate","disable","turn off"
]
}
}
]
},{
"name": "CODE","values": [
{
"id": "codeon","name": {
"value": "codeoff","synonyms": [
"1234"
]
}
},{
"id": "codeoff","name": {
"value": "codeon","synonyms": [
"1234"
]
}
}
]
}
]
}
}
}
import logging
import os
from flask import Flask
from flask_ask import Ask,request,session,question,statement
import RPi.GPIO as GPIO
app = Flask(__name__)
ask = Ask(app,"/")
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
STATUSON = ["alarm on","turn on"] # all values that are defined as synonyms in
type
STATUSOFF = ["alarm off","turn off"]
CODEON = ["1234"]
CODEOFF = ["1234"]
@ask.launch
def launch():
speech_text = 'Welcome to your alarm.'
return question(speech_text).reprompt(speech_text).simple_card(speech_text)
@ask.intent('AlarmIntent',mapping = {'status':'status'})
@ask.launch
def launch():
speech_text = 'Please provide pin.'
return question(speech_text).reprompt(speech_text).simple_card(speech_text)
@ask.intent('CodeIntent',mapping = {'code':'code'})
def Gpio_Intent(code,status,room):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
if status in STATUSON:
if code in CODEON:
GPIO.output(17,GPIO.HIGH)
return statement('Alarm Activated')
elif status in STATUSOFF:
if code in CODEOFF:
GPIO.output(17,GPIO.LOW)
return statement('Alarm deactivated')
else:
return statement('Sorry,this command is not possible.')
@ask.intent('AMAZON.HelpIntent')
def help():
speech_text = 'You can say hello to me!'
return question(speech_text).reprompt(speech_text).simple_card('HelloWorld',speech_text)
@ask.session_ended
def session_ended():
return "{}",200
if __name__ == '__main__':
if 'ASK_VERIFY_REQUESTS' in os.environ:
verify = str(os.environ.get('ASK_VERIFY_REQUESTS','')).lower()
if verify == 'false':
app.config['ASK_VERIFY_REQUESTS'] = False
app.run(debug=True)
所以进入正题。当您说“Alexa 闹钟”时,您会收到欢迎回复,然后您可以说“闹钟开”,闹钟就会打开。但是在闹钟打开之前,Alexa 需要询问一个 PIN 码(在这种情况下,1234
一旦您回复“1234”,闹钟就需要打开。
@ask.intent('AlarmIntent',mapping = {'status':'status'}) --> Saying Alarm On
@ask.launch
def launch():
speech_text = 'Please provide pin.'
return question(speech_text).reprompt(speech_text).simple_card(speech_text) --> Alexa asks for Pin
@ask.intent('CodeIntent',mapping = {'code':'code'}) --> Saying "1234"
##and this is where the magic should happen :-) but its not :-( ##
def Gpio_Intent(code,this command is not possible.')
我得到的唯一回应是……没什么。 :-)
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。