Okay, great.
You can send notification from the Python program to the chat ID and you can also add some callbacks
so you can respond to some comments.
But now let's mix those two.
Okay.
So we can have a program that sends independent notifications and that also responds to comments here.
So I'm going to show you the program structure we're going to use.
And this is also something we're going to use for the next activities and for the final project.
So I'm going to start I'm not going to start from scratch.
I'm going to start from this Telegram bot program here, which is already working.
And I'm going to add simply this into that program.
And so what we do here is we create some handler, okay, with an updater that's going to keep an eye
on the chat, a dispatcher that's going to dispatch any comment you receive to the correct handler.
We start polling and then we use idle to stop the program here.
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.
Okay.
So basically we are going to use an infinite loop just like we did before.
This is kind of the void setup.
And then we are going to create a void loop like we have on the Arduino.
And I'm going to do while true.
Okay.
And this is where we are going to do our program and for example, send some notifications.
So if we want to send some notification, I'm going to do this from Telegram import bot, I'm going
to put it there and I'm also going to create a bot.
Okay, so we already have the token.
Okay.
So create a bot here just before the updater for example.
So we have the bot and the updater.
And for each we give the telegram token.
Okay.
I'm also going to do this chat ID, okay.
And put it there so we can use the chat ID with the bot.
We could use this chat ID actually here so that any time you send a comment to the bot, we're going
to respond to this group.
But I'm going to keep it like this.
So basically this means that if you have another group or if you just send a message directly from your
user to the bot, well by using updated active chatted, you're going to respond to whoever is sending
the message, whoever is sending start.
Okay, So from this group or from another group or from your user, if you use this chat ID well, every
time you receive something, you're just going to send back a message.
But to this group.
So if you send a command from another group, you are still going to reply to this group instead.
Okay, so we're going to keep this.
So we directly respond to the one who sends the comment.
I'm going to remove this print here and then, well, we still have this chat ID, so when we send a
notification, however, that's always going to be in this group.
And so now let's say we want to send a notification every 10s for example, what we can do is do bot
dot send message with chat ID is equal to chat, ID and text.
Let's put Hi.
Something new.
And let's add a time.
I'm going to add a time dot sleep here for, let's say 10s.
Let's import time.
Okay.
So here, no problem.
We can use Time.sleep.
So we are not going to use that in the future if we want to do many things in the whiletrue.
But for this simple example, if we just want to send a message every 10s, that's okay.
And that's not going to be a problem for the updater because this is going to pull the telegram chat
in a different thread.
Okay, so the start handler is going to be in a different thread, not on the same thread as this.
So this is not a problem.
We can block the application.
The polling for the updater is still going to work, but then, well, the program is still not complete.
Why is that?
Because now when we start polling and then we do this and if I do control C, I'm going to exit from
the application.
But first I need to close the updater.
Okay?
Just like when you use Serial, you need to close serial communication here.
We need to close the updater with the function idle that was already done for you.
But now we need to do this.
So I'm simply going to do what we did before is to put this while true inside a try.
Except.
We've keyboard interrupt and when we have the keyboard interrupt, I'm going to do a data dot stop.
Okay.
We use the stop function on the update.
This can take some time.
So we are also going to print print stopping data.
These.
Wait a few seconds.
Okay.
So we know we have to wait a bit and not price controls in multiple times.
And let's also add a print here.
Send message so it's easier to debug.
Okay.
Now let's run that code.
Okay, so we have telegram bot ready.
We can start to send some commands with start.
We receive hello from Python and we also every ten second receive send message.
So we actually receive hi something new and we have send message here on the terminal.
Let's wait a bit.
Okay.
Send message.
You can see.
Hi.
Something new.
And we have also received a notification on my phone and on the desktop app.
And so you can see.
Hi.
Something new.
I can send a command, receive responses from the callback and I can also receive independent notifications,
all that at the same time.
Now I can go there, press Ctrl C stopping a data please wait a few seconds.
So we wait.
And the updater is now stopped and the program exited.
Okay, great.
Now you have a complete Python program structure to communicate between your Raspberry Pi and a telegram
chat.