-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (119 loc) · 3.8 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package Imports.
const Twit = require("twit");
const fetch = require("node-fetch");
// Local Imports.
const config = require("./config");
const sessions = require("./cowinapi");
// Making a Twit object for connecting to the API.
const T = new Twit(config);
// Setting up a user stream
const stream = T.stream("statuses/filter", {
track: "@YourSlots",
});
// Listening to Stream.
stream.on("tweet", tweetEvent);
// Function when tweet event is trigerred.
function tweetEvent(tweet) {
let sum1 = 0;
let sum2 = 0;
let sum3 = 0;
let sum4 = 0;
// Who is this in reply to?
let reply_to = tweet.in_reply_to_screen_name;
console.log(reply_to);
// Who sent the tweet?
let from = tweet.user.screen_name;
// Text of the tweet.
let text = tweet.text;
// Extract pin code from the text.
let pinCode = text.replace(/\D/g, "");
console.log(pinCode);
// Check if pin code is valid or not.
let isValid = validatePinCode(pinCode);
console.log(isValid);
// Convert to DD-MM-YY Format.
let date = convert(tweet.created_at);
console.log(date);
if (reply_to === "YourSlots" && isValid) {
// Get data from SETU API & send that as tweet.
const api_url = `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=${pinCode}&date=${date}`;
fetch(api_url)
.then((response) => {
if (response.ok) {
response.json().then((data) => {
data.sessions.forEach((element) => {
if (element.min_age_limit == 18) {
sum1 = sum1 + element.available_capacity_dose1;
sum2 = sum2 + element.available_capacity_dose2;
} else {
sum3 = sum3 + element.available_capacity_dose1;
sum4 = sum4 + element.available_capacity_dose2;
}
});
if (
data.sessions.length === 0 ||
(sum1 === 0 && sum2 === 0 && sum3 === 0 && sum4 === 0)
) {
let newTweet =
"@" + from + " Sorry ! No slots available in your pincode.";
// Post Tweet.
postTweet(newTweet, tweet.id_str);
} else {
let newTweet =
"@" +
from +
" There are " +
sum1 +
" dose-one slots available and " +
sum2 +
" dose-two slots available for 18 to 44 yrs.\n There are " +
sum3 +
" dose-one slots available and " +
sum4 +
" dose-two slots available for 45 yrs & above.";
// Post Tweet.
postTweet(newTweet, tweet.id_str);
}
});
} else {
console.log(response);
console.log(response.err);
}
})
.catch((e) => console.log(e));
} else if (reply_to === "YourSlots" && !isValid) {
let newTweet = "@" + from + " Your pincode looks invalid!";
// Post Tweet.
postTweet(newTweet, tweet.id_str);
}
}
// Function for Posting a tweet.
function postTweet(txt, id) {
var tweet = {
status: txt,
in_reply_to_status_id: id,
};
// Post that tweet.
T.post("statuses/update", tweet, tweeted);
// Function to make sure tweet was sent.
function tweeted(err, reply) {
if (err) {
console.log(err);
} else {
console.log("Tweeted " + reply.text);
}
}
}
// Function for validating pin code.
function validatePinCode(pinCode) {
let regex = new RegExp("^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$");
let isValid = regex.test(pinCode);
return isValid;
}
// Function for converting to DD-MM-YY Format.
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [day, mnth, date.getFullYear()].join("-");
}