WEBVTT

00:00.050 --> 00:03.260
This is step number seven of the final project.

00:03.260 --> 00:10.220
And in this step, we are going to send a message along with the photo we've just taken on the Telegram

00:10.250 --> 00:18.590
app whenever someone has requested to open the door so we already have the code to receive the button

00:18.590 --> 00:20.690
pressed message from the Arduino.

00:20.720 --> 00:24.940
We know this is an open door request and so we take a photo.

00:24.950 --> 00:30.650
Now let's initialize the telegram bot to send that photo and a message.

00:30.830 --> 00:34.430
And so I'm not going to set up the bot here, okay?

00:34.730 --> 00:38.780
If you haven't set up a bot and if you haven't created the group.

00:38.780 --> 00:45.080
So I'm going to also use the Telegram app from the Raspberry Pi or from your phone.

00:45.080 --> 00:45.320
Okay.

00:45.320 --> 00:46.430
It doesn't matter.

00:47.150 --> 00:53.150
So if you don't have a bot, if you don't have already a token saved on the Raspberry Pi, and if you

00:53.150 --> 00:58.730
don't have a group chat with the bot, then please come back to the lesson on how to set everything

00:58.730 --> 00:59.300
up.

00:59.450 --> 00:59.750
Okay.

00:59.750 --> 01:05.610
And now I'm going to here import telegram in the code.

01:05.610 --> 01:15.630
So I'm going to import the bot from telegram import bot and where are we going to initialize the bot

01:15.660 --> 01:21.210
where we initialize serial, we initialize the camera and then we wait.

01:21.210 --> 01:23.610
Let's just initialize the bot here.

01:23.610 --> 01:29.460
So init telegram bot and we can do print.

01:31.080 --> 01:31.350
It.

01:32.310 --> 01:34.230
Telegram bot.

01:36.010 --> 01:40.210
We've bought is equal to but token.

01:40.210 --> 01:46.540
So we need to give a token and to get the token we are going to get it from where we have saved it before.

01:46.540 --> 02:03.940
So we've open slash home slash by slash dot local slash share slash dot telegram bot token.

02:03.940 --> 02:09.760
So if again you don't understand this, please go back to the lesson on how to create a token and how

02:09.760 --> 02:14.980
to save it on the Raspberry Pi with a for reading.

02:15.160 --> 02:26.460
So as if and we do telegram token is equal to f dot read dot strip.

02:27.520 --> 02:27.660
Okay.

02:27.730 --> 02:35.030
So we don't have any special character at the end and we do then bot is equal to bot with token is equal

02:35.030 --> 02:38.240
to telegram token.

02:39.730 --> 02:40.420
All right.

02:40.900 --> 02:42.850
I'm not going to do the updater now.

02:42.850 --> 02:45.010
We're going to do that in the next step.

02:45.010 --> 02:52.420
And also what I'm going to do before I save is to create a new file for the step.

02:52.600 --> 02:53.470
Okay?

02:53.470 --> 02:55.750
So don't save that one.

02:56.830 --> 02:57.480
Do you want to save?

02:57.490 --> 03:00.220
No, but I'm going to save this as.

03:01.640 --> 03:02.570
Intercom.

03:03.500 --> 03:05.750
Step seven.

03:07.480 --> 03:08.170
All right.

03:08.560 --> 03:10.750
And let's continue where we were.

03:10.780 --> 03:16.300
So here we create our boat so we can send message from the boat.

03:16.300 --> 03:23.590
And to send message, we also need the chat ID What is the chat ID of our group here?

03:23.710 --> 03:35.260
Well, I'm going to come back to one of the previous Python file where we do send notification and we

03:35.260 --> 03:37.570
have the chat ID, so that is the chat ID.

03:38.600 --> 03:39.590
That I got.

03:41.080 --> 03:42.970
Okay from this group.

03:43.450 --> 03:49.480
And again, if you don't know how to get that, go back to the lessons and the telegram bot.

03:49.690 --> 03:51.880
And so chat ID is that one.

03:52.390 --> 04:01.270
Okay, now we can send messages from this bot to the chat group here and let's do that actually.

04:02.020 --> 04:09.190
So when we have opened or request taking a photo and I'm going to add another log and sending it to

04:09.790 --> 04:10.720
Telegram.

04:12.010 --> 04:21.370
So we do camera dot capture and then we're going to send a message, but not send message.

04:22.060 --> 04:24.790
Chat ID is equal to chat.

04:24.910 --> 04:36.280
ID and text is equal to let's say someone is ringing the bell.

04:38.590 --> 04:41.150
We are also going to send the photo.

04:41.150 --> 04:44.030
So to send the photo we need to open it because we capture it.

04:44.030 --> 04:50.900
But then we need to open the file with open so we have the image file name already.

04:52.640 --> 05:07.010
We are going to use our permission as photo and we do bot dot send photo with chat id is equal to chat

05:07.040 --> 05:15.350
id the one we have and photo is equal to the photo we have here and we're going to receive.

