WEBVTT

00:00.080 --> 00:00.770
Okay, great.

00:00.770 --> 00:08.960
You can send notification from the Python program to the chat ID and you can also add some callbacks

00:08.960 --> 00:11.210
so you can respond to some comments.

00:11.210 --> 00:14.210
But now let's mix those two.

00:14.240 --> 00:14.630
Okay.

00:14.630 --> 00:22.250
So we can have a program that sends independent notifications and that also responds to comments here.

00:22.250 --> 00:25.190
So I'm going to show you the program structure we're going to use.

00:25.190 --> 00:29.890
And this is also something we're going to use for the next activities and for the final project.

00:29.900 --> 00:33.320
So I'm going to start I'm not going to start from scratch.

00:33.320 --> 00:39.350
I'm going to start from this Telegram bot program here, which is already working.

00:39.350 --> 00:43.970
And I'm going to add simply this into that program.

00:44.090 --> 00:51.110
And so what we do here is we create some handler, okay, with an updater that's going to keep an eye

00:51.110 --> 00:56.360
on the chat, a dispatcher that's going to dispatch any comment you receive to the correct handler.

00:56.360 --> 01:01.590
We start polling and then we use idle to stop the program here.

01:02.260 --> 01:09.390
What I'm going to do is I'm going to remove this idle and we are going to use instead a while true loop.

01:09.390 --> 01:09.630
Okay.

01:09.630 --> 01:13.410
So basically we are going to use an infinite loop just like we did before.

01:13.410 --> 01:15.930
This is kind of the void setup.

01:15.930 --> 01:20.460
And then we are going to create a void loop like we have on the Arduino.

01:20.460 --> 01:24.540
And I'm going to do while true.

01:25.650 --> 01:25.920
Okay.

01:25.920 --> 01:33.300
And this is where we are going to do our program and for example, send some notifications.

01:33.300 --> 01:39.660
So if we want to send some notification, I'm going to do this from Telegram import bot, I'm going

01:39.660 --> 01:45.480
to put it there and I'm also going to create a bot.

01:46.620 --> 01:49.710
Okay, so we already have the token.

01:50.230 --> 01:50.440
Okay.

01:50.460 --> 01:54.450
So create a bot here just before the updater for example.

01:54.450 --> 01:57.150
So we have the bot and the updater.

01:57.750 --> 02:00.570
And for each we give the telegram token.

02:00.840 --> 02:01.140
Okay.

02:01.140 --> 02:04.860
I'm also going to do this chat ID, okay.

02:05.790 --> 02:10.110
And put it there so we can use the chat ID with the bot.

02:10.440 --> 02:18.060
We could use this chat ID actually here so that any time you send a comment to the bot, we're going

02:18.060 --> 02:19.530
to respond to this group.

02:19.530 --> 02:21.060
But I'm going to keep it like this.

02:21.060 --> 02:27.600
So basically this means that if you have another group or if you just send a message directly from your

02:27.600 --> 02:36.870
user to the bot, well by using updated active chatted, you're going to respond to whoever is sending

02:36.870 --> 02:39.110
the message, whoever is sending start.

02:39.120 --> 02:46.800
Okay, So from this group or from another group or from your user, if you use this chat ID well, every

02:46.800 --> 02:49.230
time you receive something, you're just going to send back a message.

02:49.230 --> 02:50.310
But to this group.

02:50.310 --> 02:56.070
So if you send a command from another group, you are still going to reply to this group instead.

02:56.070 --> 02:57.600
Okay, so we're going to keep this.

02:57.600 --> 03:02.400
So we directly respond to the one who sends the comment.

03:02.880 --> 03:08.080
I'm going to remove this print here and then, well, we still have this chat ID, so when we send a

03:08.080 --> 03:11.980
notification, however, that's always going to be in this group.

03:11.980 --> 03:21.280
And so now let's say we want to send a notification every 10s for example, what we can do is do bot

03:21.280 --> 03:30.220
dot send message with chat ID is equal to chat, ID and text.

03:31.690 --> 03:33.820
Let's put Hi.

03:35.240 --> 03:37.280
Something new.

03:38.630 --> 03:40.010
And let's add a time.

