-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
446 lines (358 loc) · 15.7 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
'use strict';
let util = require('util');
let http = require('http');
let Bot = require('@kikinteractive/kik');
var DOMParser = require('xmldom').DOMParser;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
// Configure the bot API endpoint, details for your bot
let bot = new Bot({
username: 'add2list',
apiKey: '6412641e-f6e6-4da1-a34b-19ae18a2b929',
baseUrl: 'https://cryptic-spire-14213.herokuapp.com/',
staticKeyboard: new Bot.ResponseKeyboard(['List', 'Getmylist', 'Help' ])
});
bot.updateBotConfiguration();
var mysql = require("mysql");
// First you need to create a connection to the db
var con = mysql.createConnection({
host: "sql12.freemysqlhosting.net",
user: "sql12174195",
password: "vJDIEQ5RFF",
port: 3306,
database: "sql12174195"
});
con.connect(function (err) {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
bot.onStartChattingMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
});
bot.onLinkMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
});
bot.onPictureMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
});
bot.onVideoMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
});
bot.onScanDataMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
});
bot.onStartChattingMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
});
bot.onTextMessage((message, next) => {
if (message.body == 'list' || message.body == 'List')
{
if (message.isInPublicChat()) {
var participants = message.participants;
var beg = "SELECT * FROM books WHERE ((Mentioned LIKE '%";
var end = "%'))";
participants = participants.join("%') OR (Mentioned LIKE '%");
var query = beg + participants + end;
var List = [];
con.query(query, function (err, rows) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
for (var i = 0; i < 15; i++) {
var TitleB = rows[i].Title;
var uname = rows[i].Mentioned;
var Name = (i + 1) + '.' + TitleB + ' Member: ' + uname;
List.push(Name);
}
var List1 = List.join('\n');
message.reply(List1);
});
} else
{
message.reply('This method only works in group chats.');
}
}
next();
});
bot.onTextMessage((message, next) => {
if (message.body.match('add') || message.body.match('Add'))
{
var book = message.body;
book = book.substring(3);
var uname = message.from;
var ISBN, Image, AuthorN, Author, TitleN;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var grbaseurl = 'http://www.goodreads.com/book/title?format=xml&key=48bnEKgbFotjftWpTKmlA&title=';
var url = grbaseurl + book;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState === 4 && xhr.status === 200) {
var respon = xhr.responseText;
var response = '<?xml version="1.0" encoding="UTF-8"?>\n' + respon + '\n</xml>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(response, "text/xml");
try {
ISBN = xmlDoc.getElementsByTagName("isbn")[0].childNodes[0].nodeValue;
Image = xmlDoc.getElementsByTagName("image_url")[0].childNodes[0].nodeValue;
AuthorN = xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
TitleN = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
} catch (err) {
message.reply('Cannot find ISBN');
} finally {
Image = Image.toString();
Author = 'Author: ' + AuthorN.toString();
message.reply(Bot.Message.picture(Image));
message.reply(Author);
con.query('SELECT Mentioned FROM books WHERE ISBN = ?', ISBN, function (err, rows) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
if (rows.length > 0) {
var unameAdded = rows[0].Mentioned;
// var unameAdded = unameAdded.toString();
var unameAdded = JSON.stringify(unameAdded);
console.log(unameAdded);
var query = "UPDATE books SET Mentioned = CONCAT( Mentioned, ' , ' , '"+uname+"' ) where ISBN = '"+ISBN+"'";
con.query(query,
function (err, res) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
console.log('Last insert ID:', res.insertId);
});
var reply = 'This book has already been added by ' + unameAdded + ' Your name has been added to the list.';
message.reply(reply);
} else {
con.query('INSERT INTO books SET ?', {Title: TitleN, Mentioned: uname, Author: AuthorN, ISBN: ISBN},
function (err, res) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
console.log('Last insert ID:', res.insertId);
});
}
});
}
}
}
}
next();
});
bot.onTextMessage((message, next) => {
if (message.body.toLowerCase().match('getmylist') || message.body.toLowerCase().match('getlist')) {
var uname;
if (message.body.toLowerCase().match('getmylist')) {
uname = message.from;
} else if (message.body.toLowerCase().match('getlist')) {
uname = message.body;
uname = uname.substring(8);
}
var List = [];
var query = "SELECT Title, Author FROM books WHERE Mentioned LIKE '%" + uname + "%'";
con.query(query, function (err, rows) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
if(rows.length>0){
for (var i = 0; i < rows.length; i++) {
var TitleB = rows[i].Title;
var Author = rows[i].Author;
var Name = (i + 1) + '.' + TitleB + ' Author: ' + Author;
List.push(Name);
}
var List1 = List.join('\n');
message.reply(List1);
}
else{
message.reply('No list found for this user');
}
});
}
next();
});
bot.onTextMessage((message, next) => {
if (message.body.toLowerCase().match('getdetails')) {
var book = message.body;
book = book.substring(11);
var ISBN, Image, AuthorN, TitleN, Description, Rating;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var grbaseurl = 'http://www.goodreads.com/book/title?format=xml&key=48bnEKgbFotjftWpTKmlA&title=';
var url = grbaseurl + book;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState === 4 && xhr.status === 200) {
var respon = xhr.responseText;
var response = '<?xml version="1.0" encoding="UTF-8"?>\n' + respon + '\n</xml>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(response, "text/xml");
try {
ISBN = xmlDoc.getElementsByTagName("isbn")[0].childNodes[0].nodeValue;
Image = xmlDoc.getElementsByTagName("image_url")[0].childNodes[0].nodeValue;
AuthorN = xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
TitleN = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
Description = xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue;
Rating = xmlDoc.getElementsByTagName("average_rating")[0].childNodes[0].nodeValue;
Image = Image.toString();
TitleN = 'Title: ' + TitleN.toString();
AuthorN = 'Author: ' + AuthorN.toString();
Rating = 'Rating: ' + Rating.toString();
Description = 'Description: ' + Description.toString();
message.reply(Bot.Message.picture(Image));
message.reply(TitleN);
message.reply(AuthorN);
message.reply(Rating);
message.reply(Description);
} catch (err) {
message.reply('Book not found!');
}
}
}
}
next();
});
bot.onTextMessage((message, next) => {
if (message.body.toLowerCase().match('delete')) {
var uname = message.from;
var book = message.body.substring(7);
var ISBN;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var grbaseurl = 'http://www.goodreads.com/book/title?format=xml&key=48bnEKgbFotjftWpTKmlA&title=';
var url = grbaseurl + book;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState === 4 && xhr.status === 200) {
var respon = xhr.responseText;
var response = '<?xml version="1.0" encoding="UTF-8"?>\n' + respon + '\n</xml>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(response, "text/xml");
try {
ISBN = xmlDoc.getElementsByTagName("isbn")[0].childNodes[0].nodeValue;
var query = "UPDATE books SET Mentioned = REPLACE(Mentioned, '," + uname + "', '') WHERE ISBN = '" + ISBN + "'";
// console.log(query);
con.query(query, function (err, rows) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
if (rows.changedRows) {
message.reply('Book deleted!');
} else {
var query1 = "UPDATE books SET Mentioned = REPLACE(Mentioned, '" + uname + "', '') WHERE ISBN = '" + ISBN + "'";
// console.log(query1);
con.query(query1, function (err, rows) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
if (rows.changedRows) {
message.reply('Book deleted!');
} else {
message.reply('Book not found!');
}
});
}
});
} catch (err) {
message.reply('Book not found!');
}
}
}
}
next();
});
bot.onTextMessage((message, next) => {
if (message.body.toLowerCase().match('cleanall')) {
if(message.from === 'queenofwords1' || message.from === 'blonde_angel_reading'|| message.from === 'magadin' ){
var query1 = 'DELETE FROM books WHERE (Mentioned = "" OR Mentioned = " ,")';
con.query(query1, function (err, rows) {
if (err){
message.reply('An error occurred. Try again. Or get in touch at [email protected]');
throw err;
}
if (rows.affectedRows>0) {
message.reply('Book deleted!');
} else {
message.reply('Nothing to delete.');
}
});
}
else{
message.reply('You do not have permission to delete.');
}
}
next();
});
bot.onTextMessage((message, next) => {
if (message.body.toLowerCase().match('hey')||message.body.toLowerCase().match('hi')||message.body.toLowerCase().match('hello')||message.body.toLowerCase().match('how')) {
bot.getUserProfile(message.from)
.then((user) => {
message.reply(`Hey ${user.firstName}!`);
message.reply(`To get a list of all functions type @add2list help`);
});
}
next();
});
/* bot.onTextMessage((message, next) => {
message.reply(message.addResponseKeyboard(['Help']));
next();
}); */
bot.onTextMessage((message, next) => {
if (message.body.toLowerCase() === 'help') {
message.reply('You can use any of these funtions:\n 1. @add2list add <bookname> - To add a new book to the List \n 2. @add2list getdetails <bookname> - To get all the details about a book from Goodreads. \n 3. @add2list getmylist - To get a list of all the books you have added \n 4. @add2list getlist <uname> - To get a list of all the books added by a user.');
// message.reply('3. @add2list getmylist - To get a list of all the books you have added \n @add2list getlist <uname>');
message.reply('5. @add2list list - To get a list of all the books that have been added.');
// message.reply('5. @add2list getlist <uname> - To get a list of all the books added by a user.');
message.reply('6. @add2list delete <bookname> - To delete a book you have added from the list.');
message.reply('7. @add2list cleanall - To delete the books added by users not in the group anymore. Only admins have this permission.');
}
next();
});
// Set up your server and start listening
let server = http
.createServer(bot.incoming())
.listen(process.env.PORT || 3000);