-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtelegram-interaction.js
373 lines (319 loc) · 10.5 KB
/
telegram-interaction.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//VERSION: 1
/**
* This script provides a framework for creating a basic Telegram bot using the
* scriping functionalities of the Gen2 devices. It allows you to define custom commands,
* validate parameters, and perform actions based on user messages.
* The bot interacts with the Telegram API to receive and send messages.
*
* The script will take advantage of the KVS to store the messages offset value. The key
* for it is the script's ident name from the configuration
*
* Please check TELEGRAM-BOT.md for instructions how to setup the telegram bot and
* further instructions of how to configure the commands
*/
let CONFIG = {
// the bot api key taken from the BotFather
botKey: "64XXXXXX33:AAH24shXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
// timeout value for HTTP requests in seconds.
pullTimeout: 5,
// timer interval in milliseconds for polling updates from Telegram API.
timer: 500,
// unique name used for communication between different parts of this script and KVS.
identName: "telegram-bot",
// if set to true, the script will print debug messages in the console
debug: false,
// timeout for pull retry on fail in seconds
retryTimeout: 3,
// object defining custom commands that the bot can understand and respond to.
commands: {
// command identifier, e.g. /toggle on|off
"/toggle": {
// list of params, parsed in the same order as in the list
params: [
{
// value key, later can be used to get the parsed value
key: "state",
/**
* Validates and processes the parameter's value
* @param {String} value the passed value
* @param {Function} sendMessage function to send a message back
* @returns {Any} return the value, or undefined|null in case of validation failure.
*/
transform: function(value, sendMessage) {
if(["on", "off"].indexOf(value) >= 0) {
return value === "on";
}
sendMessage("Unknown state");
return undefined;
}
}
],
/**
* To be executed when the command is successfully parsed and all parameters are validated.
* @param {Object} params all passed parameters, each value is maped to its key
* @param {Function} sendMessage function to send a message back
*/
handler: function(params, sendMessage) {
Shelly.call(
"Switch.Set",
{ id: 0, on: params.state },
function(_, error_code, error_message) {
// the request is successfull
if(error_code === 0) {
sendMessage("Ok, the ouput was switched");
}
else {
sendMessage(error_message);
}
}
);
},
// if true, the script waits for all parameters to be entered (can be in separate messages).
waitForAllParams: false,
// specifies the maximum number of unsuccessful tries before the command is aborted.
abortAfter: 3,
},
"/ping": {
params: [
{
key: "deviceIp",
transform: function(value, sendMessage) {
if(value.split(".").length === 4) {
return value;
}
sendMessage("Invalid IP");
},
missingMessage: "Send me an local IP"
},
{
key: "timeout",
transform: function(value, sendMessage) {
value = Number(value);
if(!isNaN(value)) {
return value;
}
sendMessage("Invalid timeout value");
},
missingMessage: "Send me the timeout value"
}
],
handler: function(params, sendMessage) {
sendMessage("Pinging... " + params.deviceIp);
Shelly.call(
"HTTP.GET",
{ url: "http://" + params.deviceIp, timeout: params.timeout },
function(_, error_code, error_message) {
if(error_code === 0) {
sendMessage("The device is reachable");
}
else {
sendMessage("The device, can't be reached. " + error_message);
}
}
);
},
waitForAllParams: true
}
},
};
let TelegramBot = {
/**
* Initializes the bot by emitting the specified event to start polling for updates.
*/
init: function () {
Shelly.call(
"KVS.Get",
{ key: CONFIG.identName },
(function (data, error) {
let value = 0;
if(error !== 0) {
console.log("Cannot read the value for the provided key, reason, using default value.");
}
else {
value = data.value;
}
this.messageOffset = value;
Shelly.emitEvent(CONFIG.identName);
}.bind(this))
);
this.baseUrl = "https://api.telegram.org/bot" + CONFIG.botKey;
},
/**
* Called when the event specified in the CONFIG is emitted
*/
onEvent: function () {
if(CONFIG.debug) {
console.log("Poll started");
}
Shelly.call(
"HTTP.REQUEST",
{
method: "POST",
url: this.baseUrl + "/getUpdates",
timeout: CONFIG.pullTimeout,
body: {
offset: this.messageOffset + 1,
limit: 1
}
},
this.onFinishPoll.bind(this),
this
);
},
/**
* Callback function after finishing polling updates from the Telegram API. See https://shelly-api-docs.shelly.cloud/gen2/Scripts/ShellyScriptLanguageFeatures#shellycall
* @param {Object|null|undefined} data the received data from the request
* @param {Number} error the id of the error
* @param {*} errorMessage the error message if has
* @returns
*/
onFinishPoll: function (data, error, errorMessage) {
if(error !== 0) {
console.log("Poll finishes with error ->", errorMessage);
console.log("Aborting... Retrying after " + CONFIG.retryTimeout + " seconds");
Timer.set(CONFIG.retryTimeout * 1000, false, function() {
console.log("Trying now...")
Shelly.emitEvent(CONFIG.identName);
});
return;
}
let response = JSON.parse(data.body);
if(!response.ok) {
console.log("Something is wrong with the config. Error:", response.description);
return;
}
if(response.result.length === 0 && CONFIG.debug) {
console.log("No new messages");
}
let lastUpdateId = -1;
for (let res of response.result) {
if(CONFIG.debug) {
console.log("New message", res.message.text);
}
this.handleMessage(res.message);
lastUpdateId = res.update_id;
}
if (lastUpdateId > this.messageOffset) {
this.messageOffset = lastUpdateId;
Shelly.call("KVS.Set", { key: CONFIG.identName, value: this.messageOffset } );
}
Timer.set(CONFIG.timer, false, function() {
Shelly.emitEvent(CONFIG.identName);
});
},
/**
* Processes received messages
* @param {Object} message received message object from the API
*/
handleMessage: function (message) {
if(CONFIG.debug) {
console.log("MSG", JSON.stringify(message.text));
}
let words = message.text.trim().split(" ");
function sendMessage(textMsg) {
if(CONFIG.debug) {
console.log("SENDING", textMsg, message.chat.id);
}
Shelly.call(
"HTTP.GET",
{
url: this.baseUrl + "/sendMessage?chat_id=" + message.chat.id + "&text=" + textMsg,
timeout: 1,
},
function(d, r, m) {
if(CONFIG.debug) {
console.log("MSG SENT", JSON.stringify(d), r, m);
}
}
);
}
if(CONFIG.debug) {
console.log("COMMAND", JSON.stringify(this.lastCommand));
}
if(
this.lastCommand &&
typeof CONFIG.commands[this.lastCommand.key].abortAfter === "number" &&
this.lastCommand.tries > CONFIG.commands[this.lastCommand.key].abortAfter
) {
sendMessage("Max tries exceeded, aborting...");
}
else {
if(CONFIG.commands) {
let params = {};
if(words.length > 0 && (words[0] in CONFIG.commands || this.lastCommand)) {
let commandKey = words[0];
let paramScanStartId = 0;
let wordCounter = 1;
if(this.lastCommand) {
commandKey = this.lastCommand.key;
params = this.lastCommand.params;
paramScanStartId = this.lastCommand.waitingParamId;
wordCounter = 0;
}
let command = CONFIG.commands[commandKey];
if(command.waitForAllParams && typeof this.lastCommand === "undefined") {
this.lastCommand = {
key: commandKey,
params: {},
waitingParamId: 0,
tries: 0
};
}
for (let i = paramScanStartId; i < command.params.length; i++) {
if(wordCounter >= words.length) {
sendMessage(command.params[i].missingMessage || "Missing \'" + command.params[i].key + "\' param");
if(this.lastCommand) {
this.lastCommand.waitingParamId = i;
this.lastCommand.tries += 1;
}
return;
}
else {
let value = words[wordCounter++];
if(typeof command.params[i].transform === "function") {
value = command.params[i].transform(value, sendMessage);
}
if(typeof value === "undefined" || value === null) {
if(this.lastCommand) {
this.lastCommand.waitingParamId = i;
this.lastCommand.tries += 1;
}
return;
}
params[command.params[i].key] = value;
if(this.lastCommand) {
this.lastCommand.params = params;
this.lastCommand.tries = 0;
if(CONFIG.debug) {
console.log("RESET COUNTER");
}
}
}
}
command.handler(params, sendMessage);
}
else { //no matching command
sendMessage("Not recognized command");
}
}
}
this.lastCommand = undefined;
},
};
/**
* initializes the bot and setting up event listeners.
*/
function init () {
Shelly.addEventHandler(function(data) {
if (
typeof data === "undefined" ||
typeof data.info === "undefined" ||
data.info.event !== CONFIG.identName
) {
return;
}
TelegramBot.onEvent();
});
TelegramBot.init();
}
init();