【问题标题】:Start an Alexa Interaction from the Backend从后端启动 Alexa 交互
【发布时间】:2020-10-13 18:56:34
【问题描述】:

我正在编写我的第一个 Alexa 技能,目的是通过提供语音输入与机器人进行交互,并允许机器人通过 Alexa 界面发回消息。

我正在使用 Flask-Ask Python 扩展编写本地后端,使用 ngrok 建立 http 通信和 ROS 与机器人通信。

我创建了启动和停止意图,以及我要求 Alexa 移动机器人的个人意图。

我现在需要做的是创建一个函数,允许我使用 alexa speak 输出来传达消息,而无需用户交互;例如,当触发自定义if 条件时,我希望 Alexa 播放示例消息:

if condition == True:
    def advise_function():
        sample_message = 'Messaggio di avviso'
        return statement(sample_message)

这是实际代码(供参考):

#!/usr/bin/env python
import os
import rospy
import threading
import requests
import time

from flask import Flask
from flask_ask import Ask, question, statement, session
from std_msgs.msg import String, Float64


#---------------------------------------------- FLASK & ROS INIZIALIZATION ---------------------------------------------#

app = Flask(__name__)
ask = Ask(app, "/")

# ROS node, publisher, and parameter. The node is started in a separate thread to avoid conflicts with Flask.
# The parameter *disable_signals* must be set if node is not initialized in the main thread.
threading.Thread(target=lambda: rospy.init_node('alexa_ros_node', disable_signals=True)).start()
direction_publisher = rospy.Publisher('alexa/direction', String, queue_size=100)
length_publisher = rospy.Publisher('alexa/lenght', Float64, queue_size=100)
NGROK = rospy.get_param('/ngrok', None)


#------------------------------------------------------- INTENTS -------------------------------------------------------#

# LaunchIntent -> Avvio dell'applicazione: "Apri nodo ros"
@ask.launch
def launch():
    welcome_message = 'Benvenuto nel nodo di controllo ROS. Come posso aiutarti?'
    return question(welcome_message)

# MuoviRobotIntent: "Muovi il robot"
@ask.intent('MuoviRobotIntent', default={'direzione':None, 'lunghezza':None})
def move_robot_function(direzione, lunghezza):
    direction_publisher.publish(direzione)
    length_publisher.publish(float(lunghezza))
    return statement('Ho pubblicato direzione e lunghezza sul topic ROS: {0} di {1} metri.'.format(direzione, lunghezza))


# StopIntent -> Uscita dall'applicazione: "Stop / Esci..."
@ask.session_ended
def session_ended():
    return "{}", 200

# ngrok app running
if __name__ == '__main__':
    if NGROK:
        print 'NGROK mode'
        app.run(host=os.environ['ROS_IP'], port=5000)
    else:
        print 'Manual tunneling mode'
        dirpath = os.path.dirname(__file__)
        cert_file = os.path.join(dirpath, '../config/ssl_keys/certificate.pem')
        pkey_file = os.path.join(dirpath, '../config/ssl_keys/private-key.pem')
        app.run(host=os.environ['ROS_IP'], port=5000,
                ssl_context=(cert_file, pkey_file))

【问题讨论】:

    标签: flask alexa alexa-skills-kit alexa-app flask-ask


    【解决方案1】:

    查看主动事件 API。我相信它应该涵盖您的用例。 https://developer.amazon.com/en-US/docs/alexa/smapi/proactive-events-api.html

    【讨论】:

    • 好的,那么就没有办法把alexa当做音箱了?例如:“speech(speech something)”因为我是 Python 和 Alexa (JSON) 编程的新手,所以我很难实现 Proactive Events API。
    • 在 Alexa Skills Kit 中,“speech(speech something)”必须响应客户发起的事件。您可以将机器人设置为音频流媒体源,让机器人播放音乐或一些声音,然后在其上叠加警报,然后从技能启动该流媒体源。或者您可以通过蓝牙连接到回声设备并将其用作蓝牙扬声器来播放手机或笔记本电脑中的音频。取决于为您的机器人提供动力的动力。如果是 Raspberry Pi,您可以从中流式传输或将蓝牙扬声器连接到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2016-03-28
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    相关资源
    最近更新 更多