WEBVTT

00:00.050 --> 00:03.080
This is step number eight of the final project.

00:03.110 --> 00:10.610
What we're going to do now is to so when we receive here a notification that someone is ringing the

00:10.610 --> 00:11.000
bell.

00:11.040 --> 00:15.680
We're going to handle the open command so we can open the door.

00:15.680 --> 00:22.340
So we're going to open the door for 10s and then close it and come back to the initial state where the

00:22.340 --> 00:25.460
user can ask another request.

00:25.490 --> 00:30.410
Okay, so the first thing we need to do is to initialize the telegram updater.

00:30.410 --> 00:36.380
So that's what I'm going to do now and I'm going to create a new file here for the step number eight.

00:37.470 --> 00:40.440
Let's copy this inside the step number eight.

00:40.680 --> 00:43.650
Let's close and let's save as.

00:44.870 --> 00:45.650
Intercom.

00:47.090 --> 00:47.660
Step.

00:49.050 --> 00:49.560
Eight.

00:50.950 --> 00:55.870
All right, Now let's start with the initialization of the updater.

00:56.170 --> 01:00.790
So from telegram dot XT import.

01:01.990 --> 01:04.390
Of data and also comment.

01:04.960 --> 01:11.740
And we are going to initialize the data here when we initialize the telegram bot.

01:11.740 --> 01:16.840
So we have bot here, I'm going to do a data is equal to a data.

01:18.500 --> 01:22.530
Token is equal to telegram token.

01:22.550 --> 01:27.620
So the same token and then let's create a dispatcher for our commands.

01:28.010 --> 01:31.190
Dispatcher is equal to a data dot.

01:31.520 --> 01:32.600
Dispatcher.

01:33.230 --> 01:37.070
Then we are going to add handlers for the dispatcher.

01:37.070 --> 01:44.720
So let's put to do add handlers that I'm just going to do after the initialization.

01:44.720 --> 01:49.160
And then we need to do updater dot start polling.

01:49.160 --> 01:55.490
So we need to start getting the updates from the Telegram chat and where we are going to do that.

01:55.490 --> 01:57.410
Well, not just right now.

01:57.410 --> 02:01.820
We are going to do that after the Time.sleep So here we do.

02:03.260 --> 02:10.330
Um, actually, after we reset the input buffer, a data dot starts falling.

02:11.510 --> 02:11.780
Okay.

02:11.780 --> 02:14.030
Why do we do that after the time slip?

02:14.060 --> 02:14.930
Because.

02:14.930 --> 02:19.940
Well, we keep this time actually for the communication to be correctly initialized.

02:19.940 --> 02:25.310
So if you start polling before the communication is correctly initialized and in some of the callbacks,

02:25.310 --> 02:29.540
we're going to send some messages to the Arduino.

02:29.570 --> 02:32.480
Well, you may have some problems at this time.

02:32.480 --> 02:38.450
So we wait after the three seconds to start getting the telegram comments in our code.

02:38.570 --> 02:41.600
And if I do start polling, also need to do stop.

02:42.290 --> 02:44.610
So I'm going to do here print.

02:44.630 --> 02:45.920
So we already have the structure.

02:45.950 --> 02:46.220
Okay.

02:46.220 --> 02:47.420
Nothing more to do.

02:47.510 --> 02:51.950
The while loop with the try except so print.

02:53.290 --> 02:57.190
Stopping telegram a.

02:59.020 --> 03:06.550
Wait a few seconds and let's do a data dot stop.

03:07.450 --> 03:13.480
And then when we have end of program, we know everything is correctly finished.

03:13.600 --> 03:18.190
Okay, now let's add handlers.

03:18.370 --> 03:21.310
And so to add handlers, we will need some callbacks.

03:21.320 --> 03:26.470
I'm just going to create the callbacks on top of the telegram initialization here.

03:26.500 --> 03:32.260
Let's put telegram callbacks here and def.

03:32.260 --> 03:44.710
Let's create first a start handler with update context and let's say that we want to just greet the

03:44.710 --> 03:47.710
user that uses the Start command.

03:47.830 --> 03:56.270
So what we can do is to to send a is equal to, let's say, welcome

03:58.490 --> 04:01.430
plus and how to get the user first name.

04:01.430 --> 04:03.310
Well, that's something I'm going to show you here.

04:03.320 --> 04:12.860
You can do update for example, update dot effective user dot first name.

04:13.520 --> 04:18.320
Let's add an exclamation mark because we are very happy to send this message.

04:18.680 --> 04:20.240
And let's do context.

04:20.960 --> 04:32.990
Dot, dot, dot, send message with chat ID is equal to let's directly reply to update dot effective

