Now that you know how to make the Amazon Alexa voice assistant communicate with our PC through a web
server provided by Ngrok.
Let's move on to developing the interface between the Voice Assistant and Ros.
Two.
We are going to create the logic that interprets the commands received from the voice assistant and
convert them into Ros.
Two messages that will be sent to the task server.
To develop this component, we will use the documentation, the libraries and the example code provided
by Amazon for the Voice assistant skilled developer.
Unfortunately, though, these Alexa Voice assistant APIs are not available for C plus plus.
Therefore, in this lesson we are going to implement the logic only in Python, unlike what we did in
the course so far.
There won't be an equivalent lesson for developing these functionalities in C plus plus.
However, if you are not familiar with Python or you are not comfortable with this programming language,
don't worry.
Most of the code that we are going to implement in this lesson will be copy pasted from the documentation
examples that are available in the Amazon Developer skill console.
So instead of focusing on the language syntax, I recommend you paying attention to the logic that is
used in order to add new features and new functionalities to this skill and also the logic that is used
to receive and to interpret these voice messages.
So let's start by creating a new Python script within the Arduino bot remote package.
And so by convention we need to insert the new Python scripts within the new folder called Arduino Bot
Remote.
And within this folder, let's start by creating a new file called Init Dot Pi.
And let's leave this file empty.
And now let's create another file still in the Arduino bot remote package and in the Arduino bot remote
folder.
And let's call this one Alexa interface dot pi.
In this script, let's start by declaring the python interpreter directory.
So this is in the user bin.
And the environment that we want to use is the Python three.
Next, we are going to create a web server using the python flask library so we can start opening the
documentation of the Alexa developer.
So the Alexa developer documentation.
And here let's go to the section skill development process and let's click on tools to create and manage
skills.
And then within this one, let's click on Ask SDK is.
And here we can see all the libraries that are available for different programming languages.
So let's open the one that is available for Python here.
Let's open the guide on how to host a skill as a web service using flask.
So this one host a custom skill as a web service.
So this is a very nice tutorial.
And let's scroll down to this section in which flask is used.
So here, flask ask.
And here we can see that there are several alternative Python libraries that are available for hosting
the skill.
But we are going to use this one.
So the flask ask package that we installed in the setup section of this course.
So now we can copy all of this contents, so we can copy this example code and we can paste it within
our script.
So here let's paste it.
And basically with this code, we are creating a web service using flask.
So using this library here and also we are initializing the communication with the Alexa skill using
two components from the Alexa Ask SDK library that are the skill builder and also the skill adapter.
Now, in order to start the Flask web service, we need to define the main function.
So here we need to define the main function.
And within this function we can execute the flask web server by using app dot run.
So app will contain the implementation of the Flask web server.
And here we are starting it.
Then we also need to register this Flask application within the Alexa skill adapter.
So within this object here, so let's use the instruction skill adapter register and let's pass the
application which contains the web server is the implementation.
So this one so is the implementation of the Flask class.
And also let's define the route of the web service.
So the route with the forward slash.
Now with this, we have created our web service and also we have interfaced it with the Alexa skill
adapter and also with the Skill Builder class.
And now we can add all these skills functionalities and so we can add all the actions that this script
will take.
When a specific intent is activated, we can find this information still in the Alexa documentation.
And so let's go to developing your first skill.
So let's go to this tutorial here.
And let's go to the section in which the first intent is executed.
So here let's go to the launch request handler, which is the launch intent.
And this is automatically called when we start the interaction with Alexa.
So when we say the phrase Alexa, activate the robot, so now we can just copy the code for the launch
request handler and we can paste it within our script.
So let's paste it here Before the creation of the Skill Builder, let's move all the import statement
at the beginning of this script and now we can adapt the code that we just copied for our robot.
So in order to define which action should the robot take when the voice assistant is activated?
So basically when this launch request is activated.
Let's change the speech text.
So let's change the response of the assistant.
So this one.
So this is the response that the assistant should provide when the launch request is activated.
And let's change this one to I.
How can I help?
So basically, this means that whenever we say the phrase Alexa activate the robot, the assistant will
answer us with this question so I can help.
And actually, this is all we need in order to handle the launch request when we receive it from the
Amazon.
Alexa.
Now we need to register this class.
So the launch request handler class inside the skill builder class.
So inside this class here, in fact, as we can see from the comment that we have copied, we need to
register your intent handler within the skill builder object and we can do so by using the skill builder
and by using the function add request handler.
And to this one we can pass the instance of the launch request handler that for the moment is the only
handler that we have created.
And so now we can save this file here.
And now we can already test this functionality by activating the skill with the phrase Alexa, activate
the robot, and in order for this request handler to work, we expect the Alexa assistant to return
this response.
So to say this phrase here, I.
How can I help?
We are missing one thing.
So as you can see here in the skill ID, we need to set the ID of our skill that we have just created.
And in order to take this ID, we need to go back into the Amazon Alexa Developer console.
And here, as you can see in the main page of our skill, we have the small button copy skill ID.
So let's take this one and let's paste it here within the skill ID.
So let's use the double quotes.
And let's paste the SQL ID in this way.
Now we have completed the connection between the Alexa console and this Python script.
And now we can actually run this script in order to test its functionalities.
So in order to do so, let's take the Alexa console and put it on one side.
Then let's open a new terminal and let's go into the workspace, then into the source folder and into
the Arduino bot remote package.
Then let's go into the Arduino bot remote subfolder.
And so here we have created our Alexa interface.py file and let's make this file an executable with
the command C h mod plus X and then the name of the script.
So Alexa interface.
Now if we type ls, we can see that actually the Alexa interface dot py script is an executable and
so we can execute it.
We can run it as a regular python script.
So this is not yet a Ros two node and we can start it as a regular python script.
So with the command python followed by the name of the script that is the Alex interface.
So let's press enter.
This is Python three since we are using Python three.
And now we can see that the Flask application is online.
So it is online and it is available on the port 5000 of our PC.
Now, let's also start Ngrok, because this just started the web server that listens for the new messages
and interprets the new messages.
Now let's also start andyrock that instead will make available a web service on our PC.
So let's go to the download folder in my case, and then to the Ngrok folder.
And here, in order to execute Ngrok, let's execute the file called Ngrok.
And then we want to open an Http server and we want to open this one on the Port 5000 or on our PC.
So let's press enter.
And since we open that new instance of Ngrok and since we are using the free version of this software,
it provides us with a new address to access our PC from the Internet.
So as you can see, this address here is different from the one that we set previously, and so we need
to change it also within the Arduino bot skill.
And so within the end point here, as you can see, these two address are different since we are using
the Ngrok free version.
So every time we need to change this one and we need again to save the endpoint.
Now everything is ready, so we can go back to the test section.
So here and here we can start the interaction with our web server and also here with our Flask application
that will receive the messages and will interpret them and also will provide the response that the Alexa
assistant will pronounce.
So let's activate this skill.
Activate robot.
Hi, How can I help?
And now we can see that the voice assistant answered with the expected phrase.
So the one that we set here, I.
How can I help?
And so we can see that the Rock was able to receive the message.
This time the request was accepted and was processed correctly.
Since we have the Flask server here that was able to interpret the request and also to send a response
to the server.
And so here in the Alexa we can see now both the messages, so we can see the Json message that Alexa
sent to our web service and here we can see the response that our web service returned to Alexa.
And also here we can see the phrase that we expect that we want Alexa to pronounce.
Now all that remains is to add the endling for the remaining requests inside our script in order to
complete the voice interaction model with the assistant.
Now all that remains is to handle all the remaining requests.
So all the remaining intents that Alexa can send to our web application inside the script that we have
just created in order to complete the voice interaction model with the assistant.
And then also for each of these requests, we will enable an interface with Roscoe and with the task
server that we created in order to move the robot.