Booking an Appointment Using Selenium and Telebot in Python — Part 1

Amir Moradi
Aug 24, 2020 · 4 min read

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.

Create your bot with BotFather

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.

A schematic of all steps from getting data to check open days for booking an appointment

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 :)

The Startup

Get smarter at building your thing. Join The Startup’s +799K followers.

Amir Moradi

Written by

Ph.D. student | Passionate in Deep Learning, Reinforcement Learning, Image Processing, and Web Scraping for Data Analysis.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +799K followers.

Amir Moradi

Written by

Ph.D. student | Passionate in Deep Learning, Reinforcement Learning, Image Processing, and Web Scraping for Data Analysis.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +799K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store