04:33.890 --> 04:35.000
chatted.

04:35.000 --> 04:40.670
So as you can see we can get many information in a callback from the user, the chat, the message,

04:40.670 --> 04:46.640
everything and text is equal to string to send.

04:47.180 --> 04:48.050
All right.

04:48.140 --> 04:55.040
And let's add an open door handler, which is going to be more interesting here.

04:55.370 --> 04:58.220
Date context.

04:58.490 --> 05:02.330
Let's put pass for now and we're going to implement that.

05:02.330 --> 05:04.670
But first I'm going to add the handlers here.

05:04.790 --> 05:21.320
So Dispatcher Dot add handler, command handler with start keyword and start handler function and Dispatcher

05:21.470 --> 05:29.060
Dot add handler Command handler with open keyword.

05:29.930 --> 05:30.320
Okay.

05:30.320 --> 05:31.850
If you remember here.

05:31.850 --> 05:38.240
So in the protocol we have defined, we want to send open and we also here send a message to ask to

05:38.240 --> 05:39.590
send open or deny.

05:39.920 --> 05:46.220
So let's use open and this is going to be open door.

05:46.400 --> 05:55.840
Handler Okay, Let's actually start this just to check that the operator is working.

05:55.840 --> 05:57.480
So waiting for three seconds.

05:57.490 --> 05:58.120
Okay?

05:58.120 --> 05:59.650
And let's go to Telegram.

06:00.340 --> 06:02.590
Let's write start.

06:04.250 --> 06:05.120
And you can see.

06:05.150 --> 06:06.410
Welcome, Édouard.

06:06.560 --> 06:07.700
Well, that's me.

06:08.530 --> 06:11.140
And let's kill that Ctrl C.

06:11.410 --> 06:12.040
You can see.

06:12.070 --> 06:13.240
Wait a few seconds.

06:14.970 --> 06:17.160
And then we should have.

06:18.580 --> 06:19.510
End of program.

06:19.550 --> 06:19.780
Okay.

06:19.780 --> 06:21.220
So just be patient here.

06:21.460 --> 06:27.070
And you can see with the three dashes here, it's very easy to see the end of the logs.

06:28.570 --> 06:31.510
Okay, now let's write our open door handler.

06:31.630 --> 06:33.650
So what do we want to do here?

06:33.670 --> 06:35.380
First we want to.

06:35.410 --> 06:37.510
So now we know that we need to open the door.

06:37.510 --> 06:41.200
So we're going to send the command to the Arduino to open the door.

06:41.200 --> 06:48.280
So with the servo motor to print the text that the door is opened to play a sound with the buzzer and

06:48.280 --> 06:50.740
to change the Led to green.

06:50.770 --> 06:53.620
Okay, so for actions we need to send to the Arduino.

06:54.070 --> 06:59.740
We are also going to send the confirmation to Telegram so we can know in the chat that the door has

06:59.740 --> 07:03.760
been opened and then we will wait for 10s.

07:03.880 --> 07:07.610
And after 10s have passed, we are going to close the door.

07:07.630 --> 07:09.850
So let's start.

07:09.880 --> 07:19.190
I'm going to put print opening the door so we know in the log that this is happening and then we do

07:19.220 --> 07:19.850
send.

07:19.880 --> 07:31.660
So let's write the command to the Arduino, send to Arduino open door and then another send to Arduino.

07:31.670 --> 07:34.370
So let's write actually four of them.

07:35.870 --> 07:37.450
So we want to open the door.

07:37.460 --> 07:45.530
We also want to print text and to say that the door is opened.

07:46.700 --> 07:48.410
So that the user can go.

07:48.680 --> 07:51.380
We also want to play.

07:53.600 --> 07:55.580
Let's use 300 heads.

07:55.620 --> 08:00.020
Okay, So the higher the heads, basically, the higher the tone.

08:00.020 --> 08:06.430
And well, usually when you have a higher tone, a higher frequency, this is more welcoming.

08:06.440 --> 08:06.700
Okay?

08:06.710 --> 08:09.070
When you have a sound for a door.

08:09.080 --> 08:12.260
So we're going to put 300 and play that for five.

08:12.770 --> 08:22.850
So let's not put a space here 500 milliseconds and then we can do set led right is going to be zero

08:22.850 --> 08:27.800
and then green, 255 and then blue 000.

08:28.280 --> 08:28.520
Okay.

08:28.520 --> 08:35.540
And with this, we open the door, but then we need a mechanism that is going to close the door after

08:35.540 --> 08:36.170
10s.

08:36.170 --> 08:36.470
All right.