03:40.010 --> 03:44.660
I'm going to add a time dot sleep here for, let's say 10s.

03:44.780 --> 03:47.540
Let's import time.

03:48.200 --> 03:48.440
Okay.

03:48.440 --> 03:49.370
So here, no problem.

03:49.370 --> 03:51.710
We can use Time.sleep.

03:51.710 --> 03:56.840
So we are not going to use that in the future if we want to do many things in the whiletrue.

03:56.840 --> 04:02.150
But for this simple example, if we just want to send a message every 10s, that's okay.

04:02.150 --> 04:09.680
And that's not going to be a problem for the updater because this is going to pull the telegram chat

04:09.680 --> 04:11.090
in a different thread.

04:11.090 --> 04:17.120
Okay, so the start handler is going to be in a different thread, not on the same thread as this.

04:17.120 --> 04:18.710
So this is not a problem.

04:18.710 --> 04:20.270
We can block the application.

04:20.540 --> 04:26.390
The polling for the updater is still going to work, but then, well, the program is still not complete.

04:26.420 --> 04:27.050
Why is that?

04:27.050 --> 04:33.440
Because now when we start polling and then we do this and if I do control C, I'm going to exit from

04:33.440 --> 04:34.250
the application.

04:34.250 --> 04:38.010
But first I need to close the updater.

04:38.040 --> 04:38.490
Okay?

04:38.490 --> 04:42.780
Just like when you use Serial, you need to close serial communication here.

04:42.780 --> 04:48.060
We need to close the updater with the function idle that was already done for you.

04:48.060 --> 04:49.410
But now we need to do this.

04:49.410 --> 04:56.550
So I'm simply going to do what we did before is to put this while true inside a try.

04:56.580 --> 04:57.390
Except.

04:58.590 --> 05:09.780
We've keyboard interrupt and when we have the keyboard interrupt, I'm going to do a data dot stop.

05:10.010 --> 05:10.150
Okay.

05:10.170 --> 05:12.600
We use the stop function on the update.

05:14.040 --> 05:15.690
This can take some time.

05:15.690 --> 05:21.850
So we are also going to print print stopping data.

05:22.870 --> 05:23.580
These.

05:25.260 --> 05:28.050
Wait a few seconds.

05:29.520 --> 05:29.850
Okay.

05:29.850 --> 05:34.500
So we know we have to wait a bit and not price controls in multiple times.

05:34.500 --> 05:36.810
And let's also add a print here.

05:36.960 --> 05:39.390
Send message so it's easier to debug.

05:40.410 --> 05:41.160
Okay.

05:41.160 --> 05:43.920
Now let's run that code.

05:45.940 --> 05:49.720
Okay, so we have telegram bot ready.

05:49.750 --> 05:52.900
We can start to send some commands with start.

05:53.080 --> 05:59.680
We receive hello from Python and we also every ten second receive send message.

06:00.040 --> 06:05.050
So we actually receive hi something new and we have send message here on the terminal.

06:05.050 --> 06:05.830
Let's wait a bit.

06:05.860 --> 06:06.100
Okay.

06:06.130 --> 06:06.790
Send message.

06:06.790 --> 06:07.630
You can see.

06:07.660 --> 06:08.170
Hi.

06:08.200 --> 06:08.860
Something new.

06:08.860 --> 06:14.350
And we have also received a notification on my phone and on the desktop app.

06:14.560 --> 06:16.120
And so you can see.

06:16.600 --> 06:16.840
Hi.

06:16.870 --> 06:17.530
Something new.

06:17.530 --> 06:25.900
I can send a command, receive responses from the callback and I can also receive independent notifications,

06:25.930 --> 06:27.850
all that at the same time.

06:28.570 --> 06:34.240
Now I can go there, press Ctrl C stopping a data please wait a few seconds.

06:34.240 --> 06:35.380
So we wait.

06:36.650 --> 06:41.810
And the updater is now stopped and the program exited.

06:42.470 --> 06:43.130
Okay, great.

06:43.160 --> 06:50.000
Now you have a complete Python program structure to communicate between your Raspberry Pi and a telegram

06:50.000 --> 06:50.630
chat.
