-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (84 loc) · 2.92 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
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var conf = require("./secrets.json");
var helpers = require("./functions");
var groups = require("./sites_info").groups;
var user_state_map = {};
var messages = require("./sites_info").messages;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post("/" + conf.spider_query_token, async function(req, res) {
let message = req.body.message;
if (message.from.id in user_state_map) {
if (
"query" in user_state_map[message.from.id] &&
"site" in user_state_map[message.from.id]
) {
let to_forward = message.text;
let data = {
text: messages.thank_you,
chat_id: message.from.id
};
let statusCode = await helpers.send_message("sendMessage", data);
data = {
text:
String.fromCodePoint(0x203c) +
" <b>" +
user_state_map[message.from.id].query +
"</b> for " +
user_state_map[message.from.id].site +
"\n" +
to_forward,
chat_id: groups.complaints_portal
};
statusCode = await helpers.send_message("sendMessage", data);
delete user_state_map[message.from.id];
res.status(statusCode || 200).send();
return;
} else if ("site" in user_state_map[message.from.id]) {
// site is already chosen
let query = helpers.select_query_type(message.text);
if (query != null) {
user_state_map[message.from.id].query = query;
let data = helpers.send_final_question(
message,
user_state_map[message.from.id]
);
let statusCode = await helpers.send_message("sendMessage", data);
res.status(statusCode || 200).send();
return;
} else {
let data = helpers.handleStart(user_state_map, message);
let statusCode = await helpers.send_message("sendMessage", data);
res.status(statusCode || 200).send();
return;
}
} else {
let site = helpers.select_site(message.text);
if (site != null) {
user_state_map[message.from.id].site = site;
let data = helpers.send_query_type(message);
let statusCode = await helpers.send_message("sendMessage", data);
res.status(statusCode || 200).send();
return;
} else {
let data = helpers.handleStart(user_state_map, message);
let statusCode = await helpers.send_message("sendMessage", data);
res.status(statusCode || 200).send();
return;
}
}
} else {
let data = helpers.handleStart(user_state_map, message);
let statusCode = await helpers.send_message("sendMessage", data);
res.status(statusCode || 200).send();
return;
}
});
app.get("/" + conf.spider_query_token, function(req, res) {
res.status(200).send("GET request");
});
app.listen(conf.port, function() {
console.log("listening on " + conf.port);
});