-
Notifications
You must be signed in to change notification settings - Fork 16
/
bot.js
238 lines (196 loc) · 7.28 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
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
const Telegraf = require('telegraf');
const {
Extra,
Markup
} = require('telegraf');
const config = require('./config');
const dataService = require('./dataService');
const bot = new Telegraf(config.botToken);
const helpMsg = `Command reference:
/start - Start bot (mandatory in groups)
/inc - Increment default counter
/inc1 - Increment counter 1
/incx - Increment counter x (replace x with any number)
/dec - Decrement counter
/decx - Decrement counter x
/reset - Reset counter back to 0
/resetx - Reset counter x back to 0
/set y - Set counter to y [/set y]
/setx y - Set counter x to y [/setx y]
/get - Show current counter
/getx - Show value of counter x
/getall - Show all counters
/stop - Attemt to stop bot
/about - Show information about the bot
/help - Show this help page
Tip: You can also use e.g. '/inc2 5' to increase counter two by five counts.`;
const inputErrMsg = `💥 BOOM... 🔩☠🔧🔨⚡️
Hm, that wasn't supposed to happen. You didn't input invalid characters, did you?
The usage for this command is \"/set x\", where x is a number.
At the moment, I can only count integers, if you want to add your own number system, please feel free to do so. Just click here: /about `;
const incNMsg = `To use multiple counters, simply put the number of the counter you want to increase directly after the command like so:
/inc1 <- this will increment counter 1
/inc <- this will increment the default counter (0)
This does also work with other commands like /dec1 /reset1 /set1 /get1`;
const aboutMsg = "This bot was created by @LeoDJ\nSource code and contact information can be found at https://github.com/LeoDJ/telegram-counter-bot";
function getRegExp(command) {
return new RegExp("/" + command + "[0-9]*\\b");
}
//get username for group command handling
bot.telegram.getMe().then((botInfo) => {
bot.options.username = botInfo.username;
console.log("Initialized", botInfo.username);
});
dataService.loadUsers();
function userString(ctx) {
return JSON.stringify(ctx.from.id == ctx.chat.id ? ctx.from : {
from: ctx.from,
chat: ctx.chat
});
}
function logMsg(ctx) {
var from = userString(ctx);
console.log('<', ctx.message.text, from)
}
function logOutMsg(ctx, text) {
console.log('>', {
id: ctx.chat.id
}, text);
}
bot.command('broadcast', ctx => {
if(ctx.from.id == config.adminChatId) {
var words = ctx.message.text.split(' ');
words.shift(); //remove first word (which ist "/broadcast")
if(words.length == 0) //don't send empty message
return;
var broadcastMessage = words.join(' ');
var userList = dataService.getUserList();
console.log("Sending broadcast message to", userList.length, "users: ", broadcastMessage);
userList.forEach(userId => {
console.log(">", {id: userId}, broadcastMessage);
ctx.telegram.sendMessage(userId, broadcastMessage);
});
}
});
bot.command('start', ctx => {
logMsg(ctx);
dataService.registerUser(ctx);
dataService.setCounter(ctx.chat.id, '0', 0);
var m = "Hello, I'm your personal counter bot, simply use the commands to control the counter";
ctx.reply(m);
logOutMsg(ctx, m);
setTimeout(() => {
ctx.reply(0);
logOutMsg(ctx, 0)
}, 50); //workaround to send this message definitely as second message
});
bot.command('stop', ctx => {
logMsg(ctx);
var m = "I'm sorry, Dave, I'm afraid I can't do that.";
logOutMsg(ctx, m);
ctx.reply(m);
});
bot.command(['incx', 'decx', 'getx', 'setx', 'resetx'], ctx => {
logMsg(ctx);
logOutMsg(ctx, incNMsg);
ctx.reply(incNMsg);
});
bot.command('help', ctx => {
logMsg(ctx);
logOutMsg(ctx, helpMsg);
ctx.reply(helpMsg);
});
bot.command('about', ctx => {
logMsg(ctx);
logOutMsg(ctx, aboutMsg);
ctx.reply(aboutMsg);
});
bot.command('getall', ctx => {
logMsg(ctx);
counters = dataService.getAllCounters(ctx.chat.id);
msg = "";
Object.keys(counters).forEach(counterId => {
msg += '[' + counterId + '] ' + counters[counterId].value + "\n";
});
logOutMsg(ctx, msg);
ctx.reply(msg);
});
bot.hears(getRegExp('inc'), ctx => {
logMsg(ctx);
currentCommand = 'inc';
var m = ctx.message.text.match(getRegExp(currentCommand))[0]; //filter command
var counterId = m.substring(m.indexOf(currentCommand) + currentCommand.length) || 0; //get id of command, return 0 if not found
var delta = 1;
params = ctx.message.text.split(" ");
if (params.length == 2 && !isNaN(params[1])) {
delta = Math.floor(params[1]);
}
var val = +dataService.getCounter(ctx.chat.id, counterId);
val += delta;
dataService.setCounter(ctx.chat.id, counterId, val);
var printCounterId = counterId ? "[" + counterId + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
});
bot.hears(getRegExp('dec'), ctx => {
logMsg(ctx);
currentCommand = 'dec';
var m = ctx.message.text.match(getRegExp(currentCommand))[0]; //filter command
var counterId = m.substring(m.indexOf(currentCommand) + currentCommand.length) || 0; //get id of command, return 0 if not found
var delta = 1;
params = ctx.message.text.split(" ");
if (params.length == 2 && !isNaN(params[1])) {
delta = Math.floor(params[1]);
}
var val = +dataService.getCounter(ctx.chat.id, counterId);
val -= delta;
dataService.setCounter(ctx.chat.id, counterId, val);
var printCounterId = counterId ? "[" + counterId + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
});
bot.hears(getRegExp('reset'), ctx => {
logMsg(ctx);
currentCommand = 'reset';
var m = ctx.message.text.match(getRegExp(currentCommand))[0]; //filter command
var counterId = m.substring(m.indexOf(currentCommand) + currentCommand.length) || 0; //get id of command, return 0 if not found
var val = 0;
dataService.setCounter(ctx.chat.id, counterId, val);
var printCounterId = counterId ? "[" + counterId + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
});
bot.hears(getRegExp('get'), ctx => {
logMsg(ctx);
currentCommand = 'get';
var m = ctx.message.text.match(getRegExp(currentCommand))[0]; //filter command
var counterId = m.substring(m.indexOf(currentCommand) + currentCommand.length) || 0; //get id of command, return 0 if not found
var val = +dataService.getCounter(ctx.chat.id, counterId);
var printCounterId = counterId ? "[" + counterId + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
});
bot.hears(getRegExp('set'), ctx => {
logMsg(ctx);
currentCommand = 'set';
var m = ctx.message.text.match(getRegExp(currentCommand))[0]; //filter command
var counterId = m.substring(m.indexOf(currentCommand) + currentCommand.length) || 0; //get id of command, return 0 if not found
params = ctx.message.text.split(" ");
if (params.length == 2 && !isNaN(params[1])) {
var val = Math.floor(params[1]);
dataService.setCounter(ctx.chat.id, counterId, val);
var printCounterId = counterId ? "[" + counterId + "] " : "";
val = printCounterId + val;
} else {
val = inputErrMsg;
}
logOutMsg(ctx, val);
ctx.reply(val);
});
bot.startPolling();
module.exports = {
}