WEBVTT

00:00.080 --> 00:04.970
So you have seen how to respond to some comments that you send from the telegram.

00:04.970 --> 00:06.380
Chat with the bot.

00:06.410 --> 00:11.300
But now what if you just want to send a message to that group, for example?

00:11.720 --> 00:15.410
Because here we can send a message to the group when we have a callback function.

00:15.410 --> 00:20.720
So basically when we first receive a message from the group, so you need to send something from the

00:20.720 --> 00:24.590
group, get that in the Python program and send a message back.

00:24.620 --> 00:31.220
Now, what if you just want to send a message without waiting for any input from the application?

00:31.220 --> 00:33.470
Well, that's what I'm going to show you now.

00:33.710 --> 00:35.690
I'm going to create a new program.

00:37.350 --> 00:41.180
You and, well, let's just take what we need from here.

00:41.190 --> 00:43.080
So the interpreter.

00:44.510 --> 00:50.180
We are going to need also this to get the token.

00:52.300 --> 00:52.840
Okay.

00:52.840 --> 00:53.710
And now?

00:53.710 --> 00:57.660
Well, we are not going to need the updater or the command handler.

00:57.670 --> 01:04.770
We are going to simply do from telegram import bot.

01:05.600 --> 01:05.700
Okay.

01:05.950 --> 01:17.020
And we are going to create a bot is equal to bot with token is equal to telegram token.

01:17.710 --> 01:28.210
And then from that bot we can just do bot dot send message and here like we did before, we need to.

01:28.240 --> 01:30.540
So that's the same function here.

01:30.550 --> 01:33.460
We need to give the chat ID and the text.

01:33.820 --> 01:39.930
So chat ID is equal to well what is the chat ID of this group?

01:39.940 --> 01:41.200
How to know that.

01:41.320 --> 01:49.000
So you could go on internet and there are some methods to find the chat ID for this specific chat.

01:49.000 --> 01:55.640
But now that we have already a program that can receive command from the chat, what we can do is just

01:55.640 --> 02:00.920
use this program to get the chat ID and then use the chat ID in this other program.

02:01.460 --> 02:03.620
Okay, let me show you how to do that.

02:03.620 --> 02:11.210
So in the start handler that's a good place because we receive some context and some update from the

02:11.210 --> 02:11.870
chat.

02:12.050 --> 02:18.470
And as you can see here, we respond by using update dot effective chat.id.

02:18.470 --> 02:25.460
So basically effective chat is the chat you are using here to send the start command here and then the

02:25.490 --> 02:26.930
ID is the ID of this.

02:26.930 --> 02:29.720
So I'm just going to select this.

02:29.960 --> 02:36.800
I'm going to do here print update, dot effective chatted.

02:37.370 --> 02:38.990
I'm going to run that.

02:40.990 --> 02:41.920
So telegram bot.

02:41.920 --> 02:43.720
Ready and send.

02:44.140 --> 02:44.710
Start.

02:46.660 --> 02:48.400
Okay, you can see hello from Python.

02:48.400 --> 02:57.010
And we also have here the chat ID So for a group, usually you will have something that starts with

02:57.340 --> 02:57.910
a dash.

02:57.910 --> 02:58.240
Okay.

02:58.240 --> 03:04.780
So minus sign with the chat id so you can just copy this.

03:05.870 --> 03:08.030
And then go in the other program.

03:08.390 --> 03:11.150
Let's create a variable here.

03:11.390 --> 03:17.090
Chat ID is equal to this, so we keep it here.

03:17.540 --> 03:20.200
Actually, we don't just keep it as an integer.

03:20.210 --> 03:21.070
This is a string.

03:21.080 --> 03:21.310
Okay?

03:21.320 --> 03:28.310
So we make sure this is as a string and then chat ID is equal to chat ID, so no problem with this.

03:28.310 --> 03:33.200
This is the name of the parameter and this is the argument that we give.

03:33.530 --> 03:40.250
So we give the chat ID and then text is equal to let's use something different.

03:40.280 --> 03:41.690
Let's send some French.

03:41.690 --> 03:42.320
Bonjour.

03:44.150 --> 03:46.160
And let's just add a print here.

03:46.190 --> 03:46.730
Okay?

03:46.730 --> 03:49.490
So we can know from the Python program that we have sent something.

03:49.490 --> 03:52.580
So send message to chat.

03:54.080 --> 03:57.080
Let's run this and let's look at the chat here.

03:57.710 --> 04:00.350
Run script, let's name it.

04:00.380 --> 04:02.420
Send notification.

04:05.880 --> 04:06.120
Okay.

04:06.150 --> 04:08.880
Send message to chat and you can see bonjour.

04:08.880 --> 04:12.600
And we also have the notification here for the desktop application.

04:12.600 --> 04:17.250
If you are using your phone, you should also receive a notification.

04:17.250 --> 04:21.150
I just received a notification on my phone for this chat.

04:21.360 --> 04:21.630
Okay.

04:21.630 --> 04:26.970
If you don't have the notification on your phone, make sure you have correctly allowed notification

04:26.970 --> 04:30.210
for Telegram, but this is specific to your phone.

04:30.240 --> 04:30.480
Okay.

04:30.480 --> 04:39.330
Now you can see we can both respond to comments that we send from the chat and we can also send independent

04:39.330 --> 04:44.100
messages directly to the chat from a Python program.
