-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
384 lines (326 loc) · 8.65 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
var config = require('./config.js');
var Twitter = require('twitter');
const https = require('https');
const request = require('request');
const express = require('express');
const app = express();
var client = new Twitter(config.twitter);
var stream = client.stream('statuses/filter', {track: '@BotRaju'});
stream.on('data', function(event) {
//console.log(event.text);
//console.log(event);
like(event);
Reply(event);
});
app.use(express.static('views'));
app.get('/', (req, res) => {
res.render('home');
});
function like(main)
{
var res2= {
Name : "I liked this tweet ",
id:main.id_str,
}
client.post('favorites/create', res2,
function(err, data, response) {
//console.log(data);
}
);
}
function Reply(main)
{
var text=(main.text).toLowerCase();
if(text.includes("quote") )
{
if(text.includes("not") || text.includes("donot") || text.includes("don't"))
{
SendFine(main);
}
else {
SendQuote(main);
}
}
else if(text.includes("joke") )
{
if(text.includes("not") || text.includes("donot") || text.includes("don't"))
{
SendFine(main);
}
else {
SendJoke(main);
}
}
else if(text.includes("weather") )
{
if(text.includes("not") || text.includes("donot") || text.includes("don't"))
{
SendFine(main);
}
else {
SendWeather(main, text);
}
}
else if(text.includes("-help") )
{
Help(main);
}
else
{
let reg = new RegExp("@botraju")
text=text.replace(reg,'');
if(text!=null && text.length>=2) SendDuckDuckGo(main,text);
else Help(main);
}
}
function SendFine(main)
{
var res = {
status: 'Hello '+main.user.name+', As your Wish :( ',
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
function SendQuote(main)
{
https.get('https://freequote.herokuapp.com/', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
var res = {
status: 'Hello '+main.user.name+', Here is the quote for you : \n'+JSON.parse(data).quote+' - '+JSON.parse(data).author,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
function SendJoke(main)
{
https.get('https://official-joke-api.appspot.com/random_joke', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
var res = {
status: 'Hello '+main.user.name+', Here is the Joke for you : \n'+JSON.parse(data).setup+' \n'+JSON.parse(data).punchline,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
function SendWeather(main, text)
{
const regex = /weather (?:like )?in ([A-Za-z]+)/;
const city = regex.exec(text)[1];
https.get(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${config.openweathermap.key}`, (resp) => {
resp.on('data', (d) => {
const responseJson = JSON.parse(d);
const botReply = `Hello ${main.user.name}, The current weather in ${city} is:
${responseJson.weather[0].main} - ${responseJson.weather[0].description}.
The temperature is ${responseJson.main.temp}° celsius, the atmospheric pressure ${responseJson.main.pressure} mbar and the humidity of ${responseJson.main.humidity}%.`
const res = {
status: botReply,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
function SendDuckDuckGo(main,text)
{
try
{
var url='https://api.duckduckgo.com/?q='+text+'&format=json&pretty=2';
console.log(url);
request(url, { json: true }, (err, res, data) => {
if (err) { return console.log(err); }
var AbstractURL= data.AbstractURL;
var Abstract= data.Abstract;
Abstract = Abstract.split(".")[0];
if(Abstract.length>220)
{
Abstract=Abstract.substr(0,220);
Abstract+=".. ";
}
if(data.RelatedTopics[0]==null)
{
var res = {
status: 'Hello '+main.user.name+', Sorry ! But I am still Learning, I am not sure about this ! \nTip : Do not Write who,where,whom directly write keywords for example : \nDo not Write "What is RAM", Just Write "RAM"',
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
return;
}
var FirstURL=data.RelatedTopics[0].FirstURL;
var FirstURLText=data.RelatedTopics[0].Text;
FirstURLText = FirstURLText.split(".")[0];
if(AbstractURL!="")
{
if(Abstract!="")
{
console.log(Abstract);
var res = {
status: 'Hello '+main.user.name+',\n'+Abstract+' More : '+AbstractURL,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
else if(FirstURLText!="")
{
console.log("FirstURL Text2"+FirstURLText);
var res = {
status: 'Hello '+main.user.name+',\n'+FirstURLText+' More : '+AbstractURL,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true,
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
else
{
var res = {
status: 'Hello '+main.user.name+', Here you Go : '+AbstractURL,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
}
else if(FirstURL!="")
{
console.log("FirstURL");
if(Abstract!="")
{
console.log("Abstract2"+Abstract);
var res = {
status: 'Hello '+main.user.name+',\n'+Abstract+' More : '+FirstURL,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true,
media_ids: media.media_id_string
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
else if(FirstURLText!="")
{
console.log("FirsurlText2"+FirstURLText);
var res = {
status: 'Hello '+main.user.name+',\n'+FirstURLText+' More : '+FirstURL,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
else
{
var res = {
status: 'Hello '+main.user.name+', Here you Go : '+FirstURL,
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
}
else
{
var res = {
status: 'Hello '+main.user.name+', Sorry ! But I am still Learning, I am not sure about this ! \nTip : Do not Write who,where,whom directly write keywords for example : \nDo not Write "What is RAM", Just Write "RAM"',
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
});
}
catch(e)
{
console.log(e);
}
}
function Help(main)
{
var res = {
status: 'Hello '+main.user.name+', You can Ask me a Joke, Quote, Weather or You can get information about anything... \nTip : Do not Write who,where,whom directly write keywords for example : \nDo not Write "What is RAM", Just Write "RAM"',
in_reply_to_status_id: main.id_str,
auto_populate_reply_metadata:true
};
client.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
//Set The File Type To App As EJS.
app.set('view engine', 'ejs');