In the previous part, we saw how to set up Telegram BOT and Telegram Channel.
In this article, we will see how to set up the code on your local machine and how to execute it periodically to get notifications on your Telegram Channel. So without wasting time, let's dive right in.
Part 2: Code setup and execution on Local system
You need the below applications installed on your system to start with the setup:
- Python 3.6 or above → Download
brew install python3
pip3 install pipenv
3. GIT → Download
Get the codebase
Once all the required applications are installed, open your command prompt/terminal and clone the VacciDate GIT repository on your local system.
- Open command prompt or Terminal in your system.
- Navigate to the folder where the codebase is present.
- Create a new pipenv using below command:
4. Install all the required dependencies using the below command:
5. Congratulations!!! Installation is complete.
Before we execute the program, make sure the two required env variables are exported. These are the BOT API Token and Channel Id which we extracted in the previous article.
- Open up the "env" file from the code base and update the Telegram Bot token in "telegram_bot_token" and Telegram Channel Id in "telegram_chat_id".
- Load the env variables using below command on your command prompt/terminal:
3. Check the values to Token and Channel Id by running below command:
4. Now execute below command to start the program:
python main.py -d <district_id>
Different modes available
There are 2 main mode in which this BOT works
The availability of vaccination slots is checked for the whole district (need to provide district id corresponding to your district from the list available in “data/district_data”).
You can fetch the district_id from “data/district_data” folder in GitHub repository
Along with the District Id, provide the Pincode of your area, and the BOT will filter the results for your specific area.
There are few more parameters using which the output result can be filtered
The age group can be selected when running the python script. Arguments such as “-a” or “- -age_group” can be used to filter results for 18+ or 45+ vaccine slots.
Pass 18 if you only want to track for 18+ availability or pass 45 if you only want to track for 45+ availability. Don't pass this parameter if you want to track both 18+ and 45+
The number of doses can also be filtered. An argument such as “-dose” can be used to filter results based on 1st Dose or 2nd Dose
Pass 1 if you only want to track 1st dose availability. Pass 2 if you only want to track 2nd dose availability. Don’t pass this parameter if you want to track both 1st and 2nd dose availability
Execute below command for more details
python3 main.py --help
- Below command will get details of 1st Dose vaccine availability in Mumbai District (District Id: 395) for 18+ age group
python3 main.py -d 395 -a 18 -dose 1
2. Below command will get details of 2nd Dose vaccine availability in Powai (Area in Mumbai) with pincode:400076 for all age group
python3 main.py -d 395 -p 400076 -dose 2
Scheduling a Cronjob
Congratulation, you have completed the code setup and tested the code executed successfully. Now comes the main part where we will schedule a cronjob that executes the python program every minute.
Internally the code is executed with a delay of 30 seconds. So if we schedule a Cronjob that runs every minute, the python program will be executed every 30 seconds.
This section is tested on macOS. For Windows, we need to look for an alternative.
- Execute the below command on your terminal.
env EDITOR=nano crontab -e
2. This will open up a “nano” editor on your terminal. Enter the below command in the editor and save by pressing ^
* * * * * cd path_to_main.py_in_code_base && export telegram_bot_token="your_BOT_api_token" && export telegram_chat_id=channel_chat_id && /usr/bin/python3 main.py -d <district_id> -a <age_group> -dose <dose_number> >> path_where_you_need_your_log_files_to_be_saved 2>&1
The above command will execute “main.py” every minute. “* * * * *” is a special command, which executes the job every minute. Refer to the documentation for more details.
3. Get the list of scheduled jobs by executing the below command.
Congratulations!!! You have set up the codebase on your local system and scheduled a cronjob that will execute your program every minute. In turn, your program will check for the Vaccine availability every 30 seconds and notify you once the vaccine is available.
The job is automated for you. But this is not a reliable solution. Your job will stop if you turn off your system or your internet goes down.
To make this solution reliable, you can put it on a cloud instance. I am using an instance from Linode. Linode provides you free credits of $100 for a period of 2 months. Open your free account on Linode from here.
In the 3rd Part, we will see how to create a free cloud instance and set up the codebase there.