0

I am writing my first Alexa's Skill with the aim of interacting with a robot by providing voice input and also allowing the robot to send back messages through the Alexa interface.

I'm using the Flask-Ask Python extension to write a local backend, using ngrok to establish the http communication and ROS to communicate with the robot.

I create both launch and stop intents and also a personal one in which I ask Alexa to move the robot.

What I need to do now is create a function that allows me to communicate a message using the alexa speak output, without user interaction; For example when a custom if condition is triggered, I want Alexa to play a sample message:

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

This is the actual code (for reference):

#!/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))
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

Check out the Proactive Events API. I believe it should cover your use case. https://developer.amazon.com/en-US/docs/alexa/smapi/proactive-events-api.html

LetMyPeopleCode
  • 1,895
  • 15
  • 20
  • Okay, so there is no method to use alexa as a speaker ? For example: "speech(speech something)" Cause I'm new to programming in Python and Alexa (JSON) so I'm having difficulty implementing Proactive Events API. – Davide Ferrari Jun 28 '20 at 16:16
  • In the Alexa Skills Kit "speech(speech something)" has to be in *response* to a customer-initiated event. You can set your robot up as an audio streaming source, have the robot stream music or some sound and then overlay alerts on it, then launch that streaming source from a skill. Or you can connect to an echo device via bluetooth and use it as a bluetooth speaker to play audio from your phone or laptop. Depends on what's powering your robot. If it's a Raspberry Pi, you could stream from it or connect a bluetooth speaker to it. – LetMyPeopleCode Jun 29 '20 at 02:02