iobroker.telegram 3.8.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +11 -11
- package/admin/blockly.js +1 -1
- package/admin/custom/customComponents.js +3 -3
- package/admin/custom/customComponents.js.map +1 -1
- package/admin/custom/static/js/{_91570.f7aeeaf4.chunk.js → _91570.cbc32633.chunk.js} +1 -1
- package/admin/custom/static/js/_91571.cbc32633.chunk.js +1 -0
- package/admin/custom/static/js/main.1a77b548.js +5 -0
- package/admin/custom/static/js/main.1a77b548.js.map +1 -0
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.5db7ac95.chunk.js +3 -0
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.5db7ac95.chunk.js.map +1 -0
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23731.4bcc2ec4.chunk.js +3 -0
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23731.4bcc2ec4.chunk.js.map +1 -0
- package/admin/custom/static/js/node_modules_mui_system_esm_createStyled_createStyled_js-node_modules_clsx_dist_clsx_mjs.0f2b42cc.chunk.js +9 -0
- package/admin/custom/static/js/node_modules_mui_system_esm_createStyled_createStyled_js-node_modules_clsx_dist_clsx_mjs.0f2b42cc.chunk.js.map +1 -0
- package/admin/custom/static/js/node_modules_mui_system_esm_useTheme_useTheme_js-_95040.62575f33.chunk.js +3 -0
- package/admin/custom/static/js/node_modules_mui_system_esm_useTheme_useTheme_js-_95040.62575f33.chunk.js.map +1 -0
- package/admin/custom/static/js/node_modules_mui_system_esm_useTheme_useTheme_js-_95041.be9c6178.chunk.js +3 -0
- package/admin/custom/static/js/node_modules_mui_system_esm_useTheme_useTheme_js-_95041.be9c6178.chunk.js.map +1 -0
- package/admin/custom/static/js/node_modules_prop-types_index_js.6da256b6.chunk.js +3 -0
- package/admin/custom/static/js/{node_modules_prop-types_index_js.e828aff9.chunk.js.map → node_modules_prop-types_index_js.6da256b6.chunk.js.map} +1 -1
- package/admin/custom/static/js/{node_modules_react_index_js.0d498b8f.chunk.js → node_modules_react_index_js.70ac611b.chunk.js} +3 -3
- package/admin/custom/static/js/{node_modules_react_index_js.0d498b8f.chunk.js.map → node_modules_react_index_js.70ac611b.chunk.js.map} +1 -1
- package/admin/custom/static/js/src_Components_jsx.1640c0c1.chunk.js +3 -0
- package/admin/custom/static/js/src_Components_jsx.1640c0c1.chunk.js.map +1 -0
- package/admin/custom/static/js/src_bootstrap_jsx.04ccfc57.chunk.js +11 -0
- package/admin/custom/static/js/src_bootstrap_jsx.04ccfc57.chunk.js.map +1 -0
- package/admin/i18n/de/translations.json +2 -2
- package/admin/i18n/en/translations.json +2 -2
- package/admin/jsonConfig.json +29 -0
- package/admin/jsonCustom.json +14 -0
- package/admin/rules/asset-manifest.json +21 -21
- package/admin/rules/customRuleBlocks.js +3 -3
- package/admin/rules/customRuleBlocks.js.map +1 -1
- package/admin/rules/static/js/main.5d9a9893.js +5 -0
- package/admin/rules/static/js/main.5d9a9893.js.map +1 -0
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-d234ae.d4c12c2d.chunk.js +3 -0
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-d234ae.d4c12c2d.chunk.js.map +1 -0
- package/admin/rules/static/js/{node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d270.e4c33232.chunk.js → node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d270.bc9865f0.chunk.js} +3 -3
- package/admin/rules/static/js/{node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d270.e4c33232.chunk.js.map → node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d270.bc9865f0.chunk.js.map} +1 -1
- package/admin/rules/static/js/{node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d271.01f39ba4.chunk.js → node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d271.7c879d40.chunk.js} +3 -3
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d271.7c879d40.chunk.js.map +1 -0
- package/admin/rules/static/js/node_modules_prop-types_index_js.c5b5d86f.chunk.js +3 -0
- package/admin/rules/static/js/{node_modules_prop-types_index_js.b33ab61f.chunk.js.map → node_modules_prop-types_index_js.c5b5d86f.chunk.js.map} +1 -1
- package/admin/rules/static/js/{node_modules_react_index_js.03847742.chunk.js → node_modules_react_index_js.6114a944.chunk.js} +3 -3
- package/admin/rules/static/js/{node_modules_react_index_js.03847742.chunk.js.map → node_modules_react_index_js.6114a944.chunk.js.map} +1 -1
- package/admin/rules/static/js/{src_ActionTelegram_jsx.35b48cc7.chunk.js → src_ActionTelegram_jsx.606264b1.chunk.js} +3 -3
- package/admin/rules/static/js/src_ActionTelegram_jsx.606264b1.chunk.js.map +1 -0
- package/admin/rules/static/js/src_bootstrap_jsx.daa4bc6c.chunk.js +3 -0
- package/admin/rules/static/js/src_bootstrap_jsx.daa4bc6c.chunk.js.map +1 -0
- package/admin/words.js +98 -0
- package/io-package.json +28 -28
- package/main.js +649 -354
- package/package.json +16 -16
- package/admin/custom/static/js/_91571.a2fa5ab6.chunk.js +0 -1
- package/admin/custom/static/js/main.16c1655b.js +0 -5
- package/admin/custom/static/js/main.16c1655b.js.map +0 -1
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.49b95cc9.chunk.js +0 -3
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.49b95cc9.chunk.js.map +0 -1
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23731.ae65be75.chunk.js +0 -3
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23731.ae65be75.chunk.js.map +0 -1
- package/admin/custom/static/js/node_modules_prop-types_index_js.e828aff9.chunk.js +0 -3
- package/admin/custom/static/js/src_Components_jsx.e93247d1.chunk.js +0 -3
- package/admin/custom/static/js/src_Components_jsx.e93247d1.chunk.js.map +0 -1
- package/admin/custom/static/js/src_bootstrap_jsx.80500ab8.chunk.js +0 -3
- package/admin/custom/static/js/src_bootstrap_jsx.80500ab8.chunk.js.map +0 -1
- package/admin/rules/static/js/main.6d220be4.js +0 -5
- package/admin/rules/static/js/main.6d220be4.js.map +0 -1
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-d234ae.cc670442.chunk.js +0 -3
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-d234ae.cc670442.chunk.js.map +0 -1
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d271.01f39ba4.chunk.js.map +0 -1
- package/admin/rules/static/js/node_modules_prop-types_index_js.b33ab61f.chunk.js +0 -3
- package/admin/rules/static/js/src_ActionTelegram_jsx.35b48cc7.chunk.js.map +0 -1
- package/admin/rules/static/js/src_bootstrap_jsx.98efb53a.chunk.js +0 -3
- package/admin/rules/static/js/src_bootstrap_jsx.98efb53a.chunk.js.map +0 -1
package/main.js
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
process.env.NTBA_FIX_319 = 1;
|
|
5
5
|
|
|
6
6
|
const TelegramBot = require('node-telegram-bot-api');
|
|
7
|
-
const utils
|
|
7
|
+
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
|
|
8
8
|
const adapterName = require('./package.json').name.split('.').pop();
|
|
9
|
-
const _
|
|
10
|
-
const fs
|
|
11
|
-
const path
|
|
9
|
+
const _ = require('./lib/words.js');
|
|
10
|
+
const fs = require('node:fs');
|
|
11
|
+
const path = require('node:path');
|
|
12
12
|
const { WebServer } = require('@iobroker/webserver');
|
|
13
|
-
const https
|
|
14
|
-
const axios
|
|
13
|
+
const https = require('node:https');
|
|
14
|
+
const axios = require('axios').default;
|
|
15
15
|
|
|
16
16
|
let bot;
|
|
17
17
|
let users = {};
|
|
@@ -56,22 +56,21 @@ function startAdapter(options) {
|
|
|
56
56
|
options = options || {};
|
|
57
57
|
Object.assign(options, {
|
|
58
58
|
name: adapterName,
|
|
59
|
-
|
|
60
|
-
* If the JS-Controller catches an unhandled error, this will be called
|
|
61
|
-
* so we have a chance to handle it ourselves.
|
|
62
|
-
* @param {Error} err
|
|
63
|
-
*/
|
|
64
|
-
error: (err) => {
|
|
59
|
+
error: err => {
|
|
65
60
|
// Identify unhandled errors originating from callbacks in scripts
|
|
66
61
|
// These are not caught by wrapping the execution code in try-catch
|
|
67
|
-
if (err
|
|
62
|
+
if (err) {
|
|
68
63
|
const errStr = err.toString();
|
|
69
|
-
if (
|
|
64
|
+
if (
|
|
65
|
+
errStr.includes('getaddrinfo') ||
|
|
66
|
+
errStr.includes('api.telegram.org') ||
|
|
67
|
+
errStr.includes('EAI_AGAIN')
|
|
68
|
+
) {
|
|
70
69
|
return true;
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
return false;
|
|
74
|
-
}
|
|
73
|
+
},
|
|
75
74
|
});
|
|
76
75
|
|
|
77
76
|
adapter = new utils.Adapter(options);
|
|
@@ -128,7 +127,9 @@ function startAdapter(options) {
|
|
|
128
127
|
if (!err) {
|
|
129
128
|
adapter.sendTo(obj.from, obj.command, userID, obj.callback);
|
|
130
129
|
updateUsers();
|
|
131
|
-
adapter.log.info(
|
|
130
|
+
adapter.log.info(
|
|
131
|
+
`Receiving of system messages for user "${userID}" has been changed to ${checked}!`,
|
|
132
|
+
);
|
|
132
133
|
}
|
|
133
134
|
});
|
|
134
135
|
} catch (err) {
|
|
@@ -143,7 +144,9 @@ function startAdapter(options) {
|
|
|
143
144
|
if (!err) {
|
|
144
145
|
adapter.sendTo(obj.from, obj.command, true, obj.callback);
|
|
145
146
|
updateUsers();
|
|
146
|
-
adapter.log.warn(
|
|
147
|
+
adapter.log.warn(
|
|
148
|
+
'List of saved users has been wiped. Every User has to reauthenticate with the new password!',
|
|
149
|
+
);
|
|
147
150
|
}
|
|
148
151
|
});
|
|
149
152
|
} catch (err) {
|
|
@@ -152,7 +155,7 @@ function startAdapter(options) {
|
|
|
152
155
|
}
|
|
153
156
|
} else if (obj.command === 'sendNotification') {
|
|
154
157
|
processNotification(obj);
|
|
155
|
-
} else{
|
|
158
|
+
} else {
|
|
156
159
|
processMessage(obj);
|
|
157
160
|
}
|
|
158
161
|
}
|
|
@@ -206,13 +209,15 @@ function startAdapter(options) {
|
|
|
206
209
|
const webserver = new WebServer({
|
|
207
210
|
app: handleWebHook,
|
|
208
211
|
adapter,
|
|
209
|
-
secure: adapter.config.secure
|
|
212
|
+
secure: adapter.config.secure,
|
|
210
213
|
});
|
|
211
214
|
|
|
212
215
|
server.server = await webserver.init();
|
|
213
216
|
} catch (err) {
|
|
214
217
|
adapter.log.error(`Cannot create webserver: ${err}`);
|
|
215
|
-
adapter.terminate
|
|
218
|
+
adapter.terminate
|
|
219
|
+
? adapter.terminate(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION)
|
|
220
|
+
: process.exit(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION);
|
|
216
221
|
return;
|
|
217
222
|
}
|
|
218
223
|
if (server.server) {
|
|
@@ -222,37 +227,51 @@ function startAdapter(options) {
|
|
|
222
227
|
let serverPort = adapter.config.port;
|
|
223
228
|
server.server.on('error', e => {
|
|
224
229
|
if (e.toString().includes('EACCES') && serverPort <= 1024) {
|
|
225
|
-
adapter.log.error(
|
|
226
|
-
`
|
|
227
|
-
|
|
230
|
+
adapter.log.error(
|
|
231
|
+
`node.js process has no rights to start server on the port ${serverPort}.\n` +
|
|
232
|
+
`Do you know that on linux you need special permissions for ports under 1024?\n` +
|
|
233
|
+
`You can call in shell following scrip to allow it for node.js: "iobroker fix"`,
|
|
228
234
|
);
|
|
229
235
|
} else {
|
|
230
|
-
adapter.log.error(
|
|
236
|
+
adapter.log.error(
|
|
237
|
+
`Cannot start server on ${adapter.config.bind || '0.0.0.0'}:${serverPort}: ${e}`,
|
|
238
|
+
);
|
|
231
239
|
}
|
|
232
240
|
if (!serverListening) {
|
|
233
|
-
adapter.terminate
|
|
241
|
+
adapter.terminate
|
|
242
|
+
? adapter.terminate(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION)
|
|
243
|
+
: process.exit(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION);
|
|
234
244
|
}
|
|
235
245
|
});
|
|
236
246
|
|
|
237
|
-
adapter.getPort(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
+
adapter.getPort(
|
|
248
|
+
adapter.config.port,
|
|
249
|
+
!adapter.config.bind || adapter.config.bind === '0.0.0.0'
|
|
250
|
+
? undefined
|
|
251
|
+
: adapter.config.bind || undefined,
|
|
252
|
+
port => {
|
|
253
|
+
if (parseInt(port, 10) !== adapter.config.port && !adapter.config.findNextPort) {
|
|
254
|
+
adapter.log.error(`port ${adapter.config.port} already in use`);
|
|
255
|
+
adapter.terminate ? adapter.terminate() : process.exit(1);
|
|
256
|
+
}
|
|
257
|
+
serverPort = port;
|
|
258
|
+
|
|
259
|
+
server.server.listen(
|
|
260
|
+
port,
|
|
261
|
+
!adapter.config.bind || adapter.config.bind === '0.0.0.0'
|
|
262
|
+
? undefined
|
|
263
|
+
: adapter.config.bind || undefined,
|
|
264
|
+
() => (serverListening = true),
|
|
265
|
+
);
|
|
266
|
+
adapter.log.info(`https server listening on port ${port}`);
|
|
247
267
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
268
|
+
main().catch(e => adapter.log.error(`Cannot start adapter: ${e}`));
|
|
269
|
+
},
|
|
270
|
+
);
|
|
251
271
|
}
|
|
252
272
|
});
|
|
253
273
|
} else {
|
|
254
|
-
main()
|
|
255
|
-
.catch(e => adapter.log.error(`Cannot start adapter: ${e}`));
|
|
274
|
+
main().catch(e => adapter.log.error(`Cannot start adapter: ${e}`));
|
|
256
275
|
}
|
|
257
276
|
});
|
|
258
277
|
|
|
@@ -272,8 +291,14 @@ function startAdapter(options) {
|
|
|
272
291
|
if (adapter && adapter.config) {
|
|
273
292
|
if (adapter.config.restarting !== '') {
|
|
274
293
|
// default text
|
|
275
|
-
if (
|
|
276
|
-
|
|
294
|
+
if (
|
|
295
|
+
adapter.config.restarting === '_' ||
|
|
296
|
+
adapter.config.restarting === null ||
|
|
297
|
+
adapter.config.restarting === undefined
|
|
298
|
+
) {
|
|
299
|
+
sendSystemMessage(
|
|
300
|
+
adapter.config.rememberUsers ? _('Restarting...') : _('Restarting... Reauthenticate!'),
|
|
301
|
+
);
|
|
277
302
|
} else {
|
|
278
303
|
sendSystemMessage(adapter.config.restarting);
|
|
279
304
|
}
|
|
@@ -297,7 +322,9 @@ function startAdapter(options) {
|
|
|
297
322
|
if (!state.ack) {
|
|
298
323
|
if (id.endsWith('communicate.response')) {
|
|
299
324
|
if (typeof state.val === 'object') {
|
|
300
|
-
adapter.log.error(
|
|
325
|
+
adapter.log.error(
|
|
326
|
+
`communicate.response only supports passing a message to send as string. You provided ${JSON.stringify(state.val)}. Please use "communicate.responseJson" instead with a stringified JSON object!`,
|
|
327
|
+
);
|
|
301
328
|
return;
|
|
302
329
|
}
|
|
303
330
|
|
|
@@ -306,7 +333,9 @@ function startAdapter(options) {
|
|
|
306
333
|
await adapter.setStateAsync('communicate.response', { val: state.val, ack: true });
|
|
307
334
|
} else if (id.endsWith('communicate.responseSilent')) {
|
|
308
335
|
if (typeof state.val === 'object') {
|
|
309
|
-
adapter.log.error(
|
|
336
|
+
adapter.log.error(
|
|
337
|
+
`communicate.responseSilent only supports passing a message to send as string. You provided ${JSON.stringify(state.val)}. Please use "communicate.responseSilentJson" instead with a stringified JSON object!`,
|
|
338
|
+
);
|
|
310
339
|
return;
|
|
311
340
|
}
|
|
312
341
|
// Send to someone this message
|
|
@@ -330,7 +359,9 @@ function startAdapter(options) {
|
|
|
330
359
|
await sendMessage(val, null, null, { disable_notification: true });
|
|
331
360
|
await adapter.setStateAsync('communicate.responseSilent', { val: state.val, ack: true });
|
|
332
361
|
} catch (err) {
|
|
333
|
-
adapter.log.error(
|
|
362
|
+
adapter.log.error(
|
|
363
|
+
`could not parse Json in communicate.responseSilentJon state: ${err.message}`,
|
|
364
|
+
);
|
|
334
365
|
}
|
|
335
366
|
} else if (id.endsWith('communicate.requestResponse')) {
|
|
336
367
|
try {
|
|
@@ -345,15 +376,12 @@ function startAdapter(options) {
|
|
|
345
376
|
}
|
|
346
377
|
|
|
347
378
|
// Send to someone this message
|
|
348
|
-
await sendMessage(
|
|
349
|
-
text,
|
|
350
|
-
null,
|
|
351
|
-
chatIdState ? chatIdState.val : null,
|
|
352
|
-
options,
|
|
353
|
-
);
|
|
379
|
+
await sendMessage(text, null, chatIdState ? chatIdState.val : null, options);
|
|
354
380
|
await adapter.setStateAsync('communicate.requestResponse', { val: state.val, ack: true });
|
|
355
381
|
} catch (err) {
|
|
356
|
-
adapter.log.error(
|
|
382
|
+
adapter.log.error(
|
|
383
|
+
`could not parse Json in communicate.responseSilentJon state: ${err.message}`,
|
|
384
|
+
);
|
|
357
385
|
}
|
|
358
386
|
}
|
|
359
387
|
} else if (commands[id] && commands[id].report) {
|
|
@@ -379,8 +407,12 @@ function startAdapter(options) {
|
|
|
379
407
|
});
|
|
380
408
|
|
|
381
409
|
adapter.on('objectChange', (id, obj) => {
|
|
382
|
-
if (
|
|
383
|
-
obj
|
|
410
|
+
if (
|
|
411
|
+
obj &&
|
|
412
|
+
obj.common &&
|
|
413
|
+
obj.common.custom &&
|
|
414
|
+
obj.common.custom[adapter.namespace] &&
|
|
415
|
+
obj.common.custom[adapter.namespace].enabled
|
|
384
416
|
) {
|
|
385
417
|
const alias = getName(obj);
|
|
386
418
|
if (!commands[id]) {
|
|
@@ -388,19 +420,20 @@ function startAdapter(options) {
|
|
|
388
420
|
setImmediate(() => adapter.subscribeForeignStates(id));
|
|
389
421
|
}
|
|
390
422
|
|
|
391
|
-
commands[id]
|
|
392
|
-
commands[id].type
|
|
423
|
+
commands[id] = obj.common.custom[adapter.namespace];
|
|
424
|
+
commands[id].type = obj.common.type;
|
|
393
425
|
commands[id].states = parseStates(obj.common.states);
|
|
394
|
-
commands[id].unit
|
|
395
|
-
commands[id].min
|
|
396
|
-
commands[id].max
|
|
426
|
+
commands[id].unit = obj.common && obj.common.unit;
|
|
427
|
+
commands[id].min = obj.common && obj.common.min;
|
|
428
|
+
commands[id].max = obj.common && obj.common.max;
|
|
397
429
|
commands[id].recipients = obj.common && obj.common.recipients;
|
|
398
|
-
commands[id].alias
|
|
430
|
+
commands[id].alias = alias;
|
|
399
431
|
|
|
400
432
|
// read actual state to detect changes
|
|
401
433
|
if (commands[id].reportChanges) {
|
|
402
|
-
adapter
|
|
403
|
-
.
|
|
434
|
+
adapter
|
|
435
|
+
.getForeignStateAsync(id)
|
|
436
|
+
.then(state => (commands[id].lastState = state ? state.val : undefined));
|
|
404
437
|
}
|
|
405
438
|
} else if (commands[id]) {
|
|
406
439
|
adapter.log.debug(`Removed command: ${id}`);
|
|
@@ -423,30 +456,29 @@ function startAdapter(options) {
|
|
|
423
456
|
/**
|
|
424
457
|
* Send message to all system users
|
|
425
458
|
*
|
|
426
|
-
* @param
|
|
427
|
-
* @param
|
|
428
|
-
* @returns
|
|
459
|
+
* @param text text to send
|
|
460
|
+
* @param options additional options, e.g. parse_mode
|
|
461
|
+
* @returns
|
|
429
462
|
*/
|
|
430
|
-
async function sendSystemMessage(text, options= {}) {
|
|
463
|
+
async function sendSystemMessage(text, options = {}) {
|
|
431
464
|
const _users = Object.keys(users)
|
|
432
465
|
.filter(id => users[id].sysMessages !== false)
|
|
433
|
-
.map(id => adapter.config.useUsername ? users[id].userName : users[id].firstName);
|
|
466
|
+
.map(id => (adapter.config.useUsername ? users[id].userName : users[id].firstName));
|
|
434
467
|
|
|
435
|
-
await sendMessage(text, _users, null, {...options, disable_notification: true});
|
|
468
|
+
await sendMessage(text, _users, null, { ...options, disable_notification: true });
|
|
436
469
|
}
|
|
437
470
|
|
|
438
471
|
function getStatus(id, state) {
|
|
439
472
|
if (!state) {
|
|
440
|
-
state = {val: 'State not set'};
|
|
473
|
+
state = { val: 'State not set' };
|
|
441
474
|
}
|
|
442
475
|
if (commands[id].type === 'boolean') {
|
|
443
476
|
return `${commands[id].alias} => ${state.val ? commands[id].onStatus || _('ON-Status') : commands[id].offStatus || _('OFF-Status')}`;
|
|
444
|
-
} else {
|
|
445
|
-
if (commands[id].states && commands[id].states[state.val] !== undefined) {
|
|
446
|
-
state.val = commands[id].states[state.val];
|
|
447
|
-
}
|
|
448
|
-
return `${commands[id].alias} => ${state.val}${commands[id].unit ? ` ${commands[id].unit}` : ''}`;
|
|
449
477
|
}
|
|
478
|
+
if (commands[id].states && commands[id].states[state.val] !== undefined) {
|
|
479
|
+
state.val = commands[id].states[state.val];
|
|
480
|
+
}
|
|
481
|
+
return `${commands[id].alias} => ${state.val}${commands[id].unit ? ` ${commands[id].unit}` : ''}`;
|
|
450
482
|
}
|
|
451
483
|
|
|
452
484
|
function connectionState(connected, logSuccess) {
|
|
@@ -454,17 +486,20 @@ function connectionState(connected, logSuccess) {
|
|
|
454
486
|
|
|
455
487
|
function checkConnection() {
|
|
456
488
|
pollConnectionStatus = null;
|
|
457
|
-
bot &&
|
|
458
|
-
.
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
489
|
+
bot &&
|
|
490
|
+
bot.getMe &&
|
|
491
|
+
bot
|
|
492
|
+
.getMe()
|
|
493
|
+
.then(data => {
|
|
494
|
+
adapter.log.debug(`getMe (reconnect): ${JSON.stringify(data)}`);
|
|
495
|
+
connectionState(true, errorCounter > 0);
|
|
496
|
+
})
|
|
497
|
+
.catch(error => {
|
|
498
|
+
errorCounter % 10 === 0 && adapter.log.error(`getMe (reconnect #${errorCounter}) Error:${error}`);
|
|
499
|
+
errorCounter++;
|
|
500
|
+
pollConnectionStatus && clearTimeout(pollConnectionStatus);
|
|
501
|
+
pollConnectionStatus = setTimeout(checkConnection, 1000);
|
|
502
|
+
});
|
|
468
503
|
}
|
|
469
504
|
|
|
470
505
|
if (connected && logSuccess) {
|
|
@@ -490,17 +525,22 @@ function parseStates(states) {
|
|
|
490
525
|
function getName(obj) {
|
|
491
526
|
if (obj.common.custom[adapter.namespace].alias) {
|
|
492
527
|
return obj.common.custom[adapter.namespace].alias;
|
|
493
|
-
} else {
|
|
494
|
-
let name = obj.common.name;
|
|
495
|
-
if (typeof name === 'object') {
|
|
496
|
-
name = name[systemLang] || name.en;
|
|
497
|
-
}
|
|
498
|
-
return name || obj._id;
|
|
499
528
|
}
|
|
529
|
+
let name = obj.common.name;
|
|
530
|
+
if (typeof name === 'object') {
|
|
531
|
+
name = name[systemLang] || name.en;
|
|
532
|
+
}
|
|
533
|
+
return name || obj._id;
|
|
500
534
|
}
|
|
501
535
|
|
|
502
536
|
const actions = [
|
|
503
|
-
'typing',
|
|
537
|
+
'typing',
|
|
538
|
+
'upload_photo',
|
|
539
|
+
'upload_video',
|
|
540
|
+
'record_video',
|
|
541
|
+
'record_audio',
|
|
542
|
+
'upload_document',
|
|
543
|
+
'find_location',
|
|
504
544
|
];
|
|
505
545
|
|
|
506
546
|
function handleWebHook(req, res) {
|
|
@@ -531,9 +571,11 @@ function handleWebHook(req, res) {
|
|
|
531
571
|
body += data;
|
|
532
572
|
if (body.length > 100_000) {
|
|
533
573
|
res.writeHead(413, 'Request Entity Too Large', {
|
|
534
|
-
'Content-Type': 'text/html'
|
|
574
|
+
'Content-Type': 'text/html',
|
|
535
575
|
});
|
|
536
|
-
res.end(
|
|
576
|
+
res.end(
|
|
577
|
+
'<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>',
|
|
578
|
+
);
|
|
537
579
|
}
|
|
538
580
|
});
|
|
539
581
|
req.on('end', () => {
|
|
@@ -558,34 +600,52 @@ function handleWebHook(req, res) {
|
|
|
558
600
|
function saveSendRequest(msg) {
|
|
559
601
|
adapter.log.debug(`Request [saveSendRequest]: ${JSON.stringify(msg)}`);
|
|
560
602
|
|
|
561
|
-
if
|
|
603
|
+
if (typeof msg === 'object') {
|
|
562
604
|
if (adapter.config.storeRawRequest) {
|
|
563
|
-
adapter.setState(
|
|
564
|
-
|
|
605
|
+
adapter.setState(
|
|
606
|
+
'communicate.botSendRaw',
|
|
607
|
+
JSON.stringify(msg, null, 2),
|
|
608
|
+
true,
|
|
609
|
+
err => err && adapter.log.error(err),
|
|
610
|
+
);
|
|
565
611
|
}
|
|
566
612
|
|
|
567
613
|
if (msg?.message_id) {
|
|
568
|
-
adapter.setState(
|
|
569
|
-
|
|
614
|
+
adapter.setState(
|
|
615
|
+
'communicate.botSendMessageId',
|
|
616
|
+
msg.message_id,
|
|
617
|
+
true,
|
|
618
|
+
err => err && adapter.log.error(err),
|
|
619
|
+
);
|
|
570
620
|
}
|
|
571
621
|
|
|
572
622
|
if (msg?.message_thread_id) {
|
|
573
|
-
adapter.setState(
|
|
574
|
-
|
|
623
|
+
adapter.setState(
|
|
624
|
+
'communicate.botSendMessageThreadId',
|
|
625
|
+
msg.message_thread_id,
|
|
626
|
+
true,
|
|
627
|
+
err => err && adapter.log.error(err),
|
|
628
|
+
);
|
|
575
629
|
}
|
|
576
630
|
|
|
577
631
|
if (msg?.chat && msg.chat.id) {
|
|
578
|
-
adapter.setState(
|
|
579
|
-
|
|
632
|
+
adapter.setState(
|
|
633
|
+
'communicate.botSendChatId',
|
|
634
|
+
msg.chat.id.toString(),
|
|
635
|
+
true,
|
|
636
|
+
err => err && adapter.log.error(err),
|
|
637
|
+
);
|
|
580
638
|
}
|
|
581
639
|
}
|
|
582
640
|
}
|
|
583
641
|
|
|
584
642
|
function _sendMessageHelper(dest, name, text, options) {
|
|
585
|
-
return new Promise(
|
|
643
|
+
return new Promise(resolve => {
|
|
586
644
|
const messageIds = {};
|
|
587
645
|
if (options && options.chatId !== undefined && options.user === undefined) {
|
|
588
|
-
options.user = adapter.config.useUsername
|
|
646
|
+
options.user = adapter.config.useUsername
|
|
647
|
+
? users[options.chatId].userName
|
|
648
|
+
: users[options.chatId].firstName;
|
|
589
649
|
}
|
|
590
650
|
// to push chatId value for the group chats - useful to process the errors, and list of processed messages.
|
|
591
651
|
if (options.chatId === undefined && options.user === undefined && name === 'chat' && dest) {
|
|
@@ -593,7 +653,16 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
593
653
|
}
|
|
594
654
|
if (options && options.editMessageReplyMarkup !== undefined) {
|
|
595
655
|
adapter.log.debug(`Send editMessageReplyMarkup to "${name}"`);
|
|
596
|
-
bot &&
|
|
656
|
+
bot &&
|
|
657
|
+
executeSending(
|
|
658
|
+
() =>
|
|
659
|
+
bot.editMessageReplyMarkup(
|
|
660
|
+
options.editMessageReplyMarkup.reply_markup,
|
|
661
|
+
options.editMessageReplyMarkup.options,
|
|
662
|
+
),
|
|
663
|
+
options,
|
|
664
|
+
resolve,
|
|
665
|
+
);
|
|
597
666
|
} else if (options && options.editMessageText !== undefined) {
|
|
598
667
|
adapter.log.debug(`Send editMessageText to "${name}"`);
|
|
599
668
|
bot && executeSending(() => bot.editMessageText(text, options.editMessageText.options), options, resolve);
|
|
@@ -604,8 +673,7 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
604
673
|
if (
|
|
605
674
|
(typeof text === 'string' &&
|
|
606
675
|
text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) &&
|
|
607
|
-
(fs.existsSync(text) || text.match(/^(https|http)/i))
|
|
608
|
-
) ||
|
|
676
|
+
(fs.existsSync(text) || text.match(/^(https|http)/i))) ||
|
|
609
677
|
(options && options.type === 'photo')
|
|
610
678
|
) {
|
|
611
679
|
mediaInput = {
|
|
@@ -637,7 +705,9 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
637
705
|
media: text,
|
|
638
706
|
};
|
|
639
707
|
} else if (
|
|
640
|
-
(typeof text === 'string' &&
|
|
708
|
+
(typeof text === 'string' &&
|
|
709
|
+
text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) &&
|
|
710
|
+
fs.existsSync(text)) ||
|
|
641
711
|
(options && options.type === 'document')
|
|
642
712
|
) {
|
|
643
713
|
mediaInput = {
|
|
@@ -659,7 +729,11 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
659
729
|
|
|
660
730
|
try {
|
|
661
731
|
const attachName = String(0);
|
|
662
|
-
const [formData, fileId] = bot._formatSendData(
|
|
732
|
+
const [formData, fileId] = bot._formatSendData(
|
|
733
|
+
attachName,
|
|
734
|
+
mediaInput.media,
|
|
735
|
+
mediaInput.fileOptions,
|
|
736
|
+
);
|
|
663
737
|
if (formData) {
|
|
664
738
|
opts.formData[attachName] = formData[attachName];
|
|
665
739
|
payload.media = `attach://${attachName}`;
|
|
@@ -673,28 +747,73 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
673
747
|
opts.qs.media = JSON.stringify(payload);
|
|
674
748
|
bot && executeSending(() => bot._request('editMessageMedia', opts), options, resolve);
|
|
675
749
|
} else {
|
|
676
|
-
adapter.log.error(
|
|
750
|
+
adapter.log.error(
|
|
751
|
+
`Cannot send editMessageMedia [chatId - ${options.chatId}]: unsupported media type`,
|
|
752
|
+
);
|
|
677
753
|
options = null;
|
|
678
754
|
resolve(JSON.stringify(messageIds));
|
|
679
755
|
}
|
|
680
756
|
} else {
|
|
681
|
-
adapter.log.error(
|
|
757
|
+
adapter.log.error(
|
|
758
|
+
`Cannot send editMessageMedia [chatId - ${options.chatId}]: no media found. "text" may not be empty`,
|
|
759
|
+
);
|
|
682
760
|
options = null;
|
|
683
761
|
resolve(JSON.stringify(messageIds));
|
|
684
762
|
}
|
|
685
763
|
} else if (options && options.editMessageCaption !== undefined) {
|
|
686
764
|
adapter.log.debug(`Send editMessageCaption to "${name}"`);
|
|
687
|
-
bot &&
|
|
765
|
+
bot &&
|
|
766
|
+
executeSending(
|
|
767
|
+
() => bot.editMessageCaption(text, options.editMessageCaption.options),
|
|
768
|
+
options,
|
|
769
|
+
resolve,
|
|
770
|
+
);
|
|
688
771
|
} else if (options && options.deleteMessage !== undefined) {
|
|
689
772
|
adapter.log.debug(`Send deleteMessage to "${name}"`);
|
|
690
|
-
bot &&
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
773
|
+
bot &&
|
|
774
|
+
executeSending(
|
|
775
|
+
() =>
|
|
776
|
+
bot.deleteMessage(
|
|
777
|
+
options.deleteMessage.options.chat_id,
|
|
778
|
+
options.deleteMessage.options.message_id,
|
|
779
|
+
),
|
|
780
|
+
options,
|
|
781
|
+
resolve,
|
|
782
|
+
);
|
|
783
|
+
} else if (
|
|
784
|
+
options &&
|
|
785
|
+
options.latitude !== undefined &&
|
|
786
|
+
options.longitude !== undefined &&
|
|
787
|
+
options.title !== undefined &&
|
|
788
|
+
options.address !== undefined
|
|
789
|
+
) {
|
|
790
|
+
adapter.log.debug(`Send venue to "${name}": ${options.latitude},${options.longitude}`);
|
|
791
|
+
bot &&
|
|
792
|
+
executeSending(
|
|
793
|
+
() =>
|
|
794
|
+
bot.sendVenue(
|
|
795
|
+
dest,
|
|
796
|
+
parseFloat(options.latitude),
|
|
797
|
+
parseFloat(options.longitude),
|
|
798
|
+
options.title,
|
|
799
|
+
options.address,
|
|
800
|
+
options,
|
|
801
|
+
),
|
|
802
|
+
options,
|
|
803
|
+
resolve,
|
|
804
|
+
);
|
|
805
|
+
} else if (options && options.latitude !== undefined && options.longitude !== undefined) {
|
|
806
|
+
adapter.log.debug(`Send location to "${name}": ${options.latitude},${options.longitude}`);
|
|
807
|
+
bot &&
|
|
808
|
+
executeSending(
|
|
809
|
+
() => bot.sendLocation(dest, parseFloat(options.latitude), parseFloat(options.longitude), options),
|
|
810
|
+
options,
|
|
811
|
+
resolve,
|
|
812
|
+
);
|
|
694
813
|
} else if (options && options.type === 'mediagroup') {
|
|
695
814
|
adapter.log.debug(`Send media group to "${name}": `);
|
|
696
815
|
if (bot) {
|
|
697
|
-
const {media: fileNames} = options;
|
|
816
|
+
const { media: fileNames } = options;
|
|
698
817
|
if (fileNames instanceof Array) {
|
|
699
818
|
bot.sendChatAction(dest, 'upload_photo')
|
|
700
819
|
.then(() => {
|
|
@@ -702,7 +821,7 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
702
821
|
const filesAsArray = fileNames
|
|
703
822
|
.map(element => {
|
|
704
823
|
try {
|
|
705
|
-
return {type: 'photo', media: fs.readFileSync(element)};
|
|
824
|
+
return { type: 'photo', media: fs.readFileSync(element) };
|
|
706
825
|
} catch (err) {
|
|
707
826
|
adapter.log.error(`Cannot read file ${element}: ${err}`);
|
|
708
827
|
return undefined;
|
|
@@ -739,55 +858,62 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
739
858
|
} else if (text && typeof text === 'string' && actions.includes(text)) {
|
|
740
859
|
adapter.log.debug(`Send action to "${name}": ${text}`);
|
|
741
860
|
bot && executeSending(() => bot.sendChatAction(dest, text), options, resolve);
|
|
742
|
-
} else if (
|
|
861
|
+
} else if (
|
|
862
|
+
text &&
|
|
863
|
+
((typeof text === 'string' && text.match(/\.webp$/i) && fs.existsSync(text)) ||
|
|
864
|
+
(options && options.type === 'sticker'))
|
|
865
|
+
) {
|
|
743
866
|
if (typeof text === 'string') {
|
|
744
867
|
adapter.log.debug(`Send sticker to "${name}": ${text}`);
|
|
745
868
|
} else {
|
|
746
869
|
adapter.log.debug(`Send sticker to "${name}": ${text.length} bytes`);
|
|
747
870
|
}
|
|
748
871
|
bot && executeSending(() => bot.sendSticker(dest, text, options), options, resolve);
|
|
749
|
-
} else if (
|
|
872
|
+
} else if (
|
|
873
|
+
text &&
|
|
874
|
+
((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) ||
|
|
875
|
+
(options && options.type === 'animation'))
|
|
876
|
+
) {
|
|
750
877
|
if (typeof text === 'string') {
|
|
751
878
|
adapter.log.debug(`Send animation to "${name}": ${text}`);
|
|
752
879
|
} else {
|
|
753
880
|
adapter.log.debug(`Send animation to "${name}": ${text.length} bytes`);
|
|
754
881
|
}
|
|
755
882
|
bot && executeSending(() => bot.sendAnimation(dest, text, options), options, resolve);
|
|
756
|
-
} else if (
|
|
883
|
+
} else if (
|
|
884
|
+
text &&
|
|
885
|
+
((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) ||
|
|
886
|
+
(options && options.type === 'video'))
|
|
887
|
+
) {
|
|
757
888
|
if (typeof text === 'string') {
|
|
758
889
|
adapter.log.debug(`Send video to "${name}": ${text}`);
|
|
759
890
|
} else {
|
|
760
891
|
adapter.log.debug(`Send video to "${name}": ${text.length} bytes`);
|
|
761
892
|
}
|
|
762
893
|
bot && executeSending(() => bot.sendVideo(dest, text, options), options, resolve);
|
|
763
|
-
} else if (
|
|
764
|
-
|
|
894
|
+
} else if (
|
|
895
|
+
text &&
|
|
896
|
+
((typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) ||
|
|
897
|
+
(options && options.type === 'document'))
|
|
898
|
+
) {
|
|
899
|
+
adapter.log.debug(`Send document to "${name}": ${typeof text === 'string' ? text : text.length}`);
|
|
765
900
|
bot && executeSending(() => bot.sendDocument(dest, text, options), options, resolve);
|
|
766
901
|
} else if (
|
|
767
902
|
text &&
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
text.match(/\.(wav|mp3|ogg)$/i) &&
|
|
771
|
-
fs.existsSync(text)
|
|
772
|
-
) ||
|
|
773
|
-
(options && options?.type === 'audio')
|
|
774
|
-
)
|
|
903
|
+
((typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) ||
|
|
904
|
+
(options && options?.type === 'audio'))
|
|
775
905
|
) {
|
|
776
|
-
adapter.log.debug(`Send audio to "${name}": ${
|
|
906
|
+
adapter.log.debug(`Send audio to "${name}": ${typeof text === 'string' ? text : text.length}`);
|
|
777
907
|
|
|
778
908
|
bot && executeSending(() => bot.sendAudio(dest, text, options), options, resolve);
|
|
779
909
|
} else if (
|
|
780
910
|
text &&
|
|
781
|
-
(
|
|
782
|
-
(
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
(fs.existsSync(text) || text.match(/^(https|http)/i))
|
|
786
|
-
) ||
|
|
787
|
-
(options && options.type === 'photo') // if the type of message is photo
|
|
788
|
-
)
|
|
911
|
+
((typeof text === 'string' && // if the message is a string, and it is a path to file or URL
|
|
912
|
+
text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) &&
|
|
913
|
+
(fs.existsSync(text) || text.match(/^(https|http)/i))) ||
|
|
914
|
+
(options && options.type === 'photo')) // if the type of message is photo
|
|
789
915
|
) {
|
|
790
|
-
adapter.log.debug(`Send photo to "${name}": ${
|
|
916
|
+
adapter.log.debug(`Send photo to "${name}": ${typeof text === 'string' ? text : text.length}`);
|
|
791
917
|
|
|
792
918
|
bot && executeSending(() => bot.sendPhoto(dest, text, options), options, resolve);
|
|
793
919
|
} else if (options && options.answerCallbackQuery !== undefined) {
|
|
@@ -798,7 +924,16 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
798
924
|
if (bot && callbackQueryId[options.chatId]) {
|
|
799
925
|
const originalChatId = callbackQueryId[options.chatId].id;
|
|
800
926
|
delete callbackQueryId[options.chatId];
|
|
801
|
-
executeSending(
|
|
927
|
+
executeSending(
|
|
928
|
+
() =>
|
|
929
|
+
bot.answerCallbackQuery(
|
|
930
|
+
originalChatId,
|
|
931
|
+
options.answerCallbackQuery.text,
|
|
932
|
+
options.answerCallbackQuery.showAlert,
|
|
933
|
+
),
|
|
934
|
+
options,
|
|
935
|
+
resolve,
|
|
936
|
+
);
|
|
802
937
|
}
|
|
803
938
|
} else {
|
|
804
939
|
adapter.log.debug(`Send message to [${name}]: "${text}"`);
|
|
@@ -822,8 +957,12 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
822
957
|
|
|
823
958
|
/**
|
|
824
959
|
* executes the given method and handles, what to do next
|
|
960
|
+
*
|
|
961
|
+
* @param action
|
|
962
|
+
* @param options
|
|
963
|
+
* @param resolve
|
|
825
964
|
*/
|
|
826
|
-
function executeSending(action, options, resolve){
|
|
965
|
+
function executeSending(action, options, resolve) {
|
|
827
966
|
// create an empty object, to store chat id and message id of successfully sent messages
|
|
828
967
|
const messageIds = {};
|
|
829
968
|
action()
|
|
@@ -834,7 +973,11 @@ function executeSending(action, options, resolve){
|
|
|
834
973
|
if (response?.message_id) {
|
|
835
974
|
// The chatId is mostly used in code, instead of chat_id.
|
|
836
975
|
messageIds[options.chat_id ? options.chat_id : options.chatId] = response.message_id;
|
|
837
|
-
} else if (
|
|
976
|
+
} else if (
|
|
977
|
+
typeof response === 'boolean' &&
|
|
978
|
+
options?.deleteMessage?.options?.chat_id &&
|
|
979
|
+
options?.deleteMessage?.options?.message_id
|
|
980
|
+
) {
|
|
838
981
|
messageIds[options.deleteMessage.options.chat_id] = options.deleteMessage.options.message_id;
|
|
839
982
|
}
|
|
840
983
|
// puts ids to the ioBroker database
|
|
@@ -848,9 +991,11 @@ function executeSending(action, options, resolve){
|
|
|
848
991
|
})
|
|
849
992
|
.catch(error => {
|
|
850
993
|
// add the error to the message ids object
|
|
851
|
-
messageIds.error = {[options.chat_id ? options.chat_id : options.chatId]
|
|
994
|
+
messageIds.error = { [options.chat_id ? options.chat_id : options.chatId]: error };
|
|
852
995
|
// log error to the system
|
|
853
|
-
adapter.log.error(
|
|
996
|
+
adapter.log.error(
|
|
997
|
+
`Failed sending [${options.chatId ? 'chatId' : 'user'} - ${options.chatId ? options.chatId : options.user}]: ${error}`,
|
|
998
|
+
);
|
|
854
999
|
options = null;
|
|
855
1000
|
// send the successfully sent messages as callback
|
|
856
1001
|
resolve(JSON.stringify(messageIds));
|
|
@@ -864,7 +1009,13 @@ function sendMessage(text, user, chatId, options) {
|
|
|
864
1009
|
return Promise.resolve({});
|
|
865
1010
|
}
|
|
866
1011
|
|
|
867
|
-
if (
|
|
1012
|
+
if (
|
|
1013
|
+
text &&
|
|
1014
|
+
typeof text === 'object' &&
|
|
1015
|
+
text.text !== undefined &&
|
|
1016
|
+
typeof text.text === 'string' &&
|
|
1017
|
+
options === undefined
|
|
1018
|
+
) {
|
|
868
1019
|
options = text;
|
|
869
1020
|
text = options.text;
|
|
870
1021
|
if (options.chatId) {
|
|
@@ -909,14 +1060,19 @@ function sendMessage(text, user, chatId, options) {
|
|
|
909
1060
|
}
|
|
910
1061
|
if (chatId) {
|
|
911
1062
|
tPromiseList.push(_sendMessageHelper(chatId, 'chat', text, options));
|
|
912
|
-
return Promise.all(tPromiseList)
|
|
913
|
-
.catch(e => e);
|
|
1063
|
+
return Promise.all(tPromiseList).catch(e => e);
|
|
914
1064
|
} else if (user) {
|
|
915
1065
|
if (typeof user !== 'string' && !(user instanceof Array)) {
|
|
916
1066
|
adapter.log.warn(`Invalid type of user parameter: ${typeof user}. Expected is string or array.`);
|
|
917
1067
|
}
|
|
918
1068
|
|
|
919
|
-
const userArray = Array.isArray(user)
|
|
1069
|
+
const userArray = Array.isArray(user)
|
|
1070
|
+
? user
|
|
1071
|
+
: (user || '')
|
|
1072
|
+
.toString()
|
|
1073
|
+
.split(/[,;\s]/)
|
|
1074
|
+
.map(u => u.trim())
|
|
1075
|
+
.filter(u => !!u);
|
|
920
1076
|
let matches = 0;
|
|
921
1077
|
userArray.forEach(userName => {
|
|
922
1078
|
for (const id in users) {
|
|
@@ -924,8 +1080,10 @@ function sendMessage(text, user, chatId, options) {
|
|
|
924
1080
|
continue;
|
|
925
1081
|
}
|
|
926
1082
|
|
|
927
|
-
if (
|
|
928
|
-
|
|
1083
|
+
if (
|
|
1084
|
+
(adapter.config.useUsername && users[id].userName === userName) ||
|
|
1085
|
+
(!adapter.config.useUsername && users[id].firstName === userName)
|
|
1086
|
+
) {
|
|
929
1087
|
if (options) {
|
|
930
1088
|
options.chatId = id;
|
|
931
1089
|
}
|
|
@@ -940,8 +1098,7 @@ function sendMessage(text, user, chatId, options) {
|
|
|
940
1098
|
adapter.log.warn(`${userArray.length - matches} of ${userArray.length} recipients are unknown!`);
|
|
941
1099
|
}
|
|
942
1100
|
|
|
943
|
-
return Promise.all(tPromiseList)
|
|
944
|
-
.catch(e => e);
|
|
1101
|
+
return Promise.all(tPromiseList).catch(e => e);
|
|
945
1102
|
}
|
|
946
1103
|
|
|
947
1104
|
const m = typeof text === 'string' ? text.match(/^@(.+?)\b/) : null;
|
|
@@ -955,7 +1112,10 @@ function sendMessage(text, user, chatId, options) {
|
|
|
955
1112
|
if (!Object.prototype.hasOwnProperty.call(users, id_t)) {
|
|
956
1113
|
continue;
|
|
957
1114
|
}
|
|
958
|
-
if (
|
|
1115
|
+
if (
|
|
1116
|
+
(adapter.config.useUsername && users[id_t].userName.match(re)) ||
|
|
1117
|
+
(!adapter.config.useUsername && users[id_t].firstName.match(re))
|
|
1118
|
+
) {
|
|
959
1119
|
id = id_t;
|
|
960
1120
|
break;
|
|
961
1121
|
}
|
|
@@ -972,12 +1132,18 @@ function sendMessage(text, user, chatId, options) {
|
|
|
972
1132
|
if (options) {
|
|
973
1133
|
options.chatId = id;
|
|
974
1134
|
}
|
|
975
|
-
tPromiseList.push(
|
|
1135
|
+
tPromiseList.push(
|
|
1136
|
+
_sendMessageHelper(
|
|
1137
|
+
id,
|
|
1138
|
+
adapter.config.useUsername ? users[id].userName : users[id].firstName,
|
|
1139
|
+
text,
|
|
1140
|
+
options,
|
|
1141
|
+
),
|
|
1142
|
+
);
|
|
976
1143
|
});
|
|
977
1144
|
}
|
|
978
1145
|
|
|
979
|
-
return Promise.all(tPromiseList)
|
|
980
|
-
.catch(e => e);
|
|
1146
|
+
return Promise.all(tPromiseList).catch(e => e);
|
|
981
1147
|
}
|
|
982
1148
|
|
|
983
1149
|
function saveFile(fileID, fileName, callback) {
|
|
@@ -999,7 +1165,7 @@ function saveFile(fileID, fileName, callback) {
|
|
|
999
1165
|
callback({
|
|
1000
1166
|
info: `media file has been saved to "${adapter.config.saveFilesTo}": ${fileLocation}`,
|
|
1001
1167
|
location: adapter.config.saveFilesTo,
|
|
1002
|
-
path: fileLocation
|
|
1168
|
+
path: fileLocation,
|
|
1003
1169
|
});
|
|
1004
1170
|
} catch (err) {
|
|
1005
1171
|
return callback({ error: `Error: ${err}` });
|
|
@@ -1008,14 +1174,13 @@ function saveFile(fileID, fileName, callback) {
|
|
|
1008
1174
|
try {
|
|
1009
1175
|
const fileLocation = path.join(tmpDirName, fileName); // TODO: check new urn format https://github.com/ioBroker/ioBroker.js-controller/issues/2710
|
|
1010
1176
|
|
|
1011
|
-
adapter.writeFileAsync(adapter.namespace, fileName, Buffer.concat(buf))
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
path: fileLocation,
|
|
1017
|
-
});
|
|
1177
|
+
adapter.writeFileAsync(adapter.namespace, fileName, Buffer.concat(buf)).then(() => {
|
|
1178
|
+
callback({
|
|
1179
|
+
info: `media file has been saved to "${adapter.config.saveFilesTo}": ${fileLocation}`,
|
|
1180
|
+
location: adapter.config.saveFilesTo,
|
|
1181
|
+
path: fileLocation,
|
|
1018
1182
|
});
|
|
1183
|
+
});
|
|
1019
1184
|
} catch (err) {
|
|
1020
1185
|
return callback({ error: `Error: ${err}` });
|
|
1021
1186
|
}
|
|
@@ -1054,7 +1219,7 @@ function getMessage(msg) {
|
|
|
1054
1219
|
0: 'low',
|
|
1055
1220
|
1: 'med',
|
|
1056
1221
|
2: 'high',
|
|
1057
|
-
3: 'highdef'
|
|
1222
|
+
3: 'highdef',
|
|
1058
1223
|
};
|
|
1059
1224
|
|
|
1060
1225
|
let saveOnlyQuality = adapter.config.saveFilesQuality;
|
|
@@ -1080,7 +1245,7 @@ function getMessage(msg) {
|
|
|
1080
1245
|
const id = Object.keys(mediaGroupExport).length;
|
|
1081
1246
|
mediaGroupExport[msg.media_group_id] = {
|
|
1082
1247
|
id,
|
|
1083
|
-
count: 0
|
|
1248
|
+
count: 0,
|
|
1084
1249
|
};
|
|
1085
1250
|
} else {
|
|
1086
1251
|
mediaGroupExport[msg.media_group_id].count++;
|
|
@@ -1176,7 +1341,9 @@ async function processMessage(obj) {
|
|
|
1176
1341
|
// filter out the double messages
|
|
1177
1342
|
const json = JSON.stringify(obj);
|
|
1178
1343
|
if (lastMessageTime && lastMessageText === JSON.stringify(obj) && Date.now() - lastMessageTime < 1200) {
|
|
1179
|
-
return adapter.log.debug(
|
|
1344
|
+
return adapter.log.debug(
|
|
1345
|
+
`Filter out double message [first was for ${Date.now() - lastMessageTime}ms]: ${json}`,
|
|
1346
|
+
);
|
|
1180
1347
|
}
|
|
1181
1348
|
|
|
1182
1349
|
lastMessageTime = Date.now();
|
|
@@ -1203,13 +1370,18 @@ async function processMessage(obj) {
|
|
|
1203
1370
|
case 'ask':
|
|
1204
1371
|
if (obj.message) {
|
|
1205
1372
|
const question = {
|
|
1206
|
-
cb:
|
|
1373
|
+
cb: obj.callback,
|
|
1207
1374
|
from: obj.from,
|
|
1208
|
-
ts:
|
|
1375
|
+
ts: Date.now(),
|
|
1209
1376
|
};
|
|
1210
1377
|
|
|
1211
1378
|
if (typeof obj.message === 'object') {
|
|
1212
|
-
const messages = await sendMessage(
|
|
1379
|
+
const messages = await sendMessage(
|
|
1380
|
+
obj.message.text,
|
|
1381
|
+
obj.message.user,
|
|
1382
|
+
obj.message.chatId,
|
|
1383
|
+
obj.message,
|
|
1384
|
+
);
|
|
1213
1385
|
const msgIds = messages.length > 0 ? messages.map(JSON.parse)[0] : {};
|
|
1214
1386
|
|
|
1215
1387
|
question.chatId = obj.message.chatId;
|
|
@@ -1221,23 +1393,32 @@ async function processMessage(obj) {
|
|
|
1221
1393
|
|
|
1222
1394
|
if (obj.callback) {
|
|
1223
1395
|
adapter._questions.push(question);
|
|
1224
|
-
adapter.log.debug(
|
|
1396
|
+
adapter.log.debug(
|
|
1397
|
+
`added question: ${JSON.stringify(question)} - answer timeout: ${adapter.config.answerTimeoutSec}`,
|
|
1398
|
+
);
|
|
1225
1399
|
|
|
1226
|
-
question.timeout = setTimeout(
|
|
1227
|
-
q
|
|
1228
|
-
|
|
1400
|
+
question.timeout = setTimeout(
|
|
1401
|
+
q => {
|
|
1402
|
+
q.timeout = null;
|
|
1403
|
+
adapter.sendTo(q.from, 'ask', '__timeout__', q.callback);
|
|
1229
1404
|
|
|
1230
|
-
|
|
1405
|
+
adapter.log.info(`question timeout for: ${JSON.stringify(q)}`);
|
|
1231
1406
|
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1407
|
+
// Remove keyboard
|
|
1408
|
+
if (bot && q?.chatId && q?.msgId) {
|
|
1409
|
+
bot.editMessageReplyMarkup(
|
|
1410
|
+
{ inline_keyboard: [] },
|
|
1411
|
+
{ chat_id: q.chatId, message_id: q.msgId },
|
|
1412
|
+
);
|
|
1413
|
+
adapter.log.debug(`removed inline_keyboard for msg ${q.msgId}: ${JSON.stringify(q)}`);
|
|
1414
|
+
}
|
|
1237
1415
|
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1416
|
+
const pos = adapter._questions.indexOf(q);
|
|
1417
|
+
pos !== -1 && adapter._questions.splice(pos);
|
|
1418
|
+
},
|
|
1419
|
+
adapter.config.answerTimeoutSec + 1000,
|
|
1420
|
+
question,
|
|
1421
|
+
);
|
|
1241
1422
|
}
|
|
1242
1423
|
}
|
|
1243
1424
|
break;
|
|
@@ -1260,7 +1441,9 @@ async function processMessage(obj) {
|
|
|
1260
1441
|
if (!call.users && !call.user) {
|
|
1261
1442
|
call.users = Object.keys(users)
|
|
1262
1443
|
.filter(id => users[id] && users[id].userName)
|
|
1263
|
-
.map(id =>
|
|
1444
|
+
.map(id =>
|
|
1445
|
+
users[id].userName.startsWith('@') ? users[id].userName : `@${users[id].userName}`,
|
|
1446
|
+
);
|
|
1264
1447
|
}
|
|
1265
1448
|
if (!(call.users instanceof Array)) {
|
|
1266
1449
|
call.users = [call.users];
|
|
@@ -1276,7 +1459,9 @@ async function processMessage(obj) {
|
|
|
1276
1459
|
|
|
1277
1460
|
//
|
|
1278
1461
|
if (!call.users || !call.users.length) {
|
|
1279
|
-
adapter.log.error(
|
|
1462
|
+
adapter.log.error(
|
|
1463
|
+
`Cannot make a call, because no users stored in ${adapter.namespace}.communicate.users`,
|
|
1464
|
+
);
|
|
1280
1465
|
} else {
|
|
1281
1466
|
callUsers(call.users, call.message, call.lang, call.file, call.repeats);
|
|
1282
1467
|
}
|
|
@@ -1309,13 +1494,18 @@ function callUsers(users, text, lang, file, repeats, cb) {
|
|
|
1309
1494
|
url += params.join('&');
|
|
1310
1495
|
adapter.log.debug(`CALL: ${url}`);
|
|
1311
1496
|
|
|
1312
|
-
axios
|
|
1497
|
+
axios
|
|
1498
|
+
.get(url)
|
|
1313
1499
|
.then(response => {
|
|
1314
1500
|
const body = response && response.data;
|
|
1315
1501
|
if (!body || !response || response.status !== 200) {
|
|
1316
|
-
adapter.log.error(
|
|
1502
|
+
adapter.log.error(
|
|
1503
|
+
`Cannot make a call to ${user}: ${body || (response && response.status) || 'Unknown error'}`,
|
|
1504
|
+
);
|
|
1317
1505
|
} else {
|
|
1318
|
-
adapter.log.debug(
|
|
1506
|
+
adapter.log.debug(
|
|
1507
|
+
`Call to ${user} was made: ${body.substring(body.indexOf('<p>')).replace(/<p>/g, ' ')}`,
|
|
1508
|
+
);
|
|
1319
1509
|
}
|
|
1320
1510
|
setImmediate(callUsers, users, text, lang, file, repeats, cb);
|
|
1321
1511
|
})
|
|
@@ -1337,7 +1527,7 @@ function storeUser(id, firstName, userName) {
|
|
|
1337
1527
|
}
|
|
1338
1528
|
});
|
|
1339
1529
|
|
|
1340
|
-
users[id] = {firstName, userName, sysMessages: false};
|
|
1530
|
+
users[id] = { firstName, userName, sysMessages: false };
|
|
1341
1531
|
|
|
1342
1532
|
if (adapter.config.rememberUsers) {
|
|
1343
1533
|
adapter.setState('communicate.users', JSON.stringify(users), true);
|
|
@@ -1353,9 +1543,13 @@ function getListOfCommands() {
|
|
|
1353
1543
|
if (!commands[id].readOnly) {
|
|
1354
1544
|
if (commands[id].type === 'boolean') {
|
|
1355
1545
|
if (commands[id].writeOnly) {
|
|
1356
|
-
lines.push(
|
|
1546
|
+
lines.push(
|
|
1547
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}|${commands[id].offCommand || _('OFF-Command')}`,
|
|
1548
|
+
);
|
|
1357
1549
|
} else {
|
|
1358
|
-
lines.push(
|
|
1550
|
+
lines.push(
|
|
1551
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}|${commands[id].offCommand || _('OFF-Command')}|?`,
|
|
1552
|
+
);
|
|
1359
1553
|
}
|
|
1360
1554
|
} else {
|
|
1361
1555
|
if (commands[id].writeOnly) {
|
|
@@ -1386,19 +1580,17 @@ function getCommandsKeyboard(chatId) {
|
|
|
1386
1580
|
keyboard.push([`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`]);
|
|
1387
1581
|
!commands[id].writeOnly && keyboard.push([`${commands[id].alias} ?`]);
|
|
1388
1582
|
} else {
|
|
1389
|
-
commands[id].writeOnly
|
|
1390
|
-
keyboard.push([
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
`${commands[id].alias} ?`
|
|
1396
|
-
]);
|
|
1583
|
+
commands[id].writeOnly
|
|
1584
|
+
? keyboard.push([`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`])
|
|
1585
|
+
: keyboard.push([
|
|
1586
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`,
|
|
1587
|
+
`${commands[id].alias} ?`,
|
|
1588
|
+
]);
|
|
1397
1589
|
}
|
|
1398
1590
|
} else {
|
|
1399
1591
|
if (commands[id].buttons === 1) {
|
|
1400
|
-
keyboard.push([`${commands[id].alias} ${commands[id].onCommand
|
|
1401
|
-
keyboard.push([`${commands[id].alias} ${commands[id].offCommand || _('OFF-Command')}
|
|
1592
|
+
keyboard.push([`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`]);
|
|
1593
|
+
keyboard.push([`${commands[id].alias} ${commands[id].offCommand || _('OFF-Command')}`]);
|
|
1402
1594
|
!commands[id].writeOnly && keyboard.push([`${commands[id].alias} ?`]);
|
|
1403
1595
|
} else if (commands[id].buttons === 2) {
|
|
1404
1596
|
keyboard.push([
|
|
@@ -1407,16 +1599,16 @@ function getCommandsKeyboard(chatId) {
|
|
|
1407
1599
|
]);
|
|
1408
1600
|
!commands[id].writeOnly && keyboard.push([`${commands[id].alias} ?`]);
|
|
1409
1601
|
} else {
|
|
1410
|
-
commands[id].writeOnly
|
|
1411
|
-
keyboard.push([
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
keyboard.push([
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1602
|
+
commands[id].writeOnly
|
|
1603
|
+
? keyboard.push([
|
|
1604
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`,
|
|
1605
|
+
`${commands[id].alias} ${commands[id].offCommand || _('OFF-Command')}`,
|
|
1606
|
+
])
|
|
1607
|
+
: keyboard.push([
|
|
1608
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`,
|
|
1609
|
+
`${commands[id].alias} ${commands[id].offCommand || _('OFF-Command')}`,
|
|
1610
|
+
`${commands[id].alias} ?`,
|
|
1611
|
+
]);
|
|
1420
1612
|
}
|
|
1421
1613
|
}
|
|
1422
1614
|
} else if (commands[id].states) {
|
|
@@ -1444,7 +1636,9 @@ function getCommandsKeyboard(chatId) {
|
|
|
1444
1636
|
!commands[id].writeOnly && s.push(`${commands[id].alias} ?`);
|
|
1445
1637
|
keyboard.push(s);
|
|
1446
1638
|
} else {
|
|
1447
|
-
adapter.log.warn(
|
|
1639
|
+
adapter.log.warn(
|
|
1640
|
+
`Unsupported state type for keyboard: ${commands[id].type}. Only numbers and booleans are supported`,
|
|
1641
|
+
);
|
|
1448
1642
|
}
|
|
1449
1643
|
} else {
|
|
1450
1644
|
keyboard.push([`${commands[id].alias} ?`]);
|
|
@@ -1455,14 +1649,14 @@ function getCommandsKeyboard(chatId) {
|
|
|
1455
1649
|
reply_markup: {
|
|
1456
1650
|
keyboard,
|
|
1457
1651
|
resize_keyboard: true,
|
|
1458
|
-
one_time_keyboard: true
|
|
1652
|
+
one_time_keyboard: true,
|
|
1459
1653
|
},
|
|
1460
|
-
chatId
|
|
1654
|
+
chatId,
|
|
1461
1655
|
})
|
|
1462
1656
|
.then(() => {
|
|
1463
1657
|
adapter.log.debug('Message sent');
|
|
1464
1658
|
})
|
|
1465
|
-
.catch(
|
|
1659
|
+
.catch(error => {
|
|
1466
1660
|
adapter.log.error(`Send message error: ${error}`);
|
|
1467
1661
|
});
|
|
1468
1662
|
}
|
|
@@ -1472,8 +1666,15 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1472
1666
|
const now = Date.now();
|
|
1473
1667
|
const chatId = msg.chat && msg.chat.id;
|
|
1474
1668
|
|
|
1475
|
-
let question =
|
|
1476
|
-
|
|
1669
|
+
let question =
|
|
1670
|
+
chatId &&
|
|
1671
|
+
adapter._questions.find(
|
|
1672
|
+
q => q.chatId === chatId && q.user === msg.from.id && now - q.ts < adapter.config.answerTimeoutSec,
|
|
1673
|
+
);
|
|
1674
|
+
question =
|
|
1675
|
+
question ||
|
|
1676
|
+
(chatId &&
|
|
1677
|
+
adapter._questions.find(q => q.chatId === chatId && now - q.ts < adapter.config.answerTimeoutSec));
|
|
1477
1678
|
question = question || adapter._questions.find(q => now - q.ts < adapter.config.answerTimeoutSec);
|
|
1478
1679
|
|
|
1479
1680
|
// user have 60 seconds for answer
|
|
@@ -1485,7 +1686,10 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1485
1686
|
|
|
1486
1687
|
// Remove keyboard
|
|
1487
1688
|
if (bot && question?.chatId && question?.msgId) {
|
|
1488
|
-
bot.editMessageReplyMarkup(
|
|
1689
|
+
bot.editMessageReplyMarkup(
|
|
1690
|
+
{ inline_keyboard: [] },
|
|
1691
|
+
{ chat_id: question.chatId, message_id: question.msgId },
|
|
1692
|
+
);
|
|
1489
1693
|
adapter.log.debug(`removed inline_keyboard for msg ${question.msgId}: ${JSON.stringify(question)}`);
|
|
1490
1694
|
}
|
|
1491
1695
|
}
|
|
@@ -1500,23 +1704,22 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1500
1704
|
function garbageCollector() {
|
|
1501
1705
|
const now = Date.now() - 5 * 60000; // last 5 minutes
|
|
1502
1706
|
|
|
1503
|
-
Object.keys(protection)
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
break;
|
|
1510
|
-
}
|
|
1511
|
-
}
|
|
1512
|
-
// remove all old entries
|
|
1513
|
-
if (a < protection[user].length && a) {
|
|
1514
|
-
protection[user].splice(0, a);
|
|
1515
|
-
}
|
|
1516
|
-
if (!protection[user].length) {
|
|
1517
|
-
delete protection[user];
|
|
1707
|
+
Object.keys(protection).forEach(user => {
|
|
1708
|
+
let a;
|
|
1709
|
+
for (a = 0; a < protection[user].length; a++) {
|
|
1710
|
+
// find first entry newer than 5 minutes
|
|
1711
|
+
if (protection[user][a] > now) {
|
|
1712
|
+
break;
|
|
1518
1713
|
}
|
|
1519
|
-
}
|
|
1714
|
+
}
|
|
1715
|
+
// remove all old entries
|
|
1716
|
+
if (a < protection[user].length && a) {
|
|
1717
|
+
protection[user].splice(0, a);
|
|
1718
|
+
}
|
|
1719
|
+
if (!protection[user].length) {
|
|
1720
|
+
delete protection[user];
|
|
1721
|
+
}
|
|
1722
|
+
});
|
|
1520
1723
|
|
|
1521
1724
|
if (!Object.keys(protection).length) {
|
|
1522
1725
|
gcInterval && clearInterval(gcInterval);
|
|
@@ -1530,7 +1733,11 @@ function processTelegramText(msg) {
|
|
|
1530
1733
|
// see https://core.telegram.org/bots/api#message
|
|
1531
1734
|
adapter.log.debug(JSON.stringify(msg));
|
|
1532
1735
|
|
|
1533
|
-
const user = !adapter.config.useUsername
|
|
1736
|
+
const user = !adapter.config.useUsername
|
|
1737
|
+
? msg.from.first_name
|
|
1738
|
+
: !msg.from.username
|
|
1739
|
+
? msg.from.first_name
|
|
1740
|
+
: msg.from.username;
|
|
1534
1741
|
|
|
1535
1742
|
const now = Date.now();
|
|
1536
1743
|
let pollingInterval = 0;
|
|
@@ -1540,13 +1747,15 @@ function processTelegramText(msg) {
|
|
|
1540
1747
|
|
|
1541
1748
|
// ignore all messages older than 30 seconds + polling interval
|
|
1542
1749
|
if (now - msg.date * 1000 > pollingInterval + 30000) {
|
|
1543
|
-
adapter.log.warn(
|
|
1544
|
-
|
|
1750
|
+
adapter.log.warn(
|
|
1751
|
+
`Message from ${msg.from.name} ignored, because too old: (${pollingInterval + 30000}) ${msg.text}`,
|
|
1752
|
+
);
|
|
1753
|
+
return bot
|
|
1754
|
+
.sendMessage(msg.from.id, _('Message ignored: ', systemLang) + msg.text)
|
|
1545
1755
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1546
|
-
|
|
1547
1756
|
}
|
|
1548
1757
|
|
|
1549
|
-
msg.text = (msg.text ||'').trim();
|
|
1758
|
+
msg.text = (msg.text || '').trim();
|
|
1550
1759
|
|
|
1551
1760
|
// sometimes telegram sends messages like "message@user_name"
|
|
1552
1761
|
const pos = msg.text.lastIndexOf('@');
|
|
@@ -1555,7 +1764,8 @@ function processTelegramText(msg) {
|
|
|
1555
1764
|
}
|
|
1556
1765
|
|
|
1557
1766
|
if (msg.text === '/password' && !adapter.config.doNotAcceptNewUser) {
|
|
1558
|
-
return bot
|
|
1767
|
+
return bot
|
|
1768
|
+
.sendMessage(msg.from.id, _('Please enter password in form "/password phrase"', systemLang))
|
|
1559
1769
|
.catch(error => adapter.log.error(`send Message Error:${error}`));
|
|
1560
1770
|
}
|
|
1561
1771
|
|
|
@@ -1568,7 +1778,11 @@ function processTelegramText(msg) {
|
|
|
1568
1778
|
garbageCollector();
|
|
1569
1779
|
|
|
1570
1780
|
if (protection[user] && protection[user].length >= 5) {
|
|
1571
|
-
return bot
|
|
1781
|
+
return bot
|
|
1782
|
+
.sendMessage(
|
|
1783
|
+
msg.from.id,
|
|
1784
|
+
`${_('Too many attempts. Blocked for', systemLang)} ${Math.round((now - protection[user][protection[user].length - 1]) / 1000)} ${_('seconds', systemLang)}`,
|
|
1785
|
+
)
|
|
1572
1786
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1573
1787
|
}
|
|
1574
1788
|
|
|
@@ -1582,22 +1796,23 @@ function processTelegramText(msg) {
|
|
|
1582
1796
|
adapter.log.warn(`User ${msg.from.first_name} hast not set an username in the Telegram App!!`);
|
|
1583
1797
|
}
|
|
1584
1798
|
|
|
1585
|
-
return bot
|
|
1799
|
+
return bot
|
|
1800
|
+
.sendMessage(msg.from.id, _('Welcome ', systemLang) + user)
|
|
1586
1801
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
|
|
1802
|
+
}
|
|
1803
|
+
protection[user] = protection[user] || [];
|
|
1804
|
+
protection[user].push(Date.now());
|
|
1590
1805
|
|
|
1591
|
-
|
|
1806
|
+
gcInterval = gcInterval || setInterval(() => garbageCollector(), 60000);
|
|
1592
1807
|
|
|
1593
|
-
|
|
1808
|
+
adapter.log.warn(`Got invalid password from ${user}: ${m[1]}`);
|
|
1594
1809
|
|
|
1595
|
-
|
|
1596
|
-
|
|
1810
|
+
bot.sendMessage(msg.from.id, _('Invalid password', systemLang)).catch(error =>
|
|
1811
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1812
|
+
);
|
|
1597
1813
|
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
}
|
|
1814
|
+
if (users[msg.from.id]) {
|
|
1815
|
+
delete users[msg.from.id];
|
|
1601
1816
|
}
|
|
1602
1817
|
}
|
|
1603
1818
|
}
|
|
@@ -1606,12 +1821,22 @@ function processTelegramText(msg) {
|
|
|
1606
1821
|
|
|
1607
1822
|
// If a user is not in the trusted list
|
|
1608
1823
|
if ((adapter.config.password || adapter.config.doNotAcceptNewUsers) && !users[msg.from.id]) {
|
|
1609
|
-
return bot
|
|
1824
|
+
return bot
|
|
1825
|
+
.sendMessage(
|
|
1826
|
+
msg.from.id,
|
|
1827
|
+
_(
|
|
1828
|
+
adapter.config.doNotAcceptNewUsers
|
|
1829
|
+
? 'User is not in the list'
|
|
1830
|
+
: 'Please enter password in form "/password phrase"',
|
|
1831
|
+
systemLang,
|
|
1832
|
+
),
|
|
1833
|
+
)
|
|
1610
1834
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1611
1835
|
}
|
|
1612
1836
|
|
|
1613
1837
|
if (msg.text === '/help') {
|
|
1614
|
-
return bot
|
|
1838
|
+
return bot
|
|
1839
|
+
.sendMessage(msg.from.id, getListOfCommands())
|
|
1615
1840
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1616
1841
|
}
|
|
1617
1842
|
|
|
@@ -1641,34 +1866,43 @@ function processTelegramText(msg) {
|
|
|
1641
1866
|
found = true;
|
|
1642
1867
|
if (sValue === '?') {
|
|
1643
1868
|
adapter.getForeignState(id, (err, state) =>
|
|
1644
|
-
bot
|
|
1645
|
-
.
|
|
1869
|
+
bot
|
|
1870
|
+
.sendMessage(msg.chat.id, getStatus(id, state))
|
|
1871
|
+
.catch(error => adapter.log.error(`send Message Error: ${error}`)),
|
|
1872
|
+
);
|
|
1646
1873
|
} else {
|
|
1647
1874
|
let value;
|
|
1648
1875
|
if (commands[id].states) {
|
|
1649
|
-
const sState = Object.keys(commands[id].states)
|
|
1650
|
-
|
|
1876
|
+
const sState = Object.keys(commands[id].states).find(
|
|
1877
|
+
val => commands[id].states[val] === sValue,
|
|
1878
|
+
);
|
|
1651
1879
|
if (sState !== null && sState !== undefined) {
|
|
1652
1880
|
sValue = sState;
|
|
1653
1881
|
}
|
|
1654
1882
|
}
|
|
1655
1883
|
|
|
1656
1884
|
if (commands[id].type === 'boolean') {
|
|
1657
|
-
value = commands[id].onCommand
|
|
1885
|
+
value = commands[id].onCommand
|
|
1886
|
+
? sValue === commands[id].onCommand
|
|
1887
|
+
: sValue === _('ON-Command') ||
|
|
1888
|
+
sValue === 'true' ||
|
|
1889
|
+
sValue.toLowerCase() === 'on' ||
|
|
1890
|
+
sValue === '1';
|
|
1658
1891
|
} else if (commands[id].type === 'number') {
|
|
1659
1892
|
sValue = sValue.replace('%', '').trim();
|
|
1660
1893
|
value = parseFloat(sValue);
|
|
1661
1894
|
if (sValue !== value.toString()) {
|
|
1662
|
-
bot.sendMessage(msg.chat.id, _('Invalid number %s', sValue))
|
|
1663
|
-
|
|
1895
|
+
bot.sendMessage(msg.chat.id, _('Invalid number %s', sValue)).catch(error =>
|
|
1896
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1897
|
+
);
|
|
1664
1898
|
continue;
|
|
1665
1899
|
}
|
|
1666
1900
|
} else {
|
|
1667
1901
|
value = sValue;
|
|
1668
1902
|
}
|
|
1669
1903
|
|
|
1670
|
-
adapter
|
|
1671
|
-
bot.sendMessage(msg.chat.id, _('Done')))
|
|
1904
|
+
adapter
|
|
1905
|
+
.setForeignState(id, value, false, () => bot.sendMessage(msg.chat.id, _('Done')))
|
|
1672
1906
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1673
1907
|
}
|
|
1674
1908
|
}
|
|
@@ -1707,24 +1941,28 @@ function processTelegramText(msg) {
|
|
|
1707
1941
|
}
|
|
1708
1942
|
if (msg) {
|
|
1709
1943
|
if (err) {
|
|
1710
|
-
bot.sendMessage(msg.from.id, err)
|
|
1711
|
-
|
|
1944
|
+
bot.sendMessage(msg.from.id, err).catch(error =>
|
|
1945
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1946
|
+
);
|
|
1712
1947
|
}
|
|
1713
1948
|
if (state) {
|
|
1714
1949
|
adapter.setForeignState(id1, val1, false, err => {
|
|
1715
1950
|
if (msg) {
|
|
1716
1951
|
if (err) {
|
|
1717
|
-
bot.sendMessage(msg.from.id, err)
|
|
1718
|
-
|
|
1952
|
+
bot.sendMessage(msg.from.id, err).catch(error =>
|
|
1953
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1954
|
+
);
|
|
1719
1955
|
} else {
|
|
1720
|
-
bot.sendMessage(msg.from.id, _('Done', systemLang))
|
|
1721
|
-
|
|
1956
|
+
bot.sendMessage(msg.from.id, _('Done', systemLang)).catch(error =>
|
|
1957
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1958
|
+
);
|
|
1722
1959
|
}
|
|
1723
1960
|
}
|
|
1724
1961
|
});
|
|
1725
1962
|
} else {
|
|
1726
|
-
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id1))
|
|
1727
|
-
|
|
1963
|
+
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id1)).catch(
|
|
1964
|
+
error => adapter.log.error(`send Message Error: ${error}`),
|
|
1965
|
+
);
|
|
1728
1966
|
}
|
|
1729
1967
|
}
|
|
1730
1968
|
});
|
|
@@ -1751,15 +1989,18 @@ function processTelegramText(msg) {
|
|
|
1751
1989
|
}
|
|
1752
1990
|
if (msg) {
|
|
1753
1991
|
if (err) {
|
|
1754
|
-
bot.sendMessage(msg.from.id, err)
|
|
1755
|
-
|
|
1992
|
+
bot.sendMessage(msg.from.id, err).catch(error =>
|
|
1993
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1994
|
+
);
|
|
1756
1995
|
}
|
|
1757
1996
|
if (state) {
|
|
1758
|
-
bot.sendMessage(msg.from.id, state.val.toString())
|
|
1759
|
-
|
|
1997
|
+
bot.sendMessage(msg.from.id, state.val.toString()).catch(error =>
|
|
1998
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1999
|
+
);
|
|
1760
2000
|
} else {
|
|
1761
|
-
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id2))
|
|
1762
|
-
|
|
2001
|
+
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id2)).catch(
|
|
2002
|
+
error => adapter.log.error(`send Message Error: ${error}`),
|
|
2003
|
+
);
|
|
1763
2004
|
}
|
|
1764
2005
|
}
|
|
1765
2006
|
});
|
|
@@ -1771,38 +2012,50 @@ function processTelegramText(msg) {
|
|
|
1771
2012
|
|
|
1772
2013
|
// Send to text2command
|
|
1773
2014
|
if (adapter.config.text2command) {
|
|
1774
|
-
adapter.sendTo(
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
2015
|
+
adapter.sendTo(
|
|
2016
|
+
adapter.config.text2command,
|
|
2017
|
+
'send',
|
|
2018
|
+
{
|
|
2019
|
+
text: msg.text.replace(/\//g, '#').replace(/_/g, ' '),
|
|
2020
|
+
id: msg.chat.id,
|
|
2021
|
+
user,
|
|
2022
|
+
},
|
|
2023
|
+
response => {
|
|
2024
|
+
if (response && response.response) {
|
|
2025
|
+
let text = response.response;
|
|
2026
|
+
let options;
|
|
2027
|
+
if (text && typeof text === 'string') {
|
|
2028
|
+
if (text.startsWith('<MarkdownV2>') && text.endsWith('</MarkdownV2>')) {
|
|
2029
|
+
options = { parse_mode: 'MarkdownV2' };
|
|
2030
|
+
text = text.substring(12, text.length - 13);
|
|
2031
|
+
} else if (text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
|
|
2032
|
+
options = { parse_mode: 'HTML' };
|
|
2033
|
+
text = text.substring(6, text.length - 7);
|
|
2034
|
+
} else if (text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
|
|
2035
|
+
options = { parse_mode: 'Markdown' };
|
|
2036
|
+
text = text.substring(10, text.length - 11);
|
|
2037
|
+
}
|
|
1792
2038
|
}
|
|
1793
|
-
}
|
|
1794
2039
|
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
2040
|
+
adapter.log.debug(`Send response: ${text}`);
|
|
2041
|
+
bot.sendMessage(response.id, text, options).catch(error =>
|
|
2042
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
2043
|
+
);
|
|
2044
|
+
}
|
|
2045
|
+
},
|
|
2046
|
+
);
|
|
1800
2047
|
}
|
|
1801
2048
|
|
|
1802
2049
|
adapter.setState('communicate.requestChatId', { val: msg.chat.id, ack: true });
|
|
1803
2050
|
adapter.setState('communicate.requestMessageId', { val: msg.message_id, ack: true });
|
|
1804
|
-
adapter.setState('communicate.requestMessageThreadId', {
|
|
1805
|
-
|
|
2051
|
+
adapter.setState('communicate.requestMessageThreadId', {
|
|
2052
|
+
val: msg?.is_topic_message ? msg.message_thread_id : 0,
|
|
2053
|
+
ack: true,
|
|
2054
|
+
});
|
|
2055
|
+
adapter.setState('communicate.requestUserId', {
|
|
2056
|
+
val: msg.from && msg.from.id !== undefined && msg.from.id !== null ? msg.from.id.toString() : '',
|
|
2057
|
+
ack: true,
|
|
2058
|
+
});
|
|
1806
2059
|
adapter.setState('communicate.request', { val: `[${user}]${msg.text}`, ack: true });
|
|
1807
2060
|
}
|
|
1808
2061
|
|
|
@@ -1810,9 +2063,9 @@ function connect() {
|
|
|
1810
2063
|
if (bot) {
|
|
1811
2064
|
if (!adapter.config.server) {
|
|
1812
2065
|
try {
|
|
1813
|
-
if (bot.isPolling())
|
|
2066
|
+
if (bot.isPolling()) {
|
|
1814
2067
|
adapter.log.debug('bot polling OK');
|
|
1815
|
-
else {
|
|
2068
|
+
} else {
|
|
1816
2069
|
adapter.log.debug('bot restarting...');
|
|
1817
2070
|
bot.stopPolling().then(
|
|
1818
2071
|
() => {
|
|
@@ -1821,7 +2074,7 @@ function connect() {
|
|
|
1821
2074
|
},
|
|
1822
2075
|
error => {
|
|
1823
2076
|
adapter.log.error(`Error stop polling: ${error}`);
|
|
1824
|
-
}
|
|
2077
|
+
},
|
|
1825
2078
|
);
|
|
1826
2079
|
}
|
|
1827
2080
|
} catch {
|
|
@@ -1841,7 +2094,7 @@ function connect() {
|
|
|
1841
2094
|
const serverOptions = {
|
|
1842
2095
|
polling: false,
|
|
1843
2096
|
filepath: true,
|
|
1844
|
-
baseApiUrl: adapter.config.baseApiUrl
|
|
2097
|
+
baseApiUrl: adapter.config.baseApiUrl,
|
|
1845
2098
|
};
|
|
1846
2099
|
bot = new TelegramBot(adapter.config.token, serverOptions);
|
|
1847
2100
|
if (adapter.config.url[adapter.config.url.length - 1] === '/') {
|
|
@@ -1852,12 +2105,14 @@ function connect() {
|
|
|
1852
2105
|
// Setup polling way
|
|
1853
2106
|
const pollingOptions = {
|
|
1854
2107
|
polling: {
|
|
1855
|
-
interval: parseInt(adapter.config.pollingInterval, 10) || 300
|
|
2108
|
+
interval: parseInt(adapter.config.pollingInterval, 10) || 300,
|
|
1856
2109
|
},
|
|
1857
2110
|
filepath: true,
|
|
1858
|
-
baseApiUrl: adapter.config.baseApiUrl
|
|
2111
|
+
baseApiUrl: adapter.config.baseApiUrl,
|
|
1859
2112
|
};
|
|
1860
|
-
adapter.log.debug(
|
|
2113
|
+
adapter.log.debug(
|
|
2114
|
+
`Start polling with: ${pollingOptions.polling.interval}(${typeof pollingOptions.polling.interval}) ms interval`,
|
|
2115
|
+
);
|
|
1861
2116
|
bot = new TelegramBot(adapter.config.token, pollingOptions);
|
|
1862
2117
|
bot.setWebHook('').catch(error => {
|
|
1863
2118
|
adapter.log.error(`setWebHook Error:${error}`);
|
|
@@ -1865,22 +2120,28 @@ function connect() {
|
|
|
1865
2120
|
}
|
|
1866
2121
|
|
|
1867
2122
|
// Check connection
|
|
1868
|
-
bot.getMe()
|
|
1869
|
-
|
|
1870
|
-
|
|
2123
|
+
bot.getMe()
|
|
2124
|
+
.then(data => {
|
|
2125
|
+
adapter.log.debug(`getMe: ${JSON.stringify(data)}`);
|
|
2126
|
+
connectionState(true);
|
|
1871
2127
|
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
2128
|
+
if (adapter.config.restarted !== '') {
|
|
2129
|
+
// default text
|
|
2130
|
+
if (
|
|
2131
|
+
adapter.config.restarted === '_' ||
|
|
2132
|
+
adapter.config.restarted === null ||
|
|
2133
|
+
adapter.config.restarted === undefined
|
|
2134
|
+
) {
|
|
2135
|
+
sendSystemMessage(_('Started!'));
|
|
2136
|
+
} else {
|
|
2137
|
+
sendSystemMessage(adapter.config.restarted);
|
|
2138
|
+
}
|
|
1878
2139
|
}
|
|
1879
|
-
}
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
2140
|
+
})
|
|
2141
|
+
.catch(error => {
|
|
2142
|
+
adapter.log.error(`getMe Error:${error}`);
|
|
2143
|
+
connectionState(false);
|
|
2144
|
+
});
|
|
1884
2145
|
|
|
1885
2146
|
// Matches /echo [whatever]
|
|
1886
2147
|
bot.onText(/(.+)/, processTelegramText);
|
|
@@ -1903,15 +2164,38 @@ function connect() {
|
|
|
1903
2164
|
callbackQueryId[callbackQuery.from.id] = { id: callbackQuery.id, ts: Date.now() };
|
|
1904
2165
|
|
|
1905
2166
|
if (adapter.config.storeRawRequest) {
|
|
1906
|
-
adapter.setState(
|
|
1907
|
-
|
|
2167
|
+
adapter.setState(
|
|
2168
|
+
'communicate.requestRaw',
|
|
2169
|
+
JSON.stringify(callbackQuery),
|
|
2170
|
+
true,
|
|
2171
|
+
err => err && adapter.log.error(err),
|
|
2172
|
+
);
|
|
1908
2173
|
}
|
|
1909
2174
|
|
|
1910
|
-
adapter.setState(
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
2175
|
+
adapter.setState(
|
|
2176
|
+
'communicate.requestMessageId',
|
|
2177
|
+
callbackQuery.message.message_id,
|
|
2178
|
+
true,
|
|
2179
|
+
err => err && adapter.log.error(err),
|
|
2180
|
+
);
|
|
2181
|
+
adapter.setState(
|
|
2182
|
+
'communicate.requestChatId',
|
|
2183
|
+
callbackQuery.message.chat.id,
|
|
2184
|
+
true,
|
|
2185
|
+
err => err && adapter.log.error(err),
|
|
2186
|
+
);
|
|
2187
|
+
adapter.setState(
|
|
2188
|
+
'communicate.request',
|
|
2189
|
+
`[${
|
|
2190
|
+
!adapter.config.useUsername
|
|
2191
|
+
? callbackQuery.from.first_name
|
|
2192
|
+
: !callbackQuery.from.username
|
|
2193
|
+
? callbackQuery.from.first_name
|
|
2194
|
+
: callbackQuery.from.username
|
|
2195
|
+
}]${callbackQuery.data}`,
|
|
2196
|
+
true,
|
|
2197
|
+
err => err && adapter.log.error(err),
|
|
2198
|
+
);
|
|
1915
2199
|
|
|
1916
2200
|
isAnswerForQuestion(adapter, callbackQuery);
|
|
1917
2201
|
});
|
|
@@ -1934,7 +2218,7 @@ function connect() {
|
|
|
1934
2218
|
},
|
|
1935
2219
|
error => {
|
|
1936
2220
|
adapter.log.error(`Error stop polling: ${error}`);
|
|
1937
|
-
}
|
|
2221
|
+
},
|
|
1938
2222
|
);
|
|
1939
2223
|
});
|
|
1940
2224
|
}
|
|
@@ -1952,9 +2236,17 @@ async function updateUsers() {
|
|
|
1952
2236
|
Object.keys(users).forEach(id => {
|
|
1953
2237
|
if (typeof users[id] !== 'object') {
|
|
1954
2238
|
if (adapter.config.useUsername) {
|
|
1955
|
-
users[id] = {
|
|
2239
|
+
users[id] = {
|
|
2240
|
+
userName: users[id],
|
|
2241
|
+
firstName: users[id],
|
|
2242
|
+
sysMessages: users[id].sysMessages !== false,
|
|
2243
|
+
};
|
|
1956
2244
|
} else {
|
|
1957
|
-
users[id] = {
|
|
2245
|
+
users[id] = {
|
|
2246
|
+
firstName: users[id],
|
|
2247
|
+
userName: '',
|
|
2248
|
+
sysMessages: users[id].sysMessages !== false,
|
|
2249
|
+
};
|
|
1958
2250
|
}
|
|
1959
2251
|
}
|
|
1960
2252
|
});
|
|
@@ -1975,12 +2267,12 @@ async function readAllNames(ids) {
|
|
|
1975
2267
|
try {
|
|
1976
2268
|
const obj = await adapter.getForeignObjectAsync(ids[i]);
|
|
1977
2269
|
if (obj) {
|
|
1978
|
-
commands[ids[i]].alias
|
|
1979
|
-
commands[ids[i]].type
|
|
2270
|
+
commands[ids[i]].alias = getName(obj);
|
|
2271
|
+
commands[ids[i]].type = obj.common && obj.common.type;
|
|
1980
2272
|
commands[ids[i]].states = obj.common && parseStates(obj.common.states || undefined);
|
|
1981
|
-
commands[ids[i]].unit
|
|
1982
|
-
commands[ids[i]].min
|
|
1983
|
-
commands[ids[i]].max
|
|
2273
|
+
commands[ids[i]].unit = obj.common && obj.common.unit;
|
|
2274
|
+
commands[ids[i]].min = obj.common && obj.common.min;
|
|
2275
|
+
commands[ids[i]].max = obj.common && obj.common.max;
|
|
1984
2276
|
// read actual state to detect changes
|
|
1985
2277
|
if (commands[ids[i]].reportChanges) {
|
|
1986
2278
|
const state = await adapter.getForeignStateAsync(ids[i]);
|
|
@@ -2017,7 +2309,10 @@ async function readEnums(name) {
|
|
|
2017
2309
|
name = name || 'rooms';
|
|
2018
2310
|
enums[name] = {};
|
|
2019
2311
|
try {
|
|
2020
|
-
const doc = await adapter.getObjectViewAsync('system', 'enum', {
|
|
2312
|
+
const doc = await adapter.getObjectViewAsync('system', 'enum', {
|
|
2313
|
+
startkey: `enum.${name}.`,
|
|
2314
|
+
endkey: `enum.${name}.\u9999`,
|
|
2315
|
+
});
|
|
2021
2316
|
if (doc && doc.rows) {
|
|
2022
2317
|
for (let i = 0, l = doc.rows.length; i < l; i++) {
|
|
2023
2318
|
if (doc.rows[i].value) {
|
|
@@ -2093,13 +2388,12 @@ async function main() {
|
|
|
2093
2388
|
}
|
|
2094
2389
|
}
|
|
2095
2390
|
|
|
2096
|
-
|
|
2097
2391
|
/**
|
|
2098
2392
|
* Process a `sendNotification` request
|
|
2099
2393
|
*
|
|
2100
|
-
* @param
|
|
2394
|
+
* @param obj
|
|
2101
2395
|
*/
|
|
2102
|
-
async function processNotification(
|
|
2396
|
+
async function processNotification(obj) {
|
|
2103
2397
|
adapter.log.info(`New notification received from ${obj.from}`);
|
|
2104
2398
|
|
|
2105
2399
|
const notificationMessage = buildMessageFromNotification(obj.message);
|
|
@@ -2113,20 +2407,21 @@ async function processNotification( obj) {
|
|
|
2113
2407
|
adapter.sendTo(obj.from, 'sendNotification', { sent: false }, obj.callback);
|
|
2114
2408
|
}
|
|
2115
2409
|
}
|
|
2116
|
-
|
|
2117
2410
|
}
|
|
2118
2411
|
|
|
2119
2412
|
/**
|
|
2120
2413
|
* Build up a mail object from the notification message
|
|
2121
2414
|
*
|
|
2122
|
-
* @param
|
|
2123
|
-
* @returns
|
|
2415
|
+
* @param message
|
|
2416
|
+
* @returns
|
|
2124
2417
|
*/
|
|
2125
2418
|
function buildMessageFromNotification(message) {
|
|
2126
2419
|
const subject = message.category.name;
|
|
2127
2420
|
const { instances } = message.category;
|
|
2128
2421
|
|
|
2129
|
-
const readableInstances = Object.entries(instances).map(
|
|
2422
|
+
const readableInstances = Object.entries(instances).map(
|
|
2423
|
+
([instance, entry]) => `${instance.substring('system.adapter.'.length)}: ${getNewestMessage(entry.messages)}`,
|
|
2424
|
+
);
|
|
2130
2425
|
|
|
2131
2426
|
const text = `${message.category.description}
|
|
2132
2427
|
${message.host}:
|
|
@@ -2139,8 +2434,8 @@ ${readableInstances.join('\n')}
|
|
|
2139
2434
|
/**
|
|
2140
2435
|
* Replace reserved characters in outgoing message
|
|
2141
2436
|
*
|
|
2142
|
-
* @param
|
|
2143
|
-
* @returns
|
|
2437
|
+
* @param text string to sanitize
|
|
2438
|
+
* @returns
|
|
2144
2439
|
*/
|
|
2145
2440
|
function replaceReservedCharacters(text) {
|
|
2146
2441
|
return text.replace(/([.!()-_*[\]~`><&#+=|{}])/g, '\\$1');
|
|
@@ -2149,11 +2444,11 @@ function replaceReservedCharacters(text) {
|
|
|
2149
2444
|
/**
|
|
2150
2445
|
* Extract the newest message out of a notification messages together with the localized date
|
|
2151
2446
|
*
|
|
2152
|
-
* @param
|
|
2153
|
-
* @
|
|
2447
|
+
* @param messages
|
|
2448
|
+
* @returns string
|
|
2154
2449
|
*/
|
|
2155
2450
|
function getNewestMessage(messages) {
|
|
2156
|
-
const newestMessage = messages.sort((a, b) => a.ts < b.ts ? 1 : -1)[0];
|
|
2451
|
+
const newestMessage = messages.sort((a, b) => (a.ts < b.ts ? 1 : -1))[0];
|
|
2157
2452
|
|
|
2158
2453
|
return `${new Date(newestMessage.ts).toLocaleString()} ${newestMessage.message}`;
|
|
2159
2454
|
}
|