使用 Alexa 从 Raspberry Pi 打开警报时如何使两个 Intent 工作

如何解决使用 Alexa 从 Raspberry Pi 打开警报时如何使两个 Intent 工作

我开发了一个 Alexa 技能,我可以控制 GPIO 引脚来打开 LED 灯,或者在我的情况下切换我的警报面板来布防系统。

这就是我所做的

(Alexa 开发者帐户) 我创建了两个 Intent

  1. 报警意图
  2. 代码意图

然后是两种插槽类型

  1. 状态
  2. 代码

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"
                            ]
                        }
                    }
                ]
            }
        ]
    }
}
}

然后在 RaspBerry Pi Python 文件上:

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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?