WEBVTT

00:00.150 --> 00:07.770
So you have a telegram account, your account is correctly configured and you have created a bot with

00:07.770 --> 00:08.920
the bot fader.

00:08.940 --> 00:14.640
You also have the group here with at least you and the bot.

00:14.970 --> 00:21.780
Now, in this lesson we are going to write some Python code so that the bot can respond to some comments.

00:21.790 --> 00:29.700
So for example, if I do slash start here so nothing happens, but we are going to respond to that command

00:29.700 --> 00:30.870
from the Raspberry Pi.

00:31.080 --> 00:37.050
So what you're going to see when you send slash stat from the telegram application that can be from

00:37.050 --> 00:41.550
the desktop or from your phone or from anywhere, you're going to get a response here.

00:42.180 --> 00:42.900
Let's do that.

00:42.900 --> 00:46.980
So we already have the token in the previous lesson.

00:46.980 --> 00:51.060
We have gotten the token here from the bot father.

00:51.060 --> 00:58.270
We have stored the token inside a file and now we get the token inside our Python program.

00:58.290 --> 01:06.060
Now, to be able to write some program for the bot, we will install a python module and this python

01:06.060 --> 01:08.520
module is named Python telegram.

01:08.520 --> 01:13.710
But so first of all, make sure you have pip three so sudo apt.

01:15.140 --> 01:18.470
Install python three dash pipe.

01:20.160 --> 01:21.030
Look from.

01:21.030 --> 01:21.780
It's already done.

01:21.780 --> 01:23.250
And then p IP three.

01:25.550 --> 01:29.330
Instal python dash.

01:29.630 --> 01:30.500
Telegram.

01:30.860 --> 01:31.440
Dash.

01:31.490 --> 01:32.210
But.

01:32.390 --> 01:34.070
And then you can also add dash.

01:34.070 --> 01:36.320
Dash, upgrade.

01:40.130 --> 01:44.180
And for me that was actually already installed.

01:44.690 --> 01:51.410
So now that you have the Python telegram bot module, let's use it in the code.

01:51.830 --> 02:02.470
So you're going to do from Telegram dot X to import the data and command handler.

02:02.640 --> 02:07.190
I'm just going to explain to you what this is and what it does.

02:07.910 --> 02:10.370
So we have the telegram token here.

02:10.370 --> 02:18.740
I'm going to first create a data set of data is equal to data and you need to give the tokens token

02:19.730 --> 02:20.930
is equal to.

02:21.820 --> 02:22.570
Telegram.

02:24.130 --> 02:24.670
Token.

02:25.120 --> 02:28.660
So the telegram token you have read from the file.

02:28.840 --> 02:36.430
So basically, the updater will allow this program here to well being kept updated of what's happening

02:36.520 --> 02:40.390
in the Telegram application when you send some message to the bot.

02:40.690 --> 02:52.310
And then on top of the data, we are going to add a dispatcher is equal to the data dispatcher.

02:53.320 --> 02:58.530
So the data will keep track of what's happening in the application.

02:58.540 --> 03:05.440
The dispatcher basically will take the comments that you send, for example, stat and will dispatch

03:05.440 --> 03:10.960
them to the correct callback function that we are going to write now.

03:11.770 --> 03:19.330
So for each keyword that you want the bot to respond to, you can add a command handler so we can do

03:19.330 --> 03:33.910
DISPATCHER Dot add handler and then use the command handler, command handler with first duty world.

03:34.270 --> 03:36.220
So the keyword will be start.

03:36.220 --> 03:41.840
For example, this is a common keyword that we will use for every bot to start with.

03:41.860 --> 03:44.950
And then let's say start handler.

03:45.490 --> 03:48.010
This is the function we will need to create.

03:48.310 --> 03:50.320
I closed the two parenthesis here.

03:50.680 --> 03:55.180
So while the code here, there is very nothing to understand.

03:55.180 --> 03:55.930
This is just like that.

03:55.930 --> 04:01.540
You create a data and then a dispatcher and you add for each keyword you want.

04:01.570 --> 04:05.890
You add a command handler with add handler, and that's going to be the same every time.

04:06.610 --> 04:09.730
And now that we have this, well, we need to create our.

04:09.730 --> 04:11.080
So let's create it.

04:11.470 --> 04:17.020
E we need to create def start handler.

04:17.820 --> 04:25.350
So we need to create the callback function that will be called when we send the stat command on the

04:25.350 --> 04:26.310
application.

04:26.700 --> 04:34.350
And you are going to receive two parameters which are update and context.

04:36.090 --> 04:44.670
And well, here what you can do is you can process the request and you can also actually respond to

