Home Covid Booster Vaccine Notifications to WhatsApp
Post
Cancel

Covid Booster Vaccine Notifications to WhatsApp

…since the government has announced free booster doses for 18+ age category

Straight to the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import requests
from datetime import datetime, timedelta
import json
import time

terminate = False
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
notified_session_ids = []
wait_time = 80
file_location = "/booster_covaxin/notified_session_ids.txt"

def send_whatsapp(w_message, vaccine, session_id):
    global notified_session_ids
    w_url = "https://graph.facebook.com/v13.0/<phone_id>/messages"
    w_headers = {"Authorization": "Bearer <token>","Content-Type": "application/json"}
    w_data = { "messaging_product": "whatsapp", "to": "919xxxxx7", "type": "template", "template": { "name": "vaccine", "language": { "code": "en_US" }, "components": [
        {
        "type": "header",
        "parameters": [
          {
            "type": "text",
            "text": f"{vaccine}"
          },
        ]
      },
      {
        "type": "body",
        "parameters": [
          {
            "type": "text",
            "text": f"{w_message}"
          },
        ]
      }
    ]}}
    w_response = requests.post(w_url, headers=w_headers, json=w_data)
    if w_response.status_code == 200:
        print("JSON Response ", w_response.json())
        notified_session_ids.append(session_id)

def process_the_sessions(responseJson):
    #available centers on a particular day
    global notified_session_ids
    for session in responseJson['sessions']:
        if session['min_age_limit'] > 16 and session['fee_type'] == 'Free' and session['vaccine'] == "COVAXIN" and session['session_id'] not in notified_session_ids:
            send_whatsapp(f"*{session['name']}* is available on - _{session['date']}_", session['vaccine'], session['session_id'])
            time.sleep(2)

if __name__=="__main__":
    with open(file_location) as f:
        notified_session_ids = f.read().splitlines()
    try:
        while terminate == False:
            presentday = datetime.now()
            if presentday.hour in range (13,24):
                start_from = 1
                end_at = 21
            else:
                start_from = 0
                end_at = 20
            for i in range (start_from,end_at):
                tomorrow = presentday + timedelta(i)
                dateforvaccine = tomorrow.strftime('%d-%m-%Y')
                jsonurl =  "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id=304&date="+dateforvaccine
                print (f"{jsonurl}")
                try:
                    responseJson = requests.get(jsonurl, headers = headers, timeout=5).json()
                    #print (responseJson)
                    if 'sessions' in responseJson:
                        if len(responseJson['sessions']) > 0:
                            process_the_sessions(responseJson)
                        else:
                            print ("no centers to process")
                except Exception as e:
                    print (str(e))
                    print ("Exception! There is something wrong with the response from the CoWIN API")
                    if len(notified_session_ids) > 0:
                        with open(file_location, 'w') as f:
                            for session in notified_session_ids:
                                f.write(f"{session}\n")
                        time.sleep(1)

            if len(notified_session_ids) > 0:
                with open(file_location, 'w') as f:
                    for session in notified_session_ids:
                        f.write(f"{session}\n")
            now = datetime.now()
            curr_time_string = now.strftime("%H:%M:%S, %d/%m/%Y")
            print (f"Completed the operation at {curr_time_string}")
            next_time = now + timedelta(seconds=wait_time)
            next_time_string = next_time.strftime("%H:%M:%S, %d/%m/%Y")
            print (f"Next operation at {next_time_string}")
            print("------------------------------------------------")
            time.sleep(wait_time)
    except KeyboardInterrupt:
        print ("Closed by user manually.")
        if len(notified_session_ids) > 0:
            with open(file_location, 'w') as f:
                for session in notified_session_ids:
                    f.write(f"{session}\n")
            time.sleep(1)
This post is licensed under CC BY 4.0 by the author.