Here I will explain how you can create a telegram bot using which you can send covid slots update on a telegram channel.
The whole post will be of two part, first we will see how can we create a bot and send a message to a telegram channel.
How to send a message on telegram channel using a bot
Steps :
- create your bot, by sending a message toย @botfather
- add the bot to your channel.ย stackoverflow
- Get channel id by
curl https://api.telegram.org/bot<bot_token>/getUpdates| python -m json.tool
Replace bot_token with token generated from step 1.
You need to see id from chat section such as
"chat": {
"id": xxxxxxxxxxx,
"title": "Test_call",
"type": "channel"
},
Write a code to send message to your channel usingย bot API , for simplicity you can use following function
import requests
def sendToTelegram(message,chat_id,bot_token):
response = requests.post(
url=f'https://api.telegram.org/bot{bot_token}/sendMessage',
data={"chat_id": chat_id, "text": message, "disable_notification": True }
).json()
Note
: You can use bots to send message to channels. But there is some limitation, like size of files.
Now let's move to the second part of post where we will see how to use co-win api and how we will format output based on our requirements.
Use Co-win API to find the available slot
If you refer to cowin site then you will get there are 3 ways to search for slots
By District
We will call cowin API to get slot by district
# Let's read from cowin
headers = {
'authority': 'cdn-api.co-vin.in',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
'origin': 'https://www.cowin.gov.in',
'sec-fetch-site': 'cross-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.cowin.gov.in/',
'accept-language': 'en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7',
}
params = (
('district_id', district_id),
('date',date),
)
response = requests.get('https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict', headers=headers, params=params)
By PIN
This will be similar to above request , you just need to update params and API path
params = (('pincode', pincode),
('date',date),
)
response = requests.get('https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin', headers=headers, params=params)
On the Map :
We are not going to cover this
Once you get the data from co-win api you need to parse it , and change it as per your requirements.
The data cowin api will give have both free and paid slots. Also it will include all type of vaccine(covishield, covaxin etc). So you may want to filter it out based on your requirement
import pandas as pd
result_df = pd.DataFrame()
# First check if response is correct or not
if response.status_code == 200:
dict_response = json.loads(response.text)
if 'centers' in dict_response :
df = pd.DataFrame(dict_response['centers'])
# Here I'm applying filters
df = df[df.fee_type == "Free"]
result_df = result_df.append(df, ignore_index = True)
else:
print(dict_response,type(dict_response))
else:
# If api request failed
print(response.status_code,response.text)
Now Create a message from above output which you wants to send on telegram channel
age = 18 # Since I want to select slots where minimum age is 18 not 45
message = ""
for center_id, df in result_df.groupby("center_id"):
for index, row in df.iterrows():
available_capacity1 = 0
available_capacity2 = 0
for s in row["sessions"]:
if s["min_age_limit"] == age:
available_capacity1 = s["available_capacity_dose1"]
available_capacity2 = s["available_capacity_dose2"]
if available_capacity1 + available_capacity2 > 0:
message+="Dose 1: %s,Dose 2: %s, Slot Available at %s, pincode %s\n"%(available_capacity1,available_capacity2,row['name'],row['pincode'])
# chances are there no slot available
if len(message) > 0:
return message
Application
Using above snippets you can create and send alerts on your mobile as soon as any slot is available in your area.
Using above code I created few channels , which helped more than 6000 peoples to find the slots as soon as possible because of timely notification.
Here are few screenshots of my channels
Top comments (0)