04:44.790 --> 04:45.420
the request.

04:45.420 --> 04:48.780
So here to respond to the request where you need to do context.

04:51.040 --> 05:02.500
Dot, but can you get the bot from the context parameter dot send message you need to give to arguments,

05:02.530 --> 05:05.620
chat, ID and text.

05:05.620 --> 05:10.240
So for the chat ID you're going to do chat id is equal to update.

05:10.960 --> 05:13.000
Not effective.

05:14.840 --> 05:22.910
Chat dot ID and for the text you do text equal to and just the text you want.

05:23.600 --> 05:28.280
Let's say hello from Python.

05:30.270 --> 05:31.890
Again, how to know you need to do that?

05:31.890 --> 05:34.920
Well, that's basically what they say in the documentation.

05:35.160 --> 05:38.160
There is no really any logic here.

05:38.160 --> 05:40.290
This is just how to use the bots.

05:40.410 --> 05:47.400
So context bots send a message with the chat ID, which is basically the chat ID we got.

05:48.540 --> 05:48.960
Here.

05:49.110 --> 05:54.690
So every chat So this group chat here has an ID and this is the ID we get.

05:54.690 --> 05:59.550
So we send the message to this chat ID with this text.

06:00.150 --> 06:07.840
Now that we have the handler, we need to do a data dot start pulling.

06:08.370 --> 06:13.380
So it will start being updated and then, well, if we just leave it like this, the program is going

06:13.380 --> 06:14.090
to end.

06:14.100 --> 06:20.090
So I'm going to start with the data dot idle like this.

06:20.100 --> 06:27.420
This is going to make the program block here and continue to start pulling from the bot so that when

06:27.420 --> 06:30.720
we receive a command, it goes to the dispatcher.

06:30.720 --> 06:36.660
And then we have, for example, the START keyword, which is going to call the start handler callback

06:36.660 --> 06:37.530
function here.

06:37.530 --> 06:39.810
So let's run that.

06:40.350 --> 06:41.520
Let's run the script.

06:42.240 --> 06:46.290
Actually, maybe we can just add, let's say here the print.

06:48.700 --> 06:51.700
KG, but ready.

06:53.140 --> 06:53.770
Like this.

06:53.770 --> 06:55.030
So we have an output.

06:55.030 --> 06:57.010
So let's run that with the print.

06:58.910 --> 06:59.610
Okay, We can see.

06:59.630 --> 07:02.560
Telegram got ready, and now you can see the pram is stuck.

07:02.570 --> 07:06.890
So it's waiting for commands that you send on the chat.

07:06.890 --> 07:08.150
So now you go on the chat.

07:08.150 --> 07:13.880
So whether on the desktop or in the mobile and you do slash start.

07:15.590 --> 07:19.120
And you can see we receive hello from Python.

07:19.130 --> 07:20.990
So this is correctly walking.

07:21.380 --> 07:23.810
And you can do that as many times as you want.

07:23.990 --> 07:24.800
Start.

07:25.670 --> 07:28.340
Hello from Python If you send anything else.

07:29.660 --> 07:31.220
Anything else for example?

07:31.580 --> 07:36.980
Well, you have nothing because you don't have a command handler.

07:37.220 --> 07:43.130
And so here we start with the start keyword, which is usually what you're going to do with a bot.

07:43.250 --> 07:48.440
But then you can basically duplicate this line for every keyword that you want.

07:48.830 --> 07:54.920
So you add the keyword and then you create a callback for this keyword.

07:55.190 --> 07:59.480
And usually what I do is I just put the keyword with Handler.

07:59.690 --> 08:01.910
So that is pretty explicit.

08:01.910 --> 08:04.550
And now, well, when you run this.

08:05.770 --> 08:11.230
If you want to stop the program here, you just do control C and you wait a bit.

08:12.340 --> 08:16.360
You wait a few seconds and you can see now the program has exited.

08:18.180 --> 08:18.600
All right.

08:18.600 --> 08:24.330
And one last thing is, if you want to get more documentation, you just type Python telegram bot on

08:24.330 --> 08:29.700
Google or any search engine and you're going to find the documentation here.

08:31.080 --> 08:37.920
So you have the page, that's the page, and you can find the introduction, documentation, the API,

08:37.920 --> 08:39.690
every command that you can use.

08:40.650 --> 08:46.350
So in this course I'm going to keep things quite simple so we can reach the final project quite quickly

08:46.350 --> 08:48.360
with the minimum functionalities we need.

08:48.360 --> 08:52.110
And then you can refer to the documentation for more complex behaviors.