05:15.350 --> 05:18.620
Someone is ringing the bell plus the photo.

05:18.650 --> 05:26.030
What we can also send is well, some instructions on what to answer to that.

05:26.030 --> 05:26.510
Okay.

05:26.510 --> 05:31.670
Because if you just say someone is ringing the bell, well, thank you for the information, but what

05:31.670 --> 05:32.750
do I do next?

05:32.780 --> 05:39.380
What you can send is the command to reply if you want to open or deny access.

05:39.380 --> 05:44.920
So bot dot send message chat id is equal to chat.

05:45.610 --> 05:48.850
ID text is equal to.

05:49.330 --> 05:51.190
And then let's go back to.

05:52.570 --> 05:54.490
The protocol we have.

05:57.540 --> 06:03.390
And you can see what we want is that the user on Telegram sends open or deny.

06:04.790 --> 06:13.530
So why not just saying to the user, send slash open or slash deny?

06:13.550 --> 06:19.280
And by doing this, it's actually great because well, on the application you can just click, you will

06:19.280 --> 06:23.630
be able to click on that, you will see and that's automatically going to send the comment.

06:23.660 --> 06:28.520
Okay, so we're going to see how to open or deny just in the following steps.

06:28.520 --> 06:30.440
Now let's run that code.

06:30.440 --> 06:33.440
So the code is still running on the Arduino.

06:34.040 --> 06:35.590
Let's run it here.

06:35.600 --> 06:36.890
Let's run the python.

06:36.890 --> 06:38.870
Waiting for three seconds.

06:39.290 --> 06:39.610
Okay.

06:39.620 --> 06:41.600
Main loop is starting.

06:41.600 --> 06:42.950
I'm going to open.

06:43.950 --> 06:44.820
This.

06:44.850 --> 06:49.260
Okay, so let's press on the push button.

06:49.920 --> 06:54.930
You can see open door request taking a photo and sending it to telegram.

06:54.930 --> 06:58.940
And a few seconds after we have, someone is ringing the bell.

06:58.950 --> 07:05.010
So this first message and then the photo and then send open or deny.

07:05.310 --> 07:08.010
And you can see that's what I was talking about.

07:08.040 --> 07:14.520
You can directly click so you can click from your phone or from the desktop, for example, open or

07:14.520 --> 07:14.910
deny.

07:14.910 --> 07:18.120
So here it doesn't do anything because we don't have the handler.

07:18.150 --> 07:20.430
Of course, that's what we're going to do next.

07:20.610 --> 07:24.630
And before we continue, I'm going to improve the code a little bit.

07:24.660 --> 07:25.560
Why is that?

07:25.560 --> 07:29.430
Because here, for example, if I press on the button, let's say three more times.

07:31.640 --> 07:35.360
You can see we have a lot of open door requests.

07:35.480 --> 07:36.890
So actually priced more than.

07:38.410 --> 07:44.260
Three times and you can see where we receive many upon our request.

07:44.260 --> 07:48.070
And that's kind of going to flood our conversation.

07:48.520 --> 07:48.760
Okay.

07:48.850 --> 07:54.640
That's going to completely spam the conversation and we're going to receive so many notifications.

07:54.640 --> 08:00.640
So if someone is spamming on the button, well, you're going to receive hundreds and hundreds of messages.

08:00.640 --> 08:08.380
So what I'm going to do is I'm going to do first I'm going to say that we are only going to take a photo

08:08.380 --> 08:10.690
and send it to Telegram.

08:10.780 --> 08:15.790
If five seconds have passed between now and the last time we did this.

08:15.820 --> 08:16.390
Okay.

08:16.390 --> 08:21.520
So basically, I'm going to add another delay on the button pressed command of five seconds.

08:21.520 --> 08:24.460
So we only process the command every five seconds.

08:24.640 --> 08:30.280
So if you spam on the button, we're still going to receive many button press command, but we're only

08:30.280 --> 08:31.810
going to execute it.

08:31.840 --> 08:35.590
We're only going to go in the if every five seconds max.

08:35.830 --> 08:40.720
And so I'm going to add a new global variable here at the beginning.

08:41.380 --> 08:54.310
For example, here to say last time button pressed is equal to let's initialize it to time dot time.

08:54.310 --> 09:05.320
So the current time and let's say button pressed delay is equal to five seconds.

09:05.350 --> 09:07.720
So let's put seconds here so we know it's seconds.

09:08.980 --> 09:09.520
Okay.

09:09.520 --> 09:16.480
So then if you want to modify the delay, you just modify this variable here and then well, it's quite

09:16.480 --> 09:19.960
simple actually in this.

09:19.960 --> 09:24.700
So we'll actually, when we receive the message, button pressed is correct.

09:25.030 --> 09:35.680
And if it's correct, we do another if if time dot So let's actually create time now is equal to time

09:35.680 --> 09:37.090
dot time.

