Automate CoWin Vaccine Notifier using Rasa Chatbot Easily <3

OVERVIEW

In this blog, you will learn,

  • How to check the slots availability for vaccine dose based on pincode of your area, district ID and location basis.
  • How to integrate rasa chatbot to the CoWin vaccine notifier app.
  • How to send the fetched details to your email ID.

Understanding the CoWin public APIs

Firstly, let’s understand that why we should know about the APIs here. The reason is that all the data will be scrapped from any website through the APIs only, by just changing the values. Now to understand the APIs that we are going to use for our today’s project let’s first go to the official website of API Setu. On this website you will get the public as well as the private API for the Co-Win. Here, we will use the public API to check the availability of the slots for the vaccine based on 3 parameters Pincode, district id and location.

Private and Public Cowin APIs

Here you can see the API is divided into 3 sections: User Authentication APIs, Metadata APIs and Appointment Availability APIs. Authentication APIs are the CoWin private APIs that supports only the POST request. Metadata APIs are the APIs that will provide you the information about the state and the districts so that you can fetch the results accordingly through the CoWin public APIs. Appointment Availability APIs are the public APIs that supports the GET request and you can fetch all the details related to the appointment availability for the Vaccination.

In our today’s blog, we’ll focus on the public APIs so that we can check the slots for vaccine availability, We can get the slots availability information in three different forms,
1. Pincode Based
2. District ID Based
3. Location Based

1. Pincode Based

To get the slots availability based on pin code, you have to check for the first CoWin public API as shown here.

To generate the API for pincode based, select the above API and click on “Try it out” as shown above. Then you will be asked for the pincode and the date, enter it and click on execute. It will generate the API for you. The example for the API is as given below.

https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=110052&date=31-05-2021

Now in the above API you can see that we have two values pincode and date passed in the API format. Now if I’ll change the value of the pincode and date in the API then you will get the json data from different area in the given format.

{
  "sessions": [
    {
      "center_id": 1234,
      "name": "District General Hostpital",
      "name_l": "",
      "address": "45 M G Road",
      "address_l": "",
      "state_name": "Maharashtra",
      "state_name_l": "",
      "district_name": "Satara",
      "district_name_l": "",
      "block_name": "Jaoli",
      "block_name_l": "",
      "pincode": "413608",
      "lat": 28.7,
      "long": 77.1,
      "from": "09:00:00",
      "to": "18:00:00",
      "fee_type": "Paid",
      "fee": "250",
      "session_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      "date": "31-05-2021",
      "available_capacity": 50,
      "available_capacity_dose1": 25,
      "available_capacity_dose2": 25,
      "min_age_limit": 18,
      "vaccine": "COVISHIELD",
      "slots": [
        "FORENOON",
        "AFTERNOON"
      ]
    }
  ]
}

Now, we’ll use this API further and will write a program for it to hit the GET request on the given CoWin public API to fetch the json result for it. Below is the code for hitting a GET request on the basis of pincode.

def Dose_Availability_Pincode(pincode, date):
    api = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode={}&date={}".format(pincode,date)

    return main_task(api)

def main_task(api):
    response=requests.get(api)
    data=response.json()['sessions']
    output="*"*30
    for area in data:
        if area['available_capacity']>0:
            output+="  Hospital Name:" + area['name'] + "*"*30 +"\n"
            output+='''\
Address: {}
Pincode: {}
available_capacity_dose1 : {}
available_capacity_dose2 : {}
available_capacity : {}
min_age_limit: {}
Time Slots: {}
        
'''.format(area['address'],area['pincode'],area['available_capacity_dose1'],area['available_capacity_dose2'],
                             area['available_capacity'],area['min_age_limit'],str(area['slots'])[1:-1])
            output+="*"*30
    return output

So, in the above code you can see we have two functions: one is to generate the CoWin public API based on the pincode and date. The other one is the main_task that is generating the final string with the details of all the availability slots in different hospital near to your location in a proper format.

2. District ID Based

Now the same thing we have to do to generate the CoWin public API based on the district id. Below are the steps fro it to generate the API.

So with the above steps you will be able to generate the CoWin API based on the district ID. Now the point is that how do I know that what is the ID of my district. To get that firstly you need to know about the state ID and then with that state ID you will generate the an API that will provide you the district ID and its shown below here,

