-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.js
More file actions
49 lines (38 loc) · 1.3 KB
/
processing.js
File metadata and controls
49 lines (38 loc) · 1.3 KB
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
const _ = require('lodash');
const send = require('./send');
let waitUserId = null;
const couples = [];
module.exports = async ({user_id: userId, body: text, payload}) => {
userId = +userId;
const index = _.findIndex(couples, arr => _.indexOf(arr, userId) !== -1);
if(index === -1){
if(!waitUserId || waitUserId === userId){
waitUserId = userId;
await send(userId, 'Ждем собеседника...');
return
} else {
couples.push([userId, waitUserId]);
const text = "Поприветствуйте собеседника";
await send(userId, text);
await send(waitUserId, text);
waitUserId = null;
return;
}
} else {
const couple = couples[index];
const to = _.indexOf(couple, userId) === 0 ? couple[1] : couple[0];
let button;
try {
button = +JSON.parse(payload).button;
} catch (error) { // eslint-disable-line
}
console.log(button);
if (button === 1) {
couples.splice(index, 1)
await send(to, 'Собеседник отключился');
await send(userId, 'Чат остановлен');
return;
}
await send(to, text);
};
}