-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
177 lines (139 loc) · 4.5 KB
/
bot.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
const TelegramBot = require('node-telegram-bot-api');
const storage = require('./storage.js');
const _ = require('lodash');
const config = require('./config.js');
const logger = require('./logger.js');
let currentModes = {}; // userId -> mode
const defaultAnswer = 'Hi there, please use "/" to interact';
const modeAnswers = {
add: 'We are going to add a car to your watch list! Please enter search string (one or more words)',
cancel: 'Ok then! Anythyng else?',
clear: 'Your watch list has been cleared.',
emptyList: 'Sorry, you have no cars in your watch list.',
};
class Bot {
constructor() {
this.bot = new TelegramBot(config.token, { polling: true });
}
init() {
this.bot.on('message', this.processMessage.bind(this));
logger.info('Bot initialized');
}
processMessage(message) {
const { from, text } = message;
const command = text.match(/\/(.+)/);
// command message
if (command) {
this.processCommand(from, command.pop());
// after-command message
} else if (currentModes[from.id]) {
this.processAnswer(from, text);
// new user
} else {
this.processNewUser(from);
}
}
processNewUser(user) {
this.createUser(user);
this.sendMessage(user.id, defaultAnswer);
}
processCommand(user, command) {
currentModes[user.id] = command;
switch (command) {
case 'start':
this.clearCurrentMode(user);
this.processNewUser(user);
break;
case 'add':
this.sendMessage(user.id, modeAnswers[command]);
break;
case 'cancel':
this.clearCurrentMode(user);
this.sendMessage(user.id, modeAnswers[command], { reply_markup: { hide_keyboard: true } });
break;
case 'clear':
this.clearCurrentMode(user);
storage.removeAllSubscriptions(user.id);
this.sendMessage(user.id, modeAnswers[command]);
break;
case 'list':
this.clearCurrentMode(user);
storage
.getSubscriptions(user.id)
.then(subscriptions => {
if (_.isEmpty(subscriptions)) {
this.sendMessage(user.id, modeAnswers['emptyList']);
} else {
let list = '';
subscriptions.forEach(item => {
list += item.searchString + '\n';
});
this.sendMessage(user.id, 'Here is your watch list:' + '\n\n' + list);
}
});
break;
case 'delete':
storage
.getSubscriptions(user.id)
.then(subscriptions => {
if (_.isEmpty(subscriptions)) {
this.sendMessage(user.id, modeAnswers['emptyList']);
currentModes[user.id] = '';
} else {
const list = [];
subscriptions.forEach(item => {
list.push({ text: item.searchString });
});
const markup = {
keyboard: [list],
resize_keyboard: true,
one_time_keyboard: true,
};
this.sendMessage(user.id, 'Please select what should we delete:', { reply_markup: markup });
}
});
break;
default:
break;
}
logger.info('CurrentMode of user', user.username, '[', user.id, '] was set to', currentModes[user.id]);
}
clearCurrentMode(user) {
currentModes[user.id] = '';
}
processAnswer(user, text) {
switch (currentModes[user.id]) {
case 'add':
storage.addSubscription(user.id, text);
this.sendMessage(user.id, 'Ok, "' + text + '" was added to your watch list. We`ll keep you updated!');
break;
case 'delete':
storage.removeSubscription(user.id, text);
this.sendMessage(user.id, 'Ok, "' + text + '" was removed from your watch list.', { reply_markup: { hide_keyboard: true } });
break;
default:
break;
}
}
createUser(user) {
storage.createUser({
id: user.id,
username: user.username,
firstName: user.first_name,
lastName: user.last_name,
});
}
sendNotification(userId, car) {
const price = car.price ? ' - €' + car.price : '';
const text = 'We found something for you!\n\n' + car.title + price + '\n' + car.link;
this.sendMessage(userId, text);
}
sendMessage(userId, text) {
this.bot
.sendMessage(userId, text)
.catch((error) => {
logger.error('Send message failed', { userId, text }, error.code, error.response.body);
});
}
}
module.exports = new Bot();