Now, I hope you have understood that how you can get the district ID and based on that you can get the slots availability information for Vaccine doses. Below is the code to fetch the json data on the basis of district ID and date.

def Dose_Availability_District(district_id,date):
    api="https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id={}&date={}".format(district_id,date)

    return main_task(api)

Here, the main_task function is the same as above.

3. Location Based

Now let’s understand the last one to generate the CoWin public API on the basis of location(latitude and longitude). To generate the CoWin public API follow the steps below,

With the above steps you will be able to generate the CoWin API for the based on the longitude and latitude. Now if you dont know any thing about the pincode or the district id then you can now easily check the slots availability based on your current location and i think you know that how to easily check the longitude and latitude of any location through google map. So, with those values you can get all the details for the slots availability. Below is the code based on location.

def Dose_Availability_Lon_Lat(Lattitude,Longitude):
    api="https://cdn-api.co-vin.in/api/v2/appointment/centers/public/findByLatLong?lat={}&long={}".format(Lattitude,Longitude)
    return main_task(api)

Now we have all the code ready with us. So now we can fetch the slot availability based on any of it.

Full Code:

import requests

def Dose_Availability_Lon_Lat(Lattitude,Longitude):
    api="https://cdn-api.co-vin.in/api/v2/appointment/centers/public/findByLatLong?lat={}&long={}".format(Lattitude,Longitude)
    return main_task(api)

def Dose_Availability_District(district_id,date):
    api="https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id={}&date={}".format(district_id,date)
    return main_task(api)

def Dose_Availability_Pincode(pincode, date):
    api = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode={}&date={}".format(pincode,date)

    return main_task(api)

def main_task(api):
    response=requests.get(api)
    data=response.json()['sessions']
    output="*"*30
    for area in data:
        if area['available_capacity']>0:
            # for (field,value) in area.items():
            #     print(field,':',value)
            # else:
            output+="  Hospital Name:" + area['name'] + "*"*30 +"\n"
            output+='''\
Address: {}
Pincode: {}
available_capacity_dose1 : {}
available_capacity_dose2 : {}
available_capacity : {}
min_age_limit: {}
Time Slots: {}

'''.format(area['address'],area['pincode'],area['available_capacity_dose1'],area['available_capacity_dose2'],
                             area['available_capacity'],area['min_age_limit'],str(area['slots'])[1:-1])
            output+="*"*30
    return output

Also check out this video for more clarification,

Integration of Rasa Chatbot to the CoWin Vaccine Notifier

Now, that we have the complete code ready with us for the slots availability. Now let’s integrate the above features with our chatbot, so that instead of doing it manually our chatbot will work as an assistant and will do it for us.

Features of our Chatbot:
1. Checks Slot availability for vaccination based on pincode
2. Checks Slot availability for vaccination based on district ID
3. Checks Slot availability for vaccination based on location
4. Sends you the complete details via email on to your email ID.

In our previous blogs, we have already seen and had a hands-on experience on building our own chatbot with RASA. So you can check our playlist here for Video content or Documentation for detailed explanation and demonstration on that. For now I already have a complete Rasa Chatbot project with all the features added to it. You can download the full code here.

Rasa CoWin Vaccine Notifier

Once you have the code with you, now create a virtual environment for your project and install all the dependencies through the requirements.txt file into the virtual environment with the given command.

pip install -r requirements.txt

Once you have your project setup and ready. Now start the rasa server(with/without UI) and action server to talk to the bot and see you Chatbot assistant in action. Here arer the given commands to start the action server and the rasa server. Run both of them in a seperate terminal.

rasa train && rasa x

rasa run actions

Once everything is up and running, Now talk to the bot and get the slots availability and get vaccinated with less risk and minimum stepping out from your home.

So, in the above output you can see, how you chatbot is helping you as an assistant and providing you the complete detailed result via email on your email ID.

Time to wrap up now. Hope you liked our content on How to Automate CoWin Vaccine Notifier using Rasa Chatbot. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more Rasa Chatbot contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Stay Home and Stay Safe.

Ashish Saini
Ashish Saini

I am a software developer for Python/Machine Learning. Also, I’m Rasa Hero at Rasa. Also, I’m a youtuber where I regulary uploads the videos to help this generation learn about the technology just like they are playing games.

One thought on “Automate CoWin Vaccine Notifier using Rasa Chatbot Easily <3

Leave a Reply