cowinapi / developer.cowin

This group is created to facilitate technical and integration discussions related to cowin platform. API related contents can be obtained at API setu portal https://apisetu.gov.in/public/marketplace/api/cowin
115 stars 30 forks source link

[UPDATED] Sharing AWS based Twitter bot code to get vaccination availability notifications #75

Closed snehil closed 3 years ago

snehil commented 3 years ago

Some POC code I wrote yesterday - sharing for anyone to use

What it does? This is an AWS lambda function code (Node.js) that will query the Cowin API for available vaccines for the given district (you can change this as per your need), filters the centers down to the ones that have vaccines available for that day and then sends an Email or SMS once it is available (via an AWS SNS topic). This Lambda function can then be scheduled using AWS Cloudwatch to get hourly/daily/weekly notifications.

How to set it up? You will have to create the lambda function in the Mumbai region, create an SNS topic and add Email or SMS subscriptions to it as desired. Then update the ARN of this topic and other location details in the given code (lines 5,6,7). Make sure that the IAM user and the lambda function has the appropriate permissions to invoke SNS/lambda and that's it. You can then setup a Cloudwatch rule to trigger this lambda function on a timely basis to get updates via Email or SMS.

Here's the code https://gist.github.com/snehil/830e76ef821b7b431d8580b7230c88b3

This was meant to be a quick POC so might be a bit sloppy but works for what it was intended to do.

[UPDATE] I have updated the code to create a twitter bot that posts vaccine availability notifications on a Twitter page. This bot will start posting updates for Pune district here- https://twitter.com/punevaccinewat1 (twitter handle @punevaccinewatch). Here is the AWS lambda code in case anyone wants to use it for other districts. Feel free to use it/update as you need. You can schedule this lambda function using AWS cloudwatch. Be sure to update the code with the location ID and code, and also the twitter keys/tokens etc. You will likely also need to create a deployment package (zip file) after running 'npm install twitter' locally so that the library is available to the lambda function to interact with twitter APIs (tutorials for this are on AWS wikis and on youtube).

The tweet will look like this - "COVISHIELD (Dose 1 Qty-0, Dose 2 Qty-257, Ages-18+,45+, Dates-24/05, 25/05, 26/05, 27/05, 28/05, 29/05) available at - Sahyadri Hospital Hadapsar, Hadapsar Pune, Haveli, Pune, Maharashtra, 411028, Details here: https://bit.ly/2RH8C3x"

Here's the latest code, feel free to fork - https://github.com/snehil/CovidVaccineTwitterBotIndia

abizerlokhandwala commented 3 years ago

Hey @snehil, if you are looking for a fully functional backend using AWS Lambdas, SQS, and SES that can scale appropriately with more users, you can check this out: https://github.com/abizerlokhandwala/Cowin-Notification-Service A few friends and I have created a web-application using this: https://vaccinepost.co.in/ Do subscribe if you find it useful! :D

Nirav-Bhadradiya commented 3 years ago

@abizerlokhandwala How are you getting authToken each time ? 'Authorization': f'Bearer {os.getenv("AUTH_TOKEN")}'

abizerlokhandwala commented 3 years ago

@abizerlokhandwala How are you getting authToken each time ? 'Authorization': f'Bearer {os.getenv("AUTH_TOKEN")}'

Hey Nirav! authToken was being generated through the otp API. I noticed that we could pass the token to successfully use the protected APIs (pros: no rate limits and caching). But this token expires in ~15min and I believe there is no automated way to generate it, other then receiving the OTP on your phone and generating the token (not scalable).

It is present in my code as of now but I am not using it, the public APIs is what I am making use of right now.

Ameerak124 commented 3 years ago

@snehil Do you know how to use protected API's? and anything about secret Key

Nirav-Bhadradiya commented 3 years ago

@Ameerak124

step 1 - login with mobile number step -2 verify OTP step -3 go to developers portal ( F12 in chrome ) step -4 try scheduling your appointment step -5 search through pincode step -6 click on search button step -7 observe request in Netowrk tab in developers option step -8 get Authorization token from that request headers pass it in your request header as Authorization

On Mon, May 10, 2021 at 3:41 PM SHEIK AMEER @.***> wrote:

@snehil https://github.com/snehil Do you know how to use protected API's? and anything about secret Key

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/cowinapi/developer.cowin/issues/75#issuecomment-836509616, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA74LLJMRKQGMFQPBA27VETTM6WN5ANCNFSM44KTDGMA .

Ameerak124 commented 3 years ago

@Nirav-Bhadradiya Thanks👍. Any idea about how to get permenant auth key?

Nirav-Bhadradiya commented 3 years ago

@Ameerak124 Automate OTP verification through any automation tool, may be you can create an android app which can listen for OTP and send it to some location and your app can read from there. this is the only way to get authToken i believe. But you would not need an authToken , you can use public apis, if your are getting forbidden responses then you are making more than 100 requests in 5 mins.

Ameerak124 commented 3 years ago

@Nirav-Bhadradiya Any ideas about how to generate Otp and validate ?

Nirav-Bhadradiya commented 3 years ago

@Ameerak124 take a look at api specs from cowin - https://apisetu.gov.in/public/marketplace/api/cowin, you would see generateOTP and confirmOTP endpoints

snehil commented 3 years ago

I have updated the code to create a twitter bot that posts vaccine availability notifications on a Twitter page. This bot will start posting updates for Pune district here- https://twitter.com/punevaccinewat1 (twitter handle @punevaccinewatch). Here is the AWS lambda code in case anyone wants to use it for other districts. Feel free to use it/update as you need. You can schedule this lambda function using AWS cloudwatch. Be sure to update the code with the location ID and code, and also the twitter keys/tokens etc. You will likely also need to create a deployment package (zip file) after running 'npm install twitter' locally so that the library is available to the lambda function to interact with twitter APIs (tutorials for this are on AWS wikis and on youtube). https://github.com/snehil/CovidVaccineTwitterBotIndia

The tweet will look like this - "[24/05] COVISHIELD (Dose 1 Qty-10, Dose 2 Qty-0, Ages-45+, Dates-25/05) available at - Khiwsara Patil Hosp Thergaon, Dattraj Colony Mangal Nagar Wakad Pimpri-Chinchwad Maharashtra 411033, Haveli, Pune, Maharashtra, 411033, Details-https://cowin.gov.in"

snehil commented 3 years ago

@Ameerak124 I have not tried the protected APIs yet..

snehil commented 3 years ago

Here's the latest code, feel free to fork - https://github.com/snehil/CovidVaccineTwitterBotIndia