WEBVTT

00:00.140 --> 00:05.960
Now that you know how to make the Amazon Alexa voice assistant communicate with our PC through a web

00:05.960 --> 00:08.060
server provided by Ngrok.

00:08.300 --> 00:12.920
Let's move on to developing the interface between the Voice Assistant and Ros.

00:12.970 --> 00:13.250
Two.

00:13.790 --> 00:19.760
We are going to create the logic that interprets the commands received from the voice assistant and

00:19.760 --> 00:21.200
convert them into Ros.

00:21.200 --> 00:24.500
Two messages that will be sent to the task server.

00:25.260 --> 00:31.140
To develop this component, we will use the documentation, the libraries and the example code provided

00:31.140 --> 00:34.710
by Amazon for the Voice assistant skilled developer.

00:35.220 --> 00:41.230
Unfortunately, though, these Alexa Voice assistant APIs are not available for C plus plus.

00:41.310 --> 00:47.580
Therefore, in this lesson we are going to implement the logic only in Python, unlike what we did in

00:47.580 --> 00:48.720
the course so far.

00:48.750 --> 00:53.640
There won't be an equivalent lesson for developing these functionalities in C plus plus.

00:54.090 --> 00:58.860
However, if you are not familiar with Python or you are not comfortable with this programming language,

00:58.860 --> 00:59.730
don't worry.

00:59.760 --> 01:05.220
Most of the code that we are going to implement in this lesson will be copy pasted from the documentation

01:05.220 --> 01:09.930
examples that are available in the Amazon Developer skill console.

01:10.290 --> 01:16.230
So instead of focusing on the language syntax, I recommend you paying attention to the logic that is

01:16.230 --> 01:22.230
used in order to add new features and new functionalities to this skill and also the logic that is used

01:22.230 --> 01:25.960
to receive and to interpret these voice messages.

01:26.500 --> 01:32.250
So let's start by creating a new Python script within the Arduino bot remote package.

01:32.260 --> 01:40.600
And so by convention we need to insert the new Python scripts within the new folder called Arduino Bot

01:42.370 --> 01:43.180
Remote.

01:43.180 --> 01:50.080
And within this folder, let's start by creating a new file called Init Dot Pi.

01:50.620 --> 01:52.780
And let's leave this file empty.

01:52.900 --> 01:58.930
And now let's create another file still in the Arduino bot remote package and in the Arduino bot remote

01:58.930 --> 01:59.530
folder.

01:59.530 --> 02:05.440
And let's call this one Alexa interface dot pi.

02:05.830 --> 02:10.630
In this script, let's start by declaring the python interpreter directory.

02:10.930 --> 02:15.280
So this is in the user bin.

02:16.180 --> 02:19.990
And the environment that we want to use is the Python three.

02:20.770 --> 02:27.820
Next, we are going to create a web server using the python flask library so we can start opening the

02:27.820 --> 02:30.580
documentation of the Alexa developer.

02:30.580 --> 02:33.040
So the Alexa developer documentation.

02:33.130 --> 02:43.000
And here let's go to the section skill development process and let's click on tools to create and manage

02:43.000 --> 02:43.810
skills.

02:45.770 --> 02:50.100
And then within this one, let's click on Ask SDK is.

02:50.790 --> 02:55.700
And here we can see all the libraries that are available for different programming languages.

02:55.710 --> 03:00.060
So let's open the one that is available for Python here.

03:00.060 --> 03:05.400
Let's open the guide on how to host a skill as a web service using flask.

03:05.490 --> 03:09.690
So this one host a custom skill as a web service.

03:10.170 --> 03:12.450
So this is a very nice tutorial.

03:12.450 --> 03:16.930
And let's scroll down to this section in which flask is used.

03:16.950 --> 03:19.170
So here, flask ask.

03:19.440 --> 03:24.870
And here we can see that there are several alternative Python libraries that are available for hosting

03:24.870 --> 03:25.380
the skill.

03:25.380 --> 03:27.330
But we are going to use this one.

03:27.330 --> 03:32.940
So the flask ask package that we installed in the setup section of this course.

03:33.330 --> 03:42.030
So now we can copy all of this contents, so we can copy this example code and we can paste it within

03:42.030 --> 03:42.960
our script.

03:42.990 --> 03:45.030
So here let's paste it.

03:45.850 --> 03:50.050
And basically with this code, we are creating a web service using flask.

03:50.050 --> 03:56.710
So using this library here and also we are initializing the communication with the Alexa skill using

03:56.740 --> 04:04.510
two components from the Alexa Ask SDK library that are the skill builder and also the skill adapter.

04:04.810 --> 04:10.330
Now, in order to start the Flask web service, we need to define the main function.

04:10.330 --> 04:14.680
So here we need to define the main function.