08:36.470 --> 08:39.770
Because the door is not going to close by itself.

08:39.920 --> 08:41.360
And so we can do time.

08:41.360 --> 08:43.970
Dot sleep ten.

08:44.780 --> 08:44.920
Okay.

08:44.930 --> 08:46.100
Don't worry about this time.

08:46.100 --> 08:46.560
That sleep.

08:46.560 --> 08:51.720
I'm going to explain to you just in a bit why use this here and how it's not going to be a problem.

08:51.720 --> 09:04.200
And so after 10s we can do so print closing the door and we can send commands to the Arduino and we'll

09:04.200 --> 09:11.340
just actually, after we have sent command to the Arduino here, we can also do bot dot send message

09:12.450 --> 09:13.620
to the chat.

09:13.950 --> 09:21.900
So chat ID is equal to update dot effective chat.id because we are in a callback.

09:21.900 --> 09:29.700
We have the update with the chat id and text is equal to opening the door.

09:30.870 --> 09:36.450
So we also have a confirmation on the telegram that our command was successfully processed.

09:36.570 --> 09:37.110
Okay.

09:37.380 --> 09:40.260
So then we wait for 10s and we close the door.

09:40.260 --> 09:43.680
So to close the door we send to Arduino.

09:43.710 --> 09:46.920
We're going to send also four commands.

09:49.310 --> 09:49.700
Okay.

09:49.700 --> 09:50.930
So we're going to send.

09:51.810 --> 09:58.140
Close the door and then print text.

09:58.170 --> 09:59.910
Let's go back to normal.

10:00.120 --> 10:00.660
Push on.

10:00.660 --> 10:03.690
Button to call.

10:04.470 --> 10:06.690
We're going to play also.

10:06.720 --> 10:07.590
Buzzer okay.

10:07.590 --> 10:09.840
To inform that the door is actually closing.

10:09.990 --> 10:11.160
Let's say 200.

10:11.160 --> 10:19.320
So a lower pitch for also 500 milliseconds and then set back to blue.

10:19.530 --> 10:24.750
So 000000 255.

10:25.620 --> 10:25.800
Okay.

10:25.800 --> 10:29.490
So as you can see, if you want to send comments to the Arduino, well, now that's very easy.

10:29.490 --> 10:36.630
You just do send to Arduino with the correct command and parameters and everything is going to be taken

10:36.630 --> 10:39.660
care of because you have written the code before to do that.

10:40.260 --> 10:40.770
Okay.

10:40.770 --> 10:47.940
And now if you remember, we still have a flag that we have set here, open door request.

10:47.970 --> 10:58.140
So what we want to do is we want to say that at the end open door request is equal to false so we can

10:58.140 --> 10:59.130
send a request.

10:59.130 --> 11:04.200
And then when we open the door, so we send the message from Telegram to the Raspberry Pi.

11:04.230 --> 11:06.060
This is going to open the door.

11:06.180 --> 11:12.290
And then after 10s close the door and then the open door request is no false.

11:12.300 --> 11:16.680
So we can send another request with another photo to telegram.

11:17.280 --> 11:17.790
Okay.

11:17.790 --> 11:24.060
And well, if we want actually to use the open door request boolean, which is a global variable, we

11:24.060 --> 11:30.930
need to do global open door request here because we are inside a function.

11:30.930 --> 11:36.270
So if you are inside a function in Python and you want to access a global variable to modify it, you

11:36.270 --> 11:39.350
need to use global open door request inside the function.

11:39.360 --> 11:44.460
Otherwise, well, you're just going to create a new variable open door request which is not going to

11:44.460 --> 11:45.480
be the global one.

11:45.480 --> 11:50.730
And one additional thing I'm going to do if open door request

11:53.580 --> 11:55.560
and put that inside the if.

11:55.740 --> 11:57.180
Why is that?

11:57.210 --> 11:58.380
Because.

11:58.710 --> 12:00.440
Well, let's open the three one.

12:01.720 --> 12:02.470
A chat.

12:04.260 --> 12:11.790
Thing is that if you send slash open command, so now the code is not running, so that's not going

12:11.790 --> 12:12.350
to work.

12:12.360 --> 12:16.440
But anytime you send open, that's going to go inside the open door.

12:16.470 --> 12:17.490
Handler Right.

12:17.520 --> 12:20.760
Like anytime you send start, it's going to say welcome.

12:20.760 --> 12:23.220
So every time you send open, it's going to go here.

12:23.220 --> 12:26.100
And so if you send open, it's going to open the door.

12:26.100 --> 12:31.020
But what we want in this application is that you only open the door when you have a request.

