Building a Telegram Bot for Cowin Shot Slots Rajesh Rajamani · Follow Published in
CloudForDummies · 5 min read · 💶 May 13, 2024 -- 1 Listen Share
Cowin Shot Slot
availability chase has become an extremely competitive space with the gap 💶 in supply and
demand in India . Several techies are doing their best to educate and empower people
with one 💶 of the largest vaccination drive in the world’s 2nd most populous
country.
Here is my recipe to provide timely alerts to 💶 those in search for a CoWin slot
.
Assumptions: I assume you have some idea about oth AWS Lambda , Python 💶 programming
and Telegram to proceed further.
Remember to read the last section of this
article
Architecture
Architecture
I was motivated to use AWS Lambda 💶 given the
simplicity of deploying the code and being able to scale up and scale down .
Telegram
was my choice 💶 to broadcast the messages given the flexibility it has in creating a bot
, channel and the “200,000” subscribers limit 💶 to any broadcast channel you create.
That’s convenient.
Step 1: Bot Creation with Telegram
If you have not already signed up
for 💶 Telegram , I think its time you do one . Proceed after you have created your
Telegram ID and signed 💶 up.
Telegram provided the ultra-simple way to create your own
bot . You can chat with “@botfather“ a specialized Telegram Bot 💶 that can assist you
.
First , open up your Telegram app on phone and search for “ @botfather
”
botfather
Click “Start” 💶 to communicate
communicated started with botfather
Now we
want to create a newbot . Use the command “/newbot” and proceed with the
💶 process.
Ensure the following.
a. Select a unique name for your bot
b. Get the
Authorization token ( save it some where we 💶 need it shortly )
c. Set up a Description
for your bot
Step 2: Channel Creation with Telegram
A Bot account can be 💶 used to
respond to messages ( essentially reply ) . However that’s not convenient if we have to
transmit messages 💶 to large audience . In such situations it’s useful to use “Channels”
. So let’s create one.
New Channel
Name your Channel
Once 💶 the channel is created , you
need to add the Bot created in step 1 as an administrator to enable 💶 sending broadcast
messages .
It’s such a good idea to limit the rights the bot will have for this
channel.
Step 3: 💶 Create Lambda Function
I’m not going to deep dive about how to create
the Lambda Function .
Note : The script I 💶 have provided may not be the most optimized
one . I have purposefully left it generic for customization . Please 💶 feel free to use
it as you please.
Things to note while creating the Lambda Function
Ensure to create
your Lambda Function 💶 in ap-south-1 “Mumbai Region” Ensure to keep your execution
trigger to not exceed 100 API calls within a 5 minute 💶 interval .
The lambda function
has 2 critical steps
a. Fetching data from the Cowin API for the district / districts
defined 💶 in the districts variable ( lines 15–48 )
b. Broadcasting the message to
Telegram . ( lines 57–74 )
Observe that in 💶 line 67 I’m invoking the Telegram API with
the botapikey (parameter botapikey ) from step 1 and the channel ID 💶 ( parameter chatid
) from step 2 and the message to be transmitted ( which is essentially the records that
💶 i fetched from the Cowin API )
url =
f”//api.telegram/{botapikey}/sendMessage?chat_id={chatid}&text={finalmessage}"
St
ep 4: Schedule the Lambda Function
For this I’m using AWS CloudWatch .
Click 💶 on
Rules
Create a New Rule
Use a Cron Expression here to specify how frequently you want
to run the function. Other 💶 considerations such as concurrency , scaling can be done at
the Lambda function window.
Use Crontab.guru if in doubt .
My Bot 💶 is
broadcasting
Strategy Points to keep in mind :
The same approach is being used by
several tech enthusiasts to provide customized 💶 updates . And this means the Cowin API
Server is taking a lot of hits. Avoid throttling your requests to 💶 the API . You may end
up getting blocked interim. Set your schedule for fetching the API sensibly. Sometimes
the 💶 data can be in cache and upto 30 minutes old ( refer to the API Documentation ) If
possible group 💶 with other like minded enthusiasts to narrow down your scope of search
like a particular district , city , pincode 💶 whatever it is . Respect fair use policies
of all the involved technologies such as AWS , Telegram , API 💶 Setu . It’s a beautiful
thing that almost all of these services are free of cost . Let’s respect that 💶 and dont
get greedy with it. Secure your bot API Keys with environment variables to avoid bot ID
getting misused.
If 💶 you come across a suggestion , alternative approach , help please
shout out. Glad to help .
Inspiration :
Berty Thomas on 💶 Twitter : @BertyThomas
Thanks.