This is step number seven of the final project.
And in this step, we are going to send a message along with the photo we've just taken on the Telegram
app whenever someone has requested to open the door so we already have the code to receive the button
pressed message from the Arduino.
We know this is an open door request and so we take a photo.
Now let's initialize the telegram bot to send that photo and a message.
And so I'm not going to set up the bot here, okay?
If you haven't set up a bot and if you haven't created the group.
So I'm going to also use the Telegram app from the Raspberry Pi or from your phone.
Okay.
It doesn't matter.
So if you don't have a bot, if you don't have already a token saved on the Raspberry Pi, and if you
don't have a group chat with the bot, then please come back to the lesson on how to set everything
up.
Okay.
And now I'm going to here import telegram in the code.
So I'm going to import the bot from telegram import bot and where are we going to initialize the bot
where we initialize serial, we initialize the camera and then we wait.
Let's just initialize the bot here.
So init telegram bot and we can do print.
It.
Telegram bot.
We've bought is equal to but token.
So we need to give a token and to get the token we are going to get it from where we have saved it before.
So we've open slash home slash by slash dot local slash share slash dot telegram bot token.
So if again you don't understand this, please go back to the lesson on how to create a token and how
to save it on the Raspberry Pi with a for reading.
So as if and we do telegram token is equal to f dot read dot strip.
Okay.
So we don't have any special character at the end and we do then bot is equal to bot with token is equal
to telegram token.
All right.
I'm not going to do the updater now.
We're going to do that in the next step.
And also what I'm going to do before I save is to create a new file for the step.
Okay?
So don't save that one.
Do you want to save?
No, but I'm going to save this as.
Intercom.
Step seven.
All right.
And let's continue where we were.
So here we create our boat so we can send message from the boat.
And to send message, we also need the chat ID What is the chat ID of our group here?
Well, I'm going to come back to one of the previous Python file where we do send notification and we
have the chat ID, so that is the chat ID.
That I got.
Okay from this group.
And again, if you don't know how to get that, go back to the lessons and the telegram bot.
And so chat ID is that one.
Okay, now we can send messages from this bot to the chat group here and let's do that actually.
So when we have opened or request taking a photo and I'm going to add another log and sending it to
Telegram.
So we do camera dot capture and then we're going to send a message, but not send message.
Chat ID is equal to chat.
ID and text is equal to let's say someone is ringing the bell.
We are also going to send the photo.
So to send the photo we need to open it because we capture it.
But then we need to open the file with open so we have the image file name already.
We are going to use our permission as photo and we do bot dot send photo with chat id is equal to chat
id the one we have and photo is equal to the photo we have here and we're going to receive.
Someone is ringing the bell plus the photo.
What we can also send is well, some instructions on what to answer to that.
Okay.
Because if you just say someone is ringing the bell, well, thank you for the information, but what
do I do next?
What you can send is the command to reply if you want to open or deny access.
So bot dot send message chat id is equal to chat.
ID text is equal to.
And then let's go back to.
The protocol we have.
And you can see what we want is that the user on Telegram sends open or deny.
So why not just saying to the user, send slash open or slash deny?
And by doing this, it's actually great because well, on the application you can just click, you will
be able to click on that, you will see and that's automatically going to send the comment.
Okay, so we're going to see how to open or deny just in the following steps.
Now let's run that code.
So the code is still running on the Arduino.
Let's run it here.
Let's run the python.
Waiting for three seconds.
Okay.
Main loop is starting.
I'm going to open.
This.
Okay, so let's press on the push button.
You can see open door request taking a photo and sending it to telegram.
And a few seconds after we have, someone is ringing the bell.
So this first message and then the photo and then send open or deny.
And you can see that's what I was talking about.
You can directly click so you can click from your phone or from the desktop, for example, open or
deny.
So here it doesn't do anything because we don't have the handler.
Of course, that's what we're going to do next.
And before we continue, I'm going to improve the code a little bit.
Why is that?
Because here, for example, if I press on the button, let's say three more times.
You can see we have a lot of open door requests.
So actually priced more than.
Three times and you can see where we receive many upon our request.
And that's kind of going to flood our conversation.
Okay.
That's going to completely spam the conversation and we're going to receive so many notifications.
So if someone is spamming on the button, well, you're going to receive hundreds and hundreds of messages.
So what I'm going to do is I'm going to do first I'm going to say that we are only going to take a photo
and send it to Telegram.
If five seconds have passed between now and the last time we did this.
Okay.
So basically, I'm going to add another delay on the button pressed command of five seconds.
So we only process the command every five seconds.
So if you spam on the button, we're still going to receive many button press command, but we're only
going to execute it.
We're only going to go in the if every five seconds max.
And so I'm going to add a new global variable here at the beginning.
For example, here to say last time button pressed is equal to let's initialize it to time dot time.
So the current time and let's say button pressed delay is equal to five seconds.
So let's put seconds here so we know it's seconds.
Okay.
So then if you want to modify the delay, you just modify this variable here and then well, it's quite
simple actually in this.
So we'll actually, when we receive the message, button pressed is correct.
And if it's correct, we do another if if time dot So let's actually create time now is equal to time
dot time.
So if time now minus let's go back here, make sure we have the same last time button pressed.
Here he is greater or equal than the button pressed delay.
In that case, we're going to do this.
I'm going to put this inside the if.
And we are also going to update the last time button pressed with time dot now.
So now let's run that again and just look at the logs here.
Okay.
Main loop is starting and I'm going to press on the push button and I'm going to continue to press on
the push button.
But you see, we only have one open door request.
And if I continue, well, another one.
But that is five seconds after.
Okay.
And so we have received here not so many.
We have received two new notifications, but not 20.
And well, when looking at this, we can still improve because when the user has pressed on the push
button and you have the text, please wait for a few seconds.
What we can decide is to not send any other notification to Telegram.
Okay, just one.
When we press on the push button and then it's going to wait for the answer from telegram.
Okay?
Because in our situation now, someone can still spam on the push button and you're going to receive
a new notification every five seconds.
So I'm going to add here a new.
Flags or boolean that I'm going to name.
Open door request is equal to false at the beginning, of course.
And so when we receive the open door request, what we do actually is to say open door request is equal
to true.
So now the flag is true.
And what we're going to do is we're going to add something in this.
If I'm going to put if and and another condition here, for example, let's put parentheses so it's
a bit clearer, not open door request.
So when you press on the push button and you have the screen, please wait for a few seconds.
If you press again on the push button one second after or 10s or 20s after, it's not going to take
a new photo.
It's not going to send anything.
And so here we are blocking the code.
But what we're going to do next is that when the here when we send open or deny from Telegram, we are
going to release this.
So we are going to put the flag open door request to false.
And so we give another chance for the user to do an open door request.
Okay.
So with this mechanism, we are sure that we are going to receive just one notification for one user.
And so later on, if we just, for example, accept or deny very fast, we are not going to receive
more than one notification every five seconds.
So this way we protect a little bit our telegram chat.
Okay.
Because you don't want to flood your chat.
Let's run this.
And okay, we have an error here.
Invalid syntax.
Well, it's maybe better if I put a backslash here for multi-line.
If.
And waiting for three seconds.
Okay.
Main loop is starting.
Press on the push button.
Let's look at the log open door request.
Okay.
We have notification.
Okay.
And then press again.
I press again.
Nothing is happening.
I press again and no more than five seconds have passed.
But still nothing is happening because now we are waiting for the response from Telegram.
And that's what we are going to do now.
So Ctrl C and that's the end of the step number seven.