In this tutorial, I want to share my experience in creating Appointment_Bot, my first telegram bot for an automation task. Here, I suppose that you are not familiar with bots and their atmosphere. Still, you know to code in python and, more important, you have ambitions in discovering the new world of web scraping and sharing the results of your analysis in some social media like Telegram. Now, I dive in code explanation, and I want to tell you why Appointment_Bot? Let’s go.
During 2019, I decided to continue my study and research as a Ph.D. student. So, I got an admission of Electrical Engineering. Meanwhile, at the beginning of 2020, the world faced a global problem, COVID 19 pandemic. If you have tried to get an appointment for Biometric, you confirm that the website is awful in its speed and showing different errors during the date booking process. This is not the whole story. On the other hand, appointment times are opened suddenly, and if you are not ready for the booking process, you will lose the battle (It means that the website is not friendly with phone’s browsers and you should have access to a laptop or PC).
With the help of *Appointment_Bot*, users open their Telegram and start the booking process in their smartphones. The bot gets the required information and logins into the user’s account. Then, it passes all steps to reach the final calendar of open days for an appointment. During the process, the bot notifies the user if it gets any error, or passes each step. Appointment_Bot uses selenium to act like a real user, from waiting between each action to click on “Submit” and “Continue” buttons.
By reading this tutorial, you will know how to
1- Create a Telegram bot using BotFather.
2- Check Gmail account for new message update.
3- Work with bs4 to extract target data from HTML and CSS code of a website.
4- Work with Selenium to execute an automated operation in the human behavior level.
5- Work with Telebot, a framework for Telegram Bot API, to handle different user’s input commands.
To start, first, you need to start a conversation with this ID: @BotFather. Next, you insert /newbot command, and BotFather creates a new bot for you. But just before you complete the process, you have to give some information about your bot, like name and username. Finally, you will receive your bot’s TOKEN id. Read this conversation between BotFather and me.
Now, you have the required data to start your coding. To have a better understanding of Appointment_Bot’s procedure, first, see the picture below.
When the user inserts /start, the message handler of Telebot finds this command and runs send_welcome function.
In this function, an inline keyboard button is created, named Booking Appointment, and whenever the user press this button, a query is sent to vac_callback function.
Here, the user must select which center wants to book an appointment, Ankara, or Istanbul. For this purpose, two inline keyboard buttons are designed for cities. If the user presses each button, a proper function is called to handle the generated query.
Also, a user class is defined to save some important parameters of each user such as password and username of the account, Gmail password if the username is a Gmail type of email and the user allows the bot to access his/her Gmail automatically.
Important tip: If you want to send a message to get a specific input from the user, it is better to use such a format. First, the bot sends a message, for example, to instruct the user how to insert a username and password; then, the input data is passed to the next message handler for further process. See example.
Next, the given username is checked, and if is a Gmail type, the bot asks the user to get the permission of his/her email access to get OTP. OTP is a one time password, which is sent by the website and has time limitation for use. Input data must be sent in separate rows, and otherwise, the bot sends a warning message.
In response, based on the user’s answer, two call_backs are called to handle queries.
In the booking function, get_captcha is called to take a screenshot of the captcha image. The bot posts captcha and waits for the user’s input. Then, it calls update_captcha to process the inserted captcha number.
Here, to find the elements of the webpage, first right click on the page and select inspect. Then, in elements section of the appeared window on the right side of the page, find your element like this.
Please read the continue in Part 2
Thank you :)