09:37.300 --> 09:45.650
So if time now minus let's go back here, make sure we have the same last time button pressed.

09:48.300 --> 09:55.080
Here he is greater or equal than the button pressed delay.

09:55.500 --> 09:58.620
In that case, we're going to do this.

09:58.620 --> 10:02.550
I'm going to put this inside the if.

10:03.150 --> 10:09.840
And we are also going to update the last time button pressed with time dot now.

10:10.080 --> 10:15.600
So now let's run that again and just look at the logs here.

10:16.980 --> 10:17.280
Okay.

10:17.310 --> 10:24.570
Main loop is starting and I'm going to press on the push button and I'm going to continue to press on

10:24.570 --> 10:25.110
the push button.

10:25.110 --> 10:28.800
But you see, we only have one open door request.

10:29.160 --> 10:31.410
And if I continue, well, another one.

10:31.410 --> 10:34.170
But that is five seconds after.

10:34.650 --> 10:34.920
Okay.

10:34.920 --> 10:38.010
And so we have received here not so many.

10:38.010 --> 10:43.080
We have received two new notifications, but not 20.

10:43.080 --> 10:48.580
And well, when looking at this, we can still improve because when the user has pressed on the push

10:48.580 --> 10:52.290
button and you have the text, please wait for a few seconds.

10:52.300 --> 10:57.880
What we can decide is to not send any other notification to Telegram.

10:58.000 --> 10:59.440
Okay, just one.

10:59.440 --> 11:04.270
When we press on the push button and then it's going to wait for the answer from telegram.

11:04.630 --> 11:04.840
Okay?

11:04.840 --> 11:10.030
Because in our situation now, someone can still spam on the push button and you're going to receive

11:10.030 --> 11:12.280
a new notification every five seconds.

11:12.280 --> 11:15.250
So I'm going to add here a new.

11:16.110 --> 11:19.480
Flags or boolean that I'm going to name.

11:19.500 --> 11:26.100
Open door request is equal to false at the beginning, of course.

11:26.100 --> 11:37.470
And so when we receive the open door request, what we do actually is to say open door request is equal

11:37.470 --> 11:39.160
to true.

11:39.180 --> 11:40.680
So now the flag is true.

11:40.680 --> 11:43.440
And what we're going to do is we're going to add something in this.

11:43.470 --> 11:52.500
If I'm going to put if and and another condition here, for example, let's put parentheses so it's

11:52.500 --> 11:59.670
a bit clearer, not open door request.

12:00.420 --> 12:05.190
So when you press on the push button and you have the screen, please wait for a few seconds.

12:05.220 --> 12:11.970
If you press again on the push button one second after or 10s or 20s after, it's not going to take

12:11.970 --> 12:12.590
a new photo.

12:12.600 --> 12:14.250
It's not going to send anything.

12:14.250 --> 12:16.380
And so here we are blocking the code.

12:16.380 --> 12:24.930
But what we're going to do next is that when the here when we send open or deny from Telegram, we are

12:24.930 --> 12:26.940
going to release this.

12:26.940 --> 12:30.000
So we are going to put the flag open door request to false.

12:30.000 --> 12:34.800
And so we give another chance for the user to do an open door request.

12:35.130 --> 12:35.550
Okay.

12:35.550 --> 12:41.970
So with this mechanism, we are sure that we are going to receive just one notification for one user.

12:41.970 --> 12:47.610
And so later on, if we just, for example, accept or deny very fast, we are not going to receive

12:47.610 --> 12:50.220
more than one notification every five seconds.

12:50.220 --> 12:53.880
So this way we protect a little bit our telegram chat.

12:53.880 --> 12:54.060
Okay.

12:54.060 --> 12:57.120
Because you don't want to flood your chat.

12:57.150 --> 12:58.920
Let's run this.

13:00.520 --> 13:04.380
And okay, we have an error here.

13:04.390 --> 13:06.250
Invalid syntax.

13:08.570 --> 13:12.620
Well, it's maybe better if I put a backslash here for multi-line.

13:12.620 --> 13:13.160
If.

13:14.970 --> 13:16.740
And waiting for three seconds.

13:17.960 --> 13:18.250
Okay.

13:18.260 --> 13:19.280
Main loop is starting.

13:19.400 --> 13:20.360
Press on the push button.

13:20.360 --> 13:23.120
Let's look at the log open door request.

13:23.420 --> 13:23.780
Okay.

13:23.780 --> 13:25.940
We have notification.

13:27.260 --> 13:27.860
Okay.

13:27.860 --> 13:29.510
And then press again.

13:30.660 --> 13:31.390
I press again.

13:31.400 --> 13:32.450
Nothing is happening.

13:32.450 --> 13:35.600
I press again and no more than five seconds have passed.

13:35.600 --> 13:41.360
But still nothing is happening because now we are waiting for the response from Telegram.

13:41.360 --> 13:43.240
And that's what we are going to do now.

13:43.250 --> 13:46.940
So Ctrl C and that's the end of the step number seven.