04:19.240 --> 04:27.790
And within this function we can execute the flask web server by using app dot run.

04:27.790 --> 04:32.950
So app will contain the implementation of the Flask web server.

04:32.950 --> 04:34.600
And here we are starting it.

04:35.570 --> 04:41.610
Then we also need to register this Flask application within the Alexa skill adapter.

04:41.630 --> 04:54.860
So within this object here, so let's use the instruction skill adapter register and let's pass the

04:54.860 --> 04:59.180
application which contains the web server is the implementation.

04:59.720 --> 05:03.500
So this one so is the implementation of the Flask class.

05:03.860 --> 05:06.650
And also let's define the route of the web service.

05:07.460 --> 05:11.210
So the route with the forward slash.

05:11.990 --> 05:18.320
Now with this, we have created our web service and also we have interfaced it with the Alexa skill

05:18.320 --> 05:21.410
adapter and also with the Skill Builder class.

05:21.530 --> 05:27.710
And now we can add all these skills functionalities and so we can add all the actions that this script

05:27.710 --> 05:28.280
will take.

05:28.280 --> 05:35.540
When a specific intent is activated, we can find this information still in the Alexa documentation.

05:35.750 --> 05:39.200
And so let's go to developing your first skill.

05:39.200 --> 05:41.390
So let's go to this tutorial here.

05:42.000 --> 05:46.990
And let's go to the section in which the first intent is executed.

05:47.010 --> 05:52.170
So here let's go to the launch request handler, which is the launch intent.

05:52.170 --> 05:55.980
And this is automatically called when we start the interaction with Alexa.

05:55.980 --> 06:04.650
So when we say the phrase Alexa, activate the robot, so now we can just copy the code for the launch

06:05.130 --> 06:09.690
request handler and we can paste it within our script.

06:09.690 --> 06:17.700
So let's paste it here Before the creation of the Skill Builder, let's move all the import statement

06:18.720 --> 06:26.070
at the beginning of this script and now we can adapt the code that we just copied for our robot.

06:26.190 --> 06:32.220
So in order to define which action should the robot take when the voice assistant is activated?

06:32.220 --> 06:36.330
So basically when this launch request is activated.

06:37.200 --> 06:39.420
Let's change the speech text.

06:39.420 --> 06:42.960
So let's change the response of the assistant.

06:43.110 --> 06:44.550
So this one.

06:45.000 --> 06:51.480
So this is the response that the assistant should provide when the launch request is activated.

06:51.600 --> 06:54.420
And let's change this one to I.

06:57.180 --> 06:58.170
How can I help?

06:58.350 --> 07:05.670
So basically, this means that whenever we say the phrase Alexa activate the robot, the assistant will

07:05.670 --> 07:09.510
answer us with this question so I can help.

07:09.930 --> 07:15.900
And actually, this is all we need in order to handle the launch request when we receive it from the

07:15.900 --> 07:16.290
Amazon.

07:16.290 --> 07:16.710
Alexa.

07:17.310 --> 07:19.560
Now we need to register this class.

07:19.560 --> 07:24.450
So the launch request handler class inside the skill builder class.

07:24.450 --> 07:31.050
So inside this class here, in fact, as we can see from the comment that we have copied, we need to

07:31.050 --> 07:39.840
register your intent handler within the skill builder object and we can do so by using the skill builder

07:39.840 --> 07:46.260
and by using the function add request handler.

07:46.620 --> 07:53.490
And to this one we can pass the instance of the launch request handler that for the moment is the only

07:53.490 --> 07:55.320
handler that we have created.

07:55.680 --> 07:58.470
And so now we can save this file here.

07:58.470 --> 08:04.800
And now we can already test this functionality by activating the skill with the phrase Alexa, activate

08:04.800 --> 08:11.520
the robot, and in order for this request handler to work, we expect the Alexa assistant to return

08:11.520 --> 08:12.600
this response.

08:12.600 --> 08:15.240
So to say this phrase here, I.

08:15.240 --> 08:16.200
How can I help?

08:16.650 --> 08:18.230
We are missing one thing.

08:18.240 --> 08:25.770
So as you can see here in the skill ID, we need to set the ID of our skill that we have just created.

08:25.950 --> 08:32.130
And in order to take this ID, we need to go back into the Amazon Alexa Developer console.

08:32.340 --> 08:39.040
And here, as you can see in the main page of our skill, we have the small button copy skill ID.

08:39.370 --> 08:44.860
So let's take this one and let's paste it here within the skill ID.

08:45.340 --> 08:47.320
So let's use the double quotes.

08:48.410 --> 08:51.320
And let's paste the SQL ID in this way.

08:51.350 --> 08:57.320
Now we have completed the connection between the Alexa console and this Python script.

