This is step number nine of the final project, and that will be the last step where we write Python
code.
After this, our Python program is going to be complete for this project.
And so what we still need to do now is to handle the deny the deny comment from Telegram.
So when we have an open door request, we can deny so that we don't open the door and we print an error
message for the user.
And as you will see, this step is going to be quite quick and quite easy now because when all the hard
work was done before.
So now we just need to add one functionality on top of what we have done.
I'm going to also do new.
And just paste that here.
Uh, let's remove the lines we don't need here.
Just one here and let's name it.
So let's save it as.
Intercom.
Step nine.
So what we need to do now is to add a callback for the deny command.
We already have the updater.
Everything is correctly set up, so we don't need to do that again.
We just need to create here another callback.
So after the open, for example, the deny access handler a date and context.
But pass And let's add let's not forget that because if you forget, that's of course not going to work.
Dispatcher Dot add Handler Command handler with deny.
Okay, let's check again.
We ask for deny.
So let's get exactly deny.
And the function is going to be deny access handler and I'm going to come back to the run async just
in a second.
So what do we want to do in this deny access handler?
Well, first, what we don't want to do is to open the door so we're not going to send open door and
we are not going to send close door.
Okay.
So because Well, I have put some print here, so let's do print.
Denying access.
And to deny the access we do send to Arduino.
So we're going to send something for the LCD screen, a sound and also put the Led to read.
Okay, So let's put three.
The first command is print text access denied.
And then the second one is going to be play buzzer.
So this time we can use, let's say 200.
So we have 300 here for 500 milliseconds.
So half a second here, when we close the door, we have 200.
So that's lower pitch with half seconds.
Now we are going to send also 200, but for one complete second.
Okay, you can experiment with different frequencies to find what you like best.
Okay.
I'm just going to keep things simple here and set led with 255 zero and zero.
Okay.
After we send those commands, we're going to wait a few seconds.
Time.sleep.
Let's wait for five seconds.
Okay?
So we can leave some time for the user to see that the access is denied.
And after five seconds we come back to the initial state.
So we will send to Arduino three times.
And what we do here?
Well, we do print, text, push on, button to call.
So the exact same message.
And then well, actually, we don't need to play the buzzer anymore because here we play a sound when
we open the door, when we close the door.
So that's a physical action or when we deny access.
But after that, no need to add another sound.
So let's do set directly with 000 and then so red, green and blue on.
And what we can do also is like we did for the open command is that when we deny access from Telegram,
we can say bot dot send message chat ID is equal to dot effective chat.
Dot id text denying access so that when we do the deny command here from Telegram, we also have a confirmation.
Okay, here we have a confirmation opening the door so we know that's happening.
And with denying access, we have a confirmation that we are denying the access.
And now, well, we're going to have the same problem as with the open door handler.
So basically here, if you don't put run async.
All the callbacks.
So all the deny access you're going to call are going to be put in a queue and are going to be executed
one by one instead of in parallel.
And you don't want that because you have a Time.sleep five here.
So if you press four times, then you're just going to do that for five seconds and then five seconds
Et-cetera So you're going to continue to run the callbacks for 20s.
So we do.
Run async also for the deny request.
And we also going to use those two flags here.
Okay.
So Global Open, the request and global handling Doe.
So we are first going to do if open door request and put that inside the if.
Why is that?
Because we don't want to deny an access if we don't have the request.
Okay.
Imagine that your intercom at your home is just printing access denied.
By itself, without anyone to request to open the door.
That's going to be a bit weird.
Okay, so if we have an open door request, well, actually, from telegram, we can send either open
or deny comment.
So we either going to go here or to go here, and then at the end we can do open door request is false.
Okay.
So then we can try again.
And also we are not going to deny the access if we have the handling door currently on.
So basically, if we are opening the door, we're not going to deny access at the same time.
Okay.
So I'm going to put not handling door.
This is just a question of logic here.
And the same thing is that if we are currently denying the access, we are not going to open the door.
We are going to wait that this action is finished to start another action.
And so I'm going to put here handling door is equal to true and here handling door is equal to false.
So even if we don't physically handle the door, we handle the door mechanism.
Okay?
We just keep it locked for five seconds.
Okay?
So if you send the deny command, you're going to have handling door is equal to true for the duration
of.
This.
So basically five seconds.
And then if you try to open the door while you are in this, that's not going to work because the handling
door is going to be true.
Okay.
So with those two flags, we make sure that we only process the comment from Telegram when we have a
request and that we only process one comment.
Either we open the door once or we deny the access once, and then we need to wait for another open
door request.
Okay.
And here we don't send requests if we are currently inside the request or if we are currently handling
the door.
All right.
So now is the time to try this.
Let's run that.
So the Arduino is still the same.
Could not connect.
Yes, if I actually plug the Arduino.
Okay.
Starting and then main loop is starting.
So let's press on the push button.
We have a notification.
Someone is ringing the bell and that someone is.
Well, we don't see that person.
So let's deny the access and let's look at what we have.
Okay.
We have a sound from the buzzer read Access Denied.
And after five seconds we have again the initial state.
So that's correctly working.
Now what you can do, you can try again and you can deny the access again or you can accept and open
the door.
ET cetera.
So let's correctly stop this program with control C Here you can see that it was quite faster.
Okay.
Depends on the updater, I guess.
And, well, our python code is now finished.
So this code is actually you can see 130 lines.
And as a quick recap, we have so we have some inputs, then some global variables.
We initialize the serial, we have a function for the serial, we initialize the camera and then we
have our telegram callbacks.
So three callbacks, we initialize the bot and the updater.
And after everything has been initialized, we wait for three seconds.
We put the initial state of the application and we start the main loop in the main loop.
Well, basically we just read from Serial, and if we have received something, we check if we have
a button pressed comment and if we have a button pressed comment, we try to send a request to telegram
with a message, a photo, and some instructions so that the user can give open or deny.
And that's going to call the telegram callback.
All right.
And that is the end of this step number nine of the final project.