So you have a telegram account, your account is correctly configured and you have created a bot with
the bot fader.
You also have the group here with at least you and the bot.
Now, in this lesson we are going to write some Python code so that the bot can respond to some comments.
So for example, if I do slash start here so nothing happens, but we are going to respond to that command
from the Raspberry Pi.
So what you're going to see when you send slash stat from the telegram application that can be from
the desktop or from your phone or from anywhere, you're going to get a response here.
Let's do that.
So we already have the token in the previous lesson.
We have gotten the token here from the bot father.
We have stored the token inside a file and now we get the token inside our Python program.
Now, to be able to write some program for the bot, we will install a python module and this python
module is named Python telegram.
But so first of all, make sure you have pip three so sudo apt.
Install python three dash pipe.
Look from.
It's already done.
And then p IP three.
Instal python dash.
Telegram.
Dash.
But.
And then you can also add dash.
Dash, upgrade.
And for me that was actually already installed.
So now that you have the Python telegram bot module, let's use it in the code.
So you're going to do from Telegram dot X to import the data and command handler.
I'm just going to explain to you what this is and what it does.
So we have the telegram token here.
I'm going to first create a data set of data is equal to data and you need to give the tokens token
is equal to.
Telegram.
Token.
So the telegram token you have read from the file.
So basically, the updater will allow this program here to well being kept updated of what's happening
in the Telegram application when you send some message to the bot.
And then on top of the data, we are going to add a dispatcher is equal to the data dispatcher.
So the data will keep track of what's happening in the application.
The dispatcher basically will take the comments that you send, for example, stat and will dispatch
them to the correct callback function that we are going to write now.
So for each keyword that you want the bot to respond to, you can add a command handler so we can do
DISPATCHER Dot add handler and then use the command handler, command handler with first duty world.
So the keyword will be start.
For example, this is a common keyword that we will use for every bot to start with.
And then let's say start handler.
This is the function we will need to create.
I closed the two parenthesis here.
So while the code here, there is very nothing to understand.
This is just like that.
You create a data and then a dispatcher and you add for each keyword you want.
You add a command handler with add handler, and that's going to be the same every time.
And now that we have this, well, we need to create our.
So let's create it.
E we need to create def start handler.
So we need to create the callback function that will be called when we send the stat command on the
application.
And you are going to receive two parameters which are update and context.
And well, here what you can do is you can process the request and you can also actually respond to
the request.
So here to respond to the request where you need to do context.
Dot, but can you get the bot from the context parameter dot send message you need to give to arguments,
chat, ID and text.
So for the chat ID you're going to do chat id is equal to update.
Not effective.
Chat dot ID and for the text you do text equal to and just the text you want.
Let's say hello from Python.
Again, how to know you need to do that?
Well, that's basically what they say in the documentation.
There is no really any logic here.
This is just how to use the bots.
So context bots send a message with the chat ID, which is basically the chat ID we got.
Here.
So every chat So this group chat here has an ID and this is the ID we get.
So we send the message to this chat ID with this text.
Now that we have the handler, we need to do a data dot start pulling.
So it will start being updated and then, well, if we just leave it like this, the program is going
to end.
So I'm going to start with the data dot idle like this.
This is going to make the program block here and continue to start pulling from the bot so that when
we receive a command, it goes to the dispatcher.
And then we have, for example, the START keyword, which is going to call the start handler callback
function here.
So let's run that.
Let's run the script.
Actually, maybe we can just add, let's say here the print.
KG, but ready.
Like this.
So we have an output.
So let's run that with the print.
Okay, We can see.
Telegram got ready, and now you can see the pram is stuck.
So it's waiting for commands that you send on the chat.
So now you go on the chat.
So whether on the desktop or in the mobile and you do slash start.
And you can see we receive hello from Python.
So this is correctly walking.
And you can do that as many times as you want.
Start.
Hello from Python If you send anything else.
Anything else for example?
Well, you have nothing because you don't have a command handler.
And so here we start with the start keyword, which is usually what you're going to do with a bot.
But then you can basically duplicate this line for every keyword that you want.
So you add the keyword and then you create a callback for this keyword.
And usually what I do is I just put the keyword with Handler.
So that is pretty explicit.
And now, well, when you run this.
If you want to stop the program here, you just do control C and you wait a bit.
You wait a few seconds and you can see now the program has exited.
All right.
And one last thing is, if you want to get more documentation, you just type Python telegram bot on
Google or any search engine and you're going to find the documentation here.
So you have the page, that's the page, and you can find the introduction, documentation, the API,
every command that you can use.
So in this course I'm going to keep things quite simple so we can reach the final project quite quickly
with the minimum functionalities we need.
And then you can refer to the documentation for more complex behaviors.