08:57.470 --> 09:02.630
And now we can actually run this script in order to test its functionalities.

09:02.840 --> 09:09.080
So in order to do so, let's take the Alexa console and put it on one side.

09:09.230 --> 09:17.510
Then let's open a new terminal and let's go into the workspace, then into the source folder and into

09:17.510 --> 09:20.810
the Arduino bot remote package.

09:21.440 --> 09:27.200
Then let's go into the Arduino bot remote subfolder.

09:27.200 --> 09:34.250
And so here we have created our Alexa interface.py file and let's make this file an executable with

09:34.250 --> 09:41.360
the command C h mod plus X and then the name of the script.

09:41.360 --> 09:42.860
So Alexa interface.

09:43.160 --> 09:50.520
Now if we type ls, we can see that actually the Alexa interface dot py script is an executable and

09:50.520 --> 09:51.750
so we can execute it.

09:51.750 --> 09:54.680
We can run it as a regular python script.

09:54.690 --> 09:59.580
So this is not yet a Ros two node and we can start it as a regular python script.

09:59.580 --> 10:07.290
So with the command python followed by the name of the script that is the Alex interface.

10:07.290 --> 10:08.940
So let's press enter.

10:09.210 --> 10:12.150
This is Python three since we are using Python three.

10:13.580 --> 10:17.720
And now we can see that the Flask application is online.

10:17.720 --> 10:22.430
So it is online and it is available on the port 5000 of our PC.

10:23.090 --> 10:29.870
Now, let's also start Ngrok, because this just started the web server that listens for the new messages

10:29.870 --> 10:32.420
and interprets the new messages.

10:32.870 --> 10:38.690
Now let's also start andyrock that instead will make available a web service on our PC.

10:39.350 --> 10:44.600
So let's go to the download folder in my case, and then to the Ngrok folder.

10:44.600 --> 10:50.480
And here, in order to execute Ngrok, let's execute the file called Ngrok.

10:50.900 --> 10:59.420
And then we want to open an Http server and we want to open this one on the Port 5000 or on our PC.

11:00.050 --> 11:02.060
So let's press enter.

11:02.240 --> 11:08.120
And since we open that new instance of Ngrok and since we are using the free version of this software,

11:08.120 --> 11:13.500
it provides us with a new address to access our PC from the Internet.

11:13.500 --> 11:20.820
So as you can see, this address here is different from the one that we set previously, and so we need

11:20.820 --> 11:24.090
to change it also within the Arduino bot skill.

11:24.240 --> 11:30.540
And so within the end point here, as you can see, these two address are different since we are using

11:30.540 --> 11:32.430
the Ngrok free version.

11:32.820 --> 11:38.520
So every time we need to change this one and we need again to save the endpoint.

11:38.980 --> 11:42.970
Now everything is ready, so we can go back to the test section.

11:43.670 --> 11:52.400
So here and here we can start the interaction with our web server and also here with our Flask application

11:52.400 --> 11:58.310
that will receive the messages and will interpret them and also will provide the response that the Alexa

11:58.310 --> 12:00.110
assistant will pronounce.

12:00.350 --> 12:02.390
So let's activate this skill.

12:03.680 --> 12:05.540
Activate robot.

12:08.200 --> 12:09.790
Hi, How can I help?

12:10.150 --> 12:15.040
And now we can see that the voice assistant answered with the expected phrase.

12:15.250 --> 12:18.600
So the one that we set here, I.

12:18.610 --> 12:19.450
How can I help?

12:20.580 --> 12:26.050
And so we can see that the Rock was able to receive the message.

12:26.070 --> 12:30.560
This time the request was accepted and was processed correctly.

12:30.570 --> 12:37.260
Since we have the Flask server here that was able to interpret the request and also to send a response

12:37.260 --> 12:38.340
to the server.

12:38.340 --> 12:46.170
And so here in the Alexa we can see now both the messages, so we can see the Json message that Alexa

12:46.170 --> 12:53.310
sent to our web service and here we can see the response that our web service returned to Alexa.

12:53.310 --> 12:59.160
And also here we can see the phrase that we expect that we want Alexa to pronounce.

13:00.180 --> 13:07.470
Now all that remains is to add the endling for the remaining requests inside our script in order to

13:07.470 --> 13:10.710
complete the voice interaction model with the assistant.

13:10.740 --> 13:14.910
Now all that remains is to handle all the remaining requests.

13:14.910 --> 13:21.360
So all the remaining intents that Alexa can send to our web application inside the script that we have

13:21.360 --> 13:26.460
just created in order to complete the voice interaction model with the assistant.

13:26.460 --> 13:33.060
And then also for each of these requests, we will enable an interface with Roscoe and with the task

13:33.060 --> 13:36.300
server that we created in order to move the robot.