12:31.020 --> 12:32.850
So we check that what we have a request.

12:32.850 --> 12:35.940
If we have a request, we open the door and then we close it.

12:35.940 --> 12:38.820
And then we put the open door request to false.

12:38.820 --> 12:43.680
So of course this kind of decision is depending on your real application.

12:43.680 --> 12:47.190
Maybe you want to open the door at any time and then that's fine.

12:47.190 --> 12:48.180
You just don't put that.

12:48.180 --> 12:52.590
If here, here, just put it because well, I've decided in the application to do that.

12:52.590 --> 12:54.870
But this will depend on what you want to really do.

12:54.900 --> 12:55.710
Of course.

12:55.710 --> 13:00.630
And one other thing I'm going to add here, because if you just run like this, you may have some weird

13:00.630 --> 13:06.490
behaviors, for example, that if you send a request and then from telegram, you press, you send multiple

13:06.490 --> 13:12.190
times open, well, what you will see is that this is first going to be executed and then it's going

13:12.190 --> 13:13.530
to be executed again.

13:13.540 --> 13:14.290
Why is that?

13:14.290 --> 13:14.680
Because.

13:14.680 --> 13:18.880
Well, how is the updater working basically with the dispatcher?

13:19.120 --> 13:27.070
When you receive a keyword, it's going to directly call the callback function in a different thread.

13:27.070 --> 13:27.250
Okay.

13:27.310 --> 13:31.600
So that's why, for example, the time sleep is not a problem for the main thread because that's going

13:31.600 --> 13:32.830
to be on a different thread.

13:32.830 --> 13:33.090
Okay.

13:33.100 --> 13:35.830
So now we are going to talk about Multithreading.

13:35.830 --> 13:40.630
It's a bit more complex, but I'm going to keep things simple here so you can follow.

13:40.840 --> 13:47.650
So you have the main thread here in the while loop, and when you receive a command in a different thread,

13:47.650 --> 13:49.180
you're going to execute the callback.

13:49.180 --> 13:55.690
But if you receive multiple comments, all the callbacks are going to be executed one by one in these

13:55.690 --> 13:56.290
different threads.

13:56.290 --> 14:03.010
So if you receive multiple open door handler comments, it's going to be executed once and then another

14:03.010 --> 14:04.030
time, another time.

14:04.030 --> 14:09.430
And because of the time sleep, then if you receive, for example, three comments, well it's going

14:09.430 --> 14:16.180
to first spend 10s in that and then 10s in the second one and 10s in the third one.

14:16.180 --> 14:23.170
So what we want to do basically is to say that instead of queuing the callbacks, we want to execute

14:23.170 --> 14:25.660
them all inside different threads.

14:25.660 --> 14:31.600
So every time you receive a new command, you, you execute it in yet another thread so you can run

14:31.600 --> 14:33.850
multiple callbacks at the same time.

14:33.850 --> 14:36.460
And to do this, this is actually very easy.

14:36.460 --> 14:40.690
So it's a bit complicated to explain, but it's very easy to implement.

14:40.720 --> 14:49.360
You put run async, so run asynchronously is equal to true in the command handler.

14:49.360 --> 14:51.970
So you had a third optional argument here.

14:52.210 --> 14:59.050
And now that we have this, we have yet another problem because so let's say now you request the opening

14:59.050 --> 15:03.310
of the door, you receive that on Telegram and you send open three times.

15:03.400 --> 15:07.570
So the three open door handler are going to be executed at the same time.

15:07.810 --> 15:11.320
And the open door request is going to be true for all of them.

15:11.320 --> 15:15.640
So from three different threads, we're going to send this and then send this.

15:15.640 --> 15:16.810
So we don't want this.

15:16.810 --> 15:22.690
And I'm going to add yet another flag here that I'm going to name, Let's name it.

15:22.720 --> 15:26.230
Handling door is equal to false.

15:26.230 --> 15:32.890
So whenever we handle the door, which means basically that we open the door, we close the door, we

15:32.890 --> 15:34.120
deny the access.

15:34.270 --> 15:39.880
Every time we handle the door, we're going to put this flag to true so that any other callback will

15:39.880 --> 15:42.280
not be able to handle the door at the same time.

15:42.280 --> 15:44.380
And so in our.

15:45.160 --> 15:52.840
Open door handler I'm going to do also global handling though, so I can access the global variable

15:52.840 --> 15:54.100
and modify it.

15:54.100 --> 15:58.600
And so I'm going to check before I open the door, I'm going to check that I have an open door request

15:58.600 --> 16:05.320
and that I don't have the handling door flag on.

