This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteleboyarin.js
191 lines (172 loc) · 8.36 KB
/
teleboyarin.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
'use strict';
const TelegramBot = require('node-telegram-bot-api'),
rp = require('request-promise'),
Redis = require('ioredis'),
config = require('./config.json'),
npm = require('./package.json'),
raven = require('raven');
if (process.env.SENTRY_DSN) {
const sentry = new raven.Client(process.env.SENTRY_DSN);
sentry.patchGlobal();
}
if (process.env.TELEGRAM_TOKEN) config.token = process.env.TELEGRAM_TOKEN;
const request = rp.defaults({headers: {
'User-Agent': `Teleboyarin/${npm.version}`,
Accept: 'application/json'
}});
const redis = process.env.REDIS_PORT ? new Redis(process.env.REDIS_PORT) : (config.redis ? new Redis(config.redis) : new Redis());
var workers = {};
function getWorker(process, userId) {
if (!(process.id in workers)) workers[process.id] = {};
if (userId in workers[process.id]) return new Promise((resolve, reject) => resolve(workers[process.id][userId]));
const workerGetReq = `${config.apiURL}/processes/${process.id}/workers/tagged/telegram${userId}`,
workerPostReq = `${config.apiURL}/processes/${process.id}/workers`;
return new Promise((resolve, reject) => request.get(workerGetReq).
then((worker) => !!worker ?
resolve(workers[process.id][userId] = JSON.parse(worker)) :
request.post(workerPostReq, {form: {tags: `telegram${userId}`}})).
then((worker) => resolve(workers[process.id][userId] = JSON.parse(worker))).
catch((err) => reject(err)).
catch((err) => reject(err))
);
}
const whiteList = process.env.MTSAR_PROCESSES ? process.env.MTSAR_PROCESSES.split(',') : config.processes;
function filterProcesses(processes) {
return whiteList ? processes.filter((process) => whiteList.indexOf(process.id) > -1) : processes;
}
const bot = new TelegramBot(config.token, {
polling: true
});
bot.on('text', onText);
function onText(msg) {
const text = msg.text.trim();
if (!!config.disabled) {
bot.sendMessage(msg.chat.id, 'I am relaxing.');
return;
}
redis.get(msg.from.id).then((raw) => {
const state = raw ? JSON.parse(raw) : undefined;
if (text == '/cancel') {
const reply = state ? `Cancelling the ${state.text} operation.` : 'Cancelling nothing.';
const markup = JSON.stringify({hide_keyboard: true});
redis.del(msg.from.id).then(() => bot.sendMessage(msg.chat.id, reply, {reply_markup: markup}));
} else {
switch (!!state ? state.text : undefined) {
case '/annotate': stateAnnotate(text, msg, state); break;
case '/annotate/answer': stateAnnotateAnswer(text, msg, state); break;
case '/process': stateProcess(text, msg, state); break;
default: stateInitial(text, msg, state); break;
}
}
});
}
function stateInitial(text, msg, state) {
switch (text) {
case '/start':
bot.sendMessage(msg.chat.id, `Hi, ${msg.from.first_name}!`);
break;
case '/version':
request.get(`${config.apiURL}/version`).then((body) => {
const reply = `Mechanical Tsar v${body}`;
bot.sendMessage(msg.chat.id, reply);
});
break;
case '/processes':
request.get(`${config.apiURL}/processes`).then((body) => {
const processes = filterProcesses(JSON.parse(body));
if (processes.length > 0) {
const reply = processes.map((process) => `*${process.id}*: ${process.description}`).join("\n");
bot.sendMessage(msg.chat.id, reply, {parse_mode: 'Markdown'});
} else {
const reply = "No processes.";
bot.sendMessage(msg.chat.id, reply, {parse_mode: 'Markdown'});
}
});
break;
case '/process':
case '/annotate':
request.get(`${config.apiURL}/processes`).then((body) => {
const processes = filterProcesses(JSON.parse(body));
const reply = 'Which one?';
const markup = JSON.stringify({
keyboard: processes.map((process) => [process.id]),
one_time_keyboard: true
});
bot.sendMessage(msg.chat.id, reply, {reply_markup: markup}).then(() => {
redis.set(msg.from.id, JSON.stringify({text: text, processes: processes}));
});
});
break;
default:
break;
}
}
function stateProcess(text, msg, state) {
const process = state.processes.find((p) => p.id == text);
if (process) {
const processReq = request.get(`${config.apiURL}/processes/${text}`),
workersReq = request.get(`${config.apiURL}/processes/${text}/workers`),
tasksReq = request.get(`${config.apiURL}/processes/${text}/tasks`),
answersReq = request.get(`${config.apiURL}/processes/${text}/answers`);
Promise.all([processReq, workersReq, tasksReq, answersReq]).then((responses) => {
const process = JSON.parse(responses[0]),
workers = JSON.parse(responses[1]).length,
tasks = JSON.parse(responses[2]).length,
answers = JSON.parse(responses[3]).length;
const reply = `[${process.description}](${processReq.uri.href})\n*Workers:* ${workers}.\n*Tasks:* ${tasks}.\n*Answers:* ${answers}.`;
const markup = JSON.stringify({hide_keyboard: true});
redis.del(msg.from.id).then(() => bot.sendMessage(msg.chat.id, reply, {parse_mode: 'Markdown', reply_markup: markup}));
});
}
}
function stateAnnotate(text, msg, state) {
const process = state.processes.find((p) => p.id == text);
if (process) {
getWorker(process, msg.from.id).then((worker) => {
request.get(`${config.apiURL}/processes/${process.id}/workers/${worker.id}/task`).then((response) => {
if (!response || 0 === response.length) {
const reply = 'Thank you, but this process has already been finished.';
const markup = JSON.stringify({hide_keyboard: true});
redis.del(msg.from.id).then(() => bot.sendMessage(msg.chat.id, reply, {parse_mode: 'Markdown', reply_markup: markup}));
} else {
const allocation = JSON.parse(response);
const task = allocation.tasks[0];
const reply = task.description;
const markup = JSON.stringify({
keyboard: task.answers.map((answer) => [answer]).concat([['/stop']]),
one_time_keyboard: true
});
bot.sendMessage(msg.chat.id, reply, {parse_mode: 'Markdown', reply_markup: markup}).then(() => {
redis.set(msg.from.id, JSON.stringify({text: '/annotate/answer', process: process, worker: worker, task: task}));
});
}
});
});
}
}
function stateAnnotateAnswer(text, msg, state) {
switch (text) {
case '/stop': {
const reply = 'Thank you for your help!';
const markup = JSON.stringify({hide_keyboard: true});
redis.del(msg.from.id).then(() => bot.sendMessage(msg.chat.id, reply, {reply_markup: markup}));
}
break;
default:
if (state.task.answers.indexOf(text) > -1) {
const process = state.process, worker = state.worker, task = state.task;
const answers = {}; answers[`answers[${task.id}]`] = text;
request.patch(`${config.apiURL}/processes/${process.id}/workers/${worker.id}/answers`, {form: answers}).then((response) => {
const reply = 'Your answer has been recorded!';
const markup = JSON.stringify({hide_keyboard: true});
bot.sendMessage(msg.chat.id, reply, {parse_mode: 'Markdown', reply_markup: markup}).then(() => {
const stateNew = {text: process.id, processes: [process]};
redis.set(msg.from.id, JSON.stringify(stateNew)).then(() => {
stateAnnotate(stateNew.text, msg, stateNew);
});
});
});
}
break;
}
}