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