16:05.320 --> 16:12.700
And then if I open the door, I'm going to set the handling door flag to true.

16:13.960 --> 16:18.310
And once I am done, I'm going to set it to false.

16:21.310 --> 16:29.140
Okay, So that is maybe not the ideal best 100% solution to Multithreading, but this is going to be

16:29.140 --> 16:33.400
fine for our application and I don't want to go too deep on Multithreading here.

16:33.400 --> 16:38.800
That's out of the scope of this course, but we still need to work a bit on that.

16:38.800 --> 16:40.870
So we have an application that works well.

16:40.870 --> 16:42.100
So what's going to happen here?

16:42.100 --> 16:48.350
Let's say you send three open comments from Telegram and you have the open door request on because you

16:48.350 --> 16:50.330
have previously pressed on the push button.

16:50.330 --> 16:55.010
So you're going to have three callbacks that are going to be called, let's say the first one is going

16:55.010 --> 16:59.450
to see that, yes, we have a request and we don't have the handling door flag.

16:59.570 --> 17:05.660
So it's going to enter here and put the flag to true directly and then it's going to do its thing and

17:05.660 --> 17:06.860
sleep for 10s.

17:06.860 --> 17:11.630
And then the second and the third callbacks are going to be called independently.

17:11.630 --> 17:16.610
But when they are going to arrive at that, if they're going to see that, yes, we have the open door

17:16.610 --> 17:19.100
request, but this flag is already true.

17:19.100 --> 17:22.070
So we can't enter the if and we're going to exit.

17:22.070 --> 17:27.740
And so you're going to only have one callback that is working at a time.

17:27.740 --> 17:35.750
And finally, the last thing I'm going to add in the main loop here, I'm also going to add and not

17:36.350 --> 17:38.210
handling door.

17:38.330 --> 17:44.270
So basically we want to take a photo and send it to Telegram when we have at least five second passed

17:44.270 --> 17:50.810
and when we don't have an open door request that is currently on and also when we are not handling the

17:50.810 --> 17:51.080
door.

17:51.320 --> 17:51.540
Okay.

17:51.560 --> 17:54.950
Because if we are handling the door well, the button should not work.

17:54.980 --> 17:59.840
The button should only work after the door is correctly handled.

18:00.080 --> 18:00.860
All right.

18:01.100 --> 18:02.870
So now we can test this code.

18:02.870 --> 18:08.690
If you had some trouble about the Multithreading explanation, well, that's not a problem because this

18:08.690 --> 18:13.880
is something we haven't talked about in this course that's a bit outside of the scope and you can come

18:13.880 --> 18:17.300
back to it later and continue to make progress on the project.

18:17.300 --> 18:22.970
Basically, you could do without the Multithreading, but here this is just so we can improve the code

18:23.000 --> 18:24.800
to make it more robust.

18:24.800 --> 18:27.440
And now I'm going to run that code.

18:31.000 --> 18:31.330
Okay.

18:31.330 --> 18:33.070
We have the starting here.

18:33.430 --> 18:35.050
Waiting for three seconds.

18:35.050 --> 18:36.940
Main loop is starting.

18:37.300 --> 18:38.860
I open the telegram app.

18:38.860 --> 18:40.840
I'm going to press on the push button.

18:40.960 --> 18:41.650
You can see.

18:41.680 --> 18:43.420
Please wait for a few seconds.

18:44.170 --> 18:46.270
Someone is ringing the bell.

18:46.300 --> 18:48.730
He and well, let's say we want to open.

18:48.730 --> 18:51.820
So I'm going to send open or just click on open here.

18:52.570 --> 18:53.230
Okay.

18:53.230 --> 18:54.220
It's working.

18:54.250 --> 18:56.730
Door is opened, green led.

18:57.670 --> 18:58.720
The door is opened.

18:58.720 --> 19:00.130
Let's wait 10s.

19:01.590 --> 19:03.210
And we should have.

19:03.930 --> 19:04.700
Okay.

19:04.710 --> 19:05.400
Push on.

19:05.400 --> 19:06.270
Button to call.

19:06.870 --> 19:09.810
Blue led and the door is closed.

19:09.960 --> 19:16.590
And with the few additions we did here on the code, you can also try to send multiple open command

19:16.590 --> 19:19.430
here and you're going to see that they are not going to work.

19:19.440 --> 19:26.010
And also, if I send open now, well, the callback is still working, but we can't open the door because

19:26.010 --> 19:27.720
we don't have a request.

19:27.930 --> 19:28.440
All right.

19:28.440 --> 19:30.810
And that's the end of the step number eight.

19:30.810 --> 19:33.270
We're almost done with the final project.
