iobroker.telegram 3.9.0 → 4.1.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 +9 -8
- package/admin/blockly.js +1 -1
- package/admin/custom/assets/Components-D2wD0zbe.js +1 -0
- package/admin/custom/assets/ConfigCustomTelegramSet__loadShare___mf_0_iobroker_mf_1_adapter_mf_2_react_mf_2_v5__loadShare__-Ci6Xy-GH.js +1 -0
- package/admin/custom/assets/ConfigCustomTelegramSet__loadShare___mf_0_mui_mf_1_icons_mf_2_material__loadShare__-DsvMV1W3.js +1 -0
- package/admin/custom/assets/ConfigCustomTelegramSet__loadShare__react__loadShare__-xs2vjw5Y.js +1 -0
- package/admin/custom/assets/ConfigCustomTelegramSet__mf_v__runtimeInit__mf_v__-J6gnGK7T.js +10 -0
- package/admin/custom/assets/DefaultPropsProvider-DmEC3G80.js +1 -0
- package/admin/custom/assets/TelegramComponent-DCLCOges.js +1 -0
- package/admin/custom/assets/TextField-bqxvaxBf.js +102 -0
- package/admin/custom/assets/ThemeProvider-BRixW8Ci.js +29 -0
- package/admin/custom/assets/_commonjsHelpers-D6-XlEtG.js +1 -0
- package/admin/custom/assets/createSvgIcon-BzWD3ACi.js +7 -0
- package/admin/custom/assets/createTheme-CfKVAAx0.js +16 -0
- package/admin/custom/assets/defaultTheme-DCM5Nrg_.js +1 -0
- package/admin/custom/assets/hostInit-DAQ2zgXM.js +2 -0
- package/admin/custom/assets/index-BY-bILJE.js +1397 -0
- package/admin/custom/assets/index-BYe_R7Ws.js +8 -0
- package/admin/custom/assets/index-BgKfcD1A.js +24 -0
- package/admin/custom/assets/index-BpXXtf3y.js +1 -0
- package/admin/custom/assets/index-CI-xWTov.js +91 -0
- package/admin/custom/assets/index-Co3YA5NW.js +9 -0
- package/admin/custom/assets/index-DFNeivSO.js +1 -0
- package/admin/custom/assets/index-DS28Nms8.js +1 -0
- package/admin/custom/assets/index-Doac_TLQ.js +779 -0
- package/admin/custom/assets/index-_YNjWM7c.js +31 -0
- package/admin/custom/assets/jsx-runtime-DscBPJdh.js +9 -0
- package/admin/custom/assets/preload-helper-C1FmrZbK.js +1 -0
- package/admin/custom/assets/useTheme-DhBqOhQ3.js +1 -0
- package/admin/custom/assets/useThemeWithoutDefault-BKCaHj9c.js +1 -0
- package/admin/custom/assets/virtualExposes-D2gQfgfw.js +2 -0
- package/admin/custom/customComponents.js +2 -5
- package/admin/i18n/de/translations.json +2 -2
- package/admin/i18n/en/translations.json +2 -2
- package/admin/jsonConfig.json +58 -2
- package/admin/jsonCustom.json +14 -0
- package/admin/rules/assets/ActionTelegram-DYJUMFCx.js +5 -0
- package/admin/rules/assets/ActionTelegram__loadShare__react__loadShare__-Ccaej0ya.js +1 -0
- package/admin/rules/assets/ActionTelegram__loadShare__react_mf_2_dom__loadShare__-CgzDrK9W.js +9 -0
- package/admin/rules/assets/ActionTelegram__mf_v__runtimeInit__mf_v__-DxlLd_3A.js +10 -0
- package/admin/rules/assets/_commonjsHelpers-Cpj98o6Y.js +1 -0
- package/admin/rules/assets/bootstrap-DXJ-CBj3.js +1 -0
- package/admin/rules/assets/hostInit-CUxlaMpR.js +2 -0
- package/admin/rules/assets/index-BrA2IcWW.js +9 -0
- package/admin/rules/assets/index-C9A6m5GR.js +1026 -0
- package/admin/rules/assets/index-DQiceR5U.js +1 -0
- package/admin/rules/assets/index-XCGquB1p.js +2 -0
- package/admin/rules/assets/index-hSC67Jtf.js +24 -0
- package/admin/rules/assets/preload-helper-C1FmrZbK.js +1 -0
- package/admin/rules/assets/virtualExposes-D_O17Ipe.js +2 -0
- package/admin/rules/customRuleBlocks.js +2 -5
- package/admin/rules/index.html +19 -0
- package/admin/rules/mf-manifest.json +1 -0
- package/admin/words.js +98 -0
- package/io-package.json +31 -30
- package/main.js +647 -355
- package/package.json +35 -25
- package/admin/custom/customComponents.js.map +0 -1
- package/admin/custom/static/js/_91570.cbc32633.chunk.js +0 -1
- package/admin/custom/static/js/_91571.42c2d423.chunk.js +0 -1
- package/admin/custom/static/js/main.7d811702.js +0 -5
- package/admin/custom/static/js/main.7d811702.js.map +0 -1
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.16d7e6d4.chunk.js +0 -3
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.16d7e6d4.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.db11e0c1.chunk.js +0 -3
- package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23731.db11e0c1.chunk.js.map +0 -1
- package/admin/custom/static/js/node_modules_prop-types_index_js.6da256b6.chunk.js +0 -3
- package/admin/custom/static/js/node_modules_prop-types_index_js.6da256b6.chunk.js.map +0 -1
- package/admin/custom/static/js/node_modules_react_index_js.70ac611b.chunk.js +0 -11
- package/admin/custom/static/js/node_modules_react_index_js.70ac611b.chunk.js.map +0 -1
- package/admin/custom/static/js/src_Components_jsx.3f7d60eb.chunk.js +0 -3
- package/admin/custom/static/js/src_Components_jsx.3f7d60eb.chunk.js.map +0 -1
- package/admin/custom/static/js/src_bootstrap_jsx.a960785f.chunk.js +0 -3
- package/admin/custom/static/js/src_bootstrap_jsx.a960785f.chunk.js.map +0 -1
- package/admin/rules/asset-manifest.json +0 -149
- package/admin/rules/customRuleBlocks.js.map +0 -1
- package/admin/rules/static/js/main.44f1f861.js +0 -5
- package/admin/rules/static/js/main.44f1f861.js.map +0 -1
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-d234ae.c537ddf6.chunk.js +0 -3
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-d234ae.c537ddf6.chunk.js.map +0 -1
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d270.bc9865f0.chunk.js +0 -10
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d270.bc9865f0.chunk.js.map +0 -1
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d271.bbc6ce97.chunk.js +0 -14
- package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_node_modules_react_index_js-_9d271.bbc6ce97.chunk.js.map +0 -1
- package/admin/rules/static/js/node_modules_prop-types_index_js.c5b5d86f.chunk.js +0 -3
- package/admin/rules/static/js/node_modules_prop-types_index_js.c5b5d86f.chunk.js.map +0 -1
- package/admin/rules/static/js/node_modules_react_index_js.6114a944.chunk.js +0 -11
- package/admin/rules/static/js/node_modules_react_index_js.6114a944.chunk.js.map +0 -1
- package/admin/rules/static/js/src_ActionTelegram_jsx.606264b1.chunk.js +0 -7
- package/admin/rules/static/js/src_ActionTelegram_jsx.606264b1.chunk.js.map +0 -1
- package/admin/rules/static/js/src_bootstrap_jsx.daa4bc6c.chunk.js +0 -3
- package/admin/rules/static/js/src_bootstrap_jsx.daa4bc6c.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}`);
|
|
@@ -421,32 +454,31 @@ function startAdapter(options) {
|
|
|
421
454
|
}
|
|
422
455
|
|
|
423
456
|
/**
|
|
424
|
-
* Send message to all system users
|
|
457
|
+
* Send a 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,31 +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
|
-
|
|
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
|
+
) {
|
|
692
790
|
adapter.log.debug(`Send venue to "${name}": ${options.latitude},${options.longitude}`);
|
|
693
|
-
bot &&
|
|
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
|
+
);
|
|
694
805
|
} else if (options && options.latitude !== undefined && options.longitude !== undefined) {
|
|
695
806
|
adapter.log.debug(`Send location to "${name}": ${options.latitude},${options.longitude}`);
|
|
696
|
-
bot &&
|
|
807
|
+
bot &&
|
|
808
|
+
executeSending(
|
|
809
|
+
() => bot.sendLocation(dest, parseFloat(options.latitude), parseFloat(options.longitude), options),
|
|
810
|
+
options,
|
|
811
|
+
resolve,
|
|
812
|
+
);
|
|
697
813
|
} else if (options && options.type === 'mediagroup') {
|
|
698
814
|
adapter.log.debug(`Send media group to "${name}": `);
|
|
699
815
|
if (bot) {
|
|
700
|
-
const {media: fileNames} = options;
|
|
816
|
+
const { media: fileNames } = options;
|
|
701
817
|
if (fileNames instanceof Array) {
|
|
702
818
|
bot.sendChatAction(dest, 'upload_photo')
|
|
703
819
|
.then(() => {
|
|
@@ -705,7 +821,7 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
705
821
|
const filesAsArray = fileNames
|
|
706
822
|
.map(element => {
|
|
707
823
|
try {
|
|
708
|
-
return {type: 'photo', media: fs.readFileSync(element)};
|
|
824
|
+
return { type: 'photo', media: fs.readFileSync(element) };
|
|
709
825
|
} catch (err) {
|
|
710
826
|
adapter.log.error(`Cannot read file ${element}: ${err}`);
|
|
711
827
|
return undefined;
|
|
@@ -742,55 +858,62 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
742
858
|
} else if (text && typeof text === 'string' && actions.includes(text)) {
|
|
743
859
|
adapter.log.debug(`Send action to "${name}": ${text}`);
|
|
744
860
|
bot && executeSending(() => bot.sendChatAction(dest, text), options, resolve);
|
|
745
|
-
} else if (
|
|
861
|
+
} else if (
|
|
862
|
+
text &&
|
|
863
|
+
((typeof text === 'string' && text.match(/\.webp$/i) && fs.existsSync(text)) ||
|
|
864
|
+
(options && options.type === 'sticker'))
|
|
865
|
+
) {
|
|
746
866
|
if (typeof text === 'string') {
|
|
747
867
|
adapter.log.debug(`Send sticker to "${name}": ${text}`);
|
|
748
868
|
} else {
|
|
749
869
|
adapter.log.debug(`Send sticker to "${name}": ${text.length} bytes`);
|
|
750
870
|
}
|
|
751
871
|
bot && executeSending(() => bot.sendSticker(dest, text, options), options, resolve);
|
|
752
|
-
} else if (
|
|
872
|
+
} else if (
|
|
873
|
+
text &&
|
|
874
|
+
((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) ||
|
|
875
|
+
(options && options.type === 'animation'))
|
|
876
|
+
) {
|
|
753
877
|
if (typeof text === 'string') {
|
|
754
878
|
adapter.log.debug(`Send animation to "${name}": ${text}`);
|
|
755
879
|
} else {
|
|
756
880
|
adapter.log.debug(`Send animation to "${name}": ${text.length} bytes`);
|
|
757
881
|
}
|
|
758
882
|
bot && executeSending(() => bot.sendAnimation(dest, text, options), options, resolve);
|
|
759
|
-
} else if (
|
|
883
|
+
} else if (
|
|
884
|
+
text &&
|
|
885
|
+
((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) ||
|
|
886
|
+
(options && options.type === 'video'))
|
|
887
|
+
) {
|
|
760
888
|
if (typeof text === 'string') {
|
|
761
889
|
adapter.log.debug(`Send video to "${name}": ${text}`);
|
|
762
890
|
} else {
|
|
763
891
|
adapter.log.debug(`Send video to "${name}": ${text.length} bytes`);
|
|
764
892
|
}
|
|
765
893
|
bot && executeSending(() => bot.sendVideo(dest, text, options), options, resolve);
|
|
766
|
-
} else if (
|
|
767
|
-
|
|
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}`);
|
|
768
900
|
bot && executeSending(() => bot.sendDocument(dest, text, options), options, resolve);
|
|
769
901
|
} else if (
|
|
770
902
|
text &&
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
text.match(/\.(wav|mp3|ogg)$/i) &&
|
|
774
|
-
fs.existsSync(text)
|
|
775
|
-
) ||
|
|
776
|
-
(options && options?.type === 'audio')
|
|
777
|
-
)
|
|
903
|
+
((typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) ||
|
|
904
|
+
(options && options?.type === 'audio'))
|
|
778
905
|
) {
|
|
779
|
-
adapter.log.debug(`Send audio to "${name}": ${
|
|
906
|
+
adapter.log.debug(`Send audio to "${name}": ${typeof text === 'string' ? text : text.length}`);
|
|
780
907
|
|
|
781
908
|
bot && executeSending(() => bot.sendAudio(dest, text, options), options, resolve);
|
|
782
909
|
} else if (
|
|
783
910
|
text &&
|
|
784
|
-
(
|
|
785
|
-
(
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
(fs.existsSync(text) || text.match(/^(https|http)/i))
|
|
789
|
-
) ||
|
|
790
|
-
(options && options.type === 'photo') // if the type of message is photo
|
|
791
|
-
)
|
|
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
|
|
792
915
|
) {
|
|
793
|
-
adapter.log.debug(`Send photo to "${name}": ${
|
|
916
|
+
adapter.log.debug(`Send photo to "${name}": ${typeof text === 'string' ? text : text.length}`);
|
|
794
917
|
|
|
795
918
|
bot && executeSending(() => bot.sendPhoto(dest, text, options), options, resolve);
|
|
796
919
|
} else if (options && options.answerCallbackQuery !== undefined) {
|
|
@@ -801,7 +924,16 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
801
924
|
if (bot && callbackQueryId[options.chatId]) {
|
|
802
925
|
const originalChatId = callbackQueryId[options.chatId].id;
|
|
803
926
|
delete callbackQueryId[options.chatId];
|
|
804
|
-
executeSending(
|
|
927
|
+
executeSending(
|
|
928
|
+
() =>
|
|
929
|
+
bot.answerCallbackQuery(
|
|
930
|
+
originalChatId,
|
|
931
|
+
options.answerCallbackQuery.text,
|
|
932
|
+
options.answerCallbackQuery.showAlert,
|
|
933
|
+
),
|
|
934
|
+
options,
|
|
935
|
+
resolve,
|
|
936
|
+
);
|
|
805
937
|
}
|
|
806
938
|
} else {
|
|
807
939
|
adapter.log.debug(`Send message to [${name}]: "${text}"`);
|
|
@@ -825,8 +957,12 @@ function _sendMessageHelper(dest, name, text, options) {
|
|
|
825
957
|
|
|
826
958
|
/**
|
|
827
959
|
* executes the given method and handles, what to do next
|
|
960
|
+
*
|
|
961
|
+
* @param action
|
|
962
|
+
* @param options
|
|
963
|
+
* @param resolve
|
|
828
964
|
*/
|
|
829
|
-
function executeSending(action, options, resolve){
|
|
965
|
+
function executeSending(action, options, resolve) {
|
|
830
966
|
// create an empty object, to store chat id and message id of successfully sent messages
|
|
831
967
|
const messageIds = {};
|
|
832
968
|
action()
|
|
@@ -837,7 +973,11 @@ function executeSending(action, options, resolve){
|
|
|
837
973
|
if (response?.message_id) {
|
|
838
974
|
// The chatId is mostly used in code, instead of chat_id.
|
|
839
975
|
messageIds[options.chat_id ? options.chat_id : options.chatId] = response.message_id;
|
|
840
|
-
} else if (
|
|
976
|
+
} else if (
|
|
977
|
+
typeof response === 'boolean' &&
|
|
978
|
+
options?.deleteMessage?.options?.chat_id &&
|
|
979
|
+
options?.deleteMessage?.options?.message_id
|
|
980
|
+
) {
|
|
841
981
|
messageIds[options.deleteMessage.options.chat_id] = options.deleteMessage.options.message_id;
|
|
842
982
|
}
|
|
843
983
|
// puts ids to the ioBroker database
|
|
@@ -851,9 +991,11 @@ function executeSending(action, options, resolve){
|
|
|
851
991
|
})
|
|
852
992
|
.catch(error => {
|
|
853
993
|
// add the error to the message ids object
|
|
854
|
-
messageIds.error = {[options.chat_id ? options.chat_id : options.chatId]
|
|
994
|
+
messageIds.error = { [options.chat_id ? options.chat_id : options.chatId]: error };
|
|
855
995
|
// log error to the system
|
|
856
|
-
adapter.log.error(
|
|
996
|
+
adapter.log.error(
|
|
997
|
+
`Failed sending [${options.chatId ? 'chatId' : 'user'} - ${options.chatId ? options.chatId : options.user}]: ${error}`,
|
|
998
|
+
);
|
|
857
999
|
options = null;
|
|
858
1000
|
// send the successfully sent messages as callback
|
|
859
1001
|
resolve(JSON.stringify(messageIds));
|
|
@@ -867,7 +1009,13 @@ function sendMessage(text, user, chatId, options) {
|
|
|
867
1009
|
return Promise.resolve({});
|
|
868
1010
|
}
|
|
869
1011
|
|
|
870
|
-
if (
|
|
1012
|
+
if (
|
|
1013
|
+
text &&
|
|
1014
|
+
typeof text === 'object' &&
|
|
1015
|
+
text.text !== undefined &&
|
|
1016
|
+
typeof text.text === 'string' &&
|
|
1017
|
+
options === undefined
|
|
1018
|
+
) {
|
|
871
1019
|
options = text;
|
|
872
1020
|
text = options.text;
|
|
873
1021
|
if (options.chatId) {
|
|
@@ -912,14 +1060,19 @@ function sendMessage(text, user, chatId, options) {
|
|
|
912
1060
|
}
|
|
913
1061
|
if (chatId) {
|
|
914
1062
|
tPromiseList.push(_sendMessageHelper(chatId, 'chat', text, options));
|
|
915
|
-
return Promise.all(tPromiseList)
|
|
916
|
-
.catch(e => e);
|
|
1063
|
+
return Promise.all(tPromiseList).catch(e => e);
|
|
917
1064
|
} else if (user) {
|
|
918
1065
|
if (typeof user !== 'string' && !(user instanceof Array)) {
|
|
919
1066
|
adapter.log.warn(`Invalid type of user parameter: ${typeof user}. Expected is string or array.`);
|
|
920
1067
|
}
|
|
921
1068
|
|
|
922
|
-
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);
|
|
923
1076
|
let matches = 0;
|
|
924
1077
|
userArray.forEach(userName => {
|
|
925
1078
|
for (const id in users) {
|
|
@@ -927,8 +1080,10 @@ function sendMessage(text, user, chatId, options) {
|
|
|
927
1080
|
continue;
|
|
928
1081
|
}
|
|
929
1082
|
|
|
930
|
-
if (
|
|
931
|
-
|
|
1083
|
+
if (
|
|
1084
|
+
(adapter.config.useUsername && users[id].userName === userName) ||
|
|
1085
|
+
(!adapter.config.useUsername && users[id].firstName === userName)
|
|
1086
|
+
) {
|
|
932
1087
|
if (options) {
|
|
933
1088
|
options.chatId = id;
|
|
934
1089
|
}
|
|
@@ -943,8 +1098,7 @@ function sendMessage(text, user, chatId, options) {
|
|
|
943
1098
|
adapter.log.warn(`${userArray.length - matches} of ${userArray.length} recipients are unknown!`);
|
|
944
1099
|
}
|
|
945
1100
|
|
|
946
|
-
return Promise.all(tPromiseList)
|
|
947
|
-
.catch(e => e);
|
|
1101
|
+
return Promise.all(tPromiseList).catch(e => e);
|
|
948
1102
|
}
|
|
949
1103
|
|
|
950
1104
|
const m = typeof text === 'string' ? text.match(/^@(.+?)\b/) : null;
|
|
@@ -958,7 +1112,10 @@ function sendMessage(text, user, chatId, options) {
|
|
|
958
1112
|
if (!Object.prototype.hasOwnProperty.call(users, id_t)) {
|
|
959
1113
|
continue;
|
|
960
1114
|
}
|
|
961
|
-
if (
|
|
1115
|
+
if (
|
|
1116
|
+
(adapter.config.useUsername && users[id_t].userName.match(re)) ||
|
|
1117
|
+
(!adapter.config.useUsername && users[id_t].firstName.match(re))
|
|
1118
|
+
) {
|
|
962
1119
|
id = id_t;
|
|
963
1120
|
break;
|
|
964
1121
|
}
|
|
@@ -975,12 +1132,18 @@ function sendMessage(text, user, chatId, options) {
|
|
|
975
1132
|
if (options) {
|
|
976
1133
|
options.chatId = id;
|
|
977
1134
|
}
|
|
978
|
-
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
|
+
);
|
|
979
1143
|
});
|
|
980
1144
|
}
|
|
981
1145
|
|
|
982
|
-
return Promise.all(tPromiseList)
|
|
983
|
-
.catch(e => e);
|
|
1146
|
+
return Promise.all(tPromiseList).catch(e => e);
|
|
984
1147
|
}
|
|
985
1148
|
|
|
986
1149
|
function saveFile(fileID, fileName, callback) {
|
|
@@ -1002,7 +1165,7 @@ function saveFile(fileID, fileName, callback) {
|
|
|
1002
1165
|
callback({
|
|
1003
1166
|
info: `media file has been saved to "${adapter.config.saveFilesTo}": ${fileLocation}`,
|
|
1004
1167
|
location: adapter.config.saveFilesTo,
|
|
1005
|
-
path: fileLocation
|
|
1168
|
+
path: fileLocation,
|
|
1006
1169
|
});
|
|
1007
1170
|
} catch (err) {
|
|
1008
1171
|
return callback({ error: `Error: ${err}` });
|
|
@@ -1011,14 +1174,13 @@ function saveFile(fileID, fileName, callback) {
|
|
|
1011
1174
|
try {
|
|
1012
1175
|
const fileLocation = path.join(tmpDirName, fileName); // TODO: check new urn format https://github.com/ioBroker/ioBroker.js-controller/issues/2710
|
|
1013
1176
|
|
|
1014
|
-
adapter.writeFileAsync(adapter.namespace, fileName, Buffer.concat(buf))
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
path: fileLocation,
|
|
1020
|
-
});
|
|
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,
|
|
1021
1182
|
});
|
|
1183
|
+
});
|
|
1022
1184
|
} catch (err) {
|
|
1023
1185
|
return callback({ error: `Error: ${err}` });
|
|
1024
1186
|
}
|
|
@@ -1057,7 +1219,7 @@ function getMessage(msg) {
|
|
|
1057
1219
|
0: 'low',
|
|
1058
1220
|
1: 'med',
|
|
1059
1221
|
2: 'high',
|
|
1060
|
-
3: 'highdef'
|
|
1222
|
+
3: 'highdef',
|
|
1061
1223
|
};
|
|
1062
1224
|
|
|
1063
1225
|
let saveOnlyQuality = adapter.config.saveFilesQuality;
|
|
@@ -1083,7 +1245,7 @@ function getMessage(msg) {
|
|
|
1083
1245
|
const id = Object.keys(mediaGroupExport).length;
|
|
1084
1246
|
mediaGroupExport[msg.media_group_id] = {
|
|
1085
1247
|
id,
|
|
1086
|
-
count: 0
|
|
1248
|
+
count: 0,
|
|
1087
1249
|
};
|
|
1088
1250
|
} else {
|
|
1089
1251
|
mediaGroupExport[msg.media_group_id].count++;
|
|
@@ -1179,7 +1341,9 @@ async function processMessage(obj) {
|
|
|
1179
1341
|
// filter out the double messages
|
|
1180
1342
|
const json = JSON.stringify(obj);
|
|
1181
1343
|
if (lastMessageTime && lastMessageText === JSON.stringify(obj) && Date.now() - lastMessageTime < 1200) {
|
|
1182
|
-
return adapter.log.debug(
|
|
1344
|
+
return adapter.log.debug(
|
|
1345
|
+
`Filter out double message [first was for ${Date.now() - lastMessageTime}ms]: ${json}`,
|
|
1346
|
+
);
|
|
1183
1347
|
}
|
|
1184
1348
|
|
|
1185
1349
|
lastMessageTime = Date.now();
|
|
@@ -1206,13 +1370,18 @@ async function processMessage(obj) {
|
|
|
1206
1370
|
case 'ask':
|
|
1207
1371
|
if (obj.message) {
|
|
1208
1372
|
const question = {
|
|
1209
|
-
cb:
|
|
1373
|
+
cb: obj.callback,
|
|
1210
1374
|
from: obj.from,
|
|
1211
|
-
ts:
|
|
1375
|
+
ts: Date.now(),
|
|
1212
1376
|
};
|
|
1213
1377
|
|
|
1214
1378
|
if (typeof obj.message === 'object') {
|
|
1215
|
-
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
|
+
);
|
|
1216
1385
|
const msgIds = messages.length > 0 ? messages.map(JSON.parse)[0] : {};
|
|
1217
1386
|
|
|
1218
1387
|
question.chatId = obj.message.chatId;
|
|
@@ -1224,23 +1393,32 @@ async function processMessage(obj) {
|
|
|
1224
1393
|
|
|
1225
1394
|
if (obj.callback) {
|
|
1226
1395
|
adapter._questions.push(question);
|
|
1227
|
-
adapter.log.debug(
|
|
1396
|
+
adapter.log.debug(
|
|
1397
|
+
`added question: ${JSON.stringify(question)} - answer timeout: ${adapter.config.answerTimeoutSec}`,
|
|
1398
|
+
);
|
|
1228
1399
|
|
|
1229
|
-
question.timeout = setTimeout(
|
|
1230
|
-
q
|
|
1231
|
-
|
|
1400
|
+
question.timeout = setTimeout(
|
|
1401
|
+
q => {
|
|
1402
|
+
q.timeout = null;
|
|
1403
|
+
adapter.sendTo(q.from, 'ask', '__timeout__', q.callback);
|
|
1232
1404
|
|
|
1233
|
-
|
|
1405
|
+
adapter.log.info(`question timeout for: ${JSON.stringify(q)}`);
|
|
1234
1406
|
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
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
|
+
}
|
|
1240
1415
|
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1416
|
+
const pos = adapter._questions.indexOf(q);
|
|
1417
|
+
pos !== -1 && adapter._questions.splice(pos);
|
|
1418
|
+
},
|
|
1419
|
+
adapter.config.answerTimeoutSec + 1000,
|
|
1420
|
+
question,
|
|
1421
|
+
);
|
|
1244
1422
|
}
|
|
1245
1423
|
}
|
|
1246
1424
|
break;
|
|
@@ -1263,7 +1441,9 @@ async function processMessage(obj) {
|
|
|
1263
1441
|
if (!call.users && !call.user) {
|
|
1264
1442
|
call.users = Object.keys(users)
|
|
1265
1443
|
.filter(id => users[id] && users[id].userName)
|
|
1266
|
-
.map(id =>
|
|
1444
|
+
.map(id =>
|
|
1445
|
+
users[id].userName.startsWith('@') ? users[id].userName : `@${users[id].userName}`,
|
|
1446
|
+
);
|
|
1267
1447
|
}
|
|
1268
1448
|
if (!(call.users instanceof Array)) {
|
|
1269
1449
|
call.users = [call.users];
|
|
@@ -1279,7 +1459,9 @@ async function processMessage(obj) {
|
|
|
1279
1459
|
|
|
1280
1460
|
//
|
|
1281
1461
|
if (!call.users || !call.users.length) {
|
|
1282
|
-
adapter.log.error(
|
|
1462
|
+
adapter.log.error(
|
|
1463
|
+
`Cannot make a call, because no users stored in ${adapter.namespace}.communicate.users`,
|
|
1464
|
+
);
|
|
1283
1465
|
} else {
|
|
1284
1466
|
callUsers(call.users, call.message, call.lang, call.file, call.repeats);
|
|
1285
1467
|
}
|
|
@@ -1312,13 +1494,18 @@ function callUsers(users, text, lang, file, repeats, cb) {
|
|
|
1312
1494
|
url += params.join('&');
|
|
1313
1495
|
adapter.log.debug(`CALL: ${url}`);
|
|
1314
1496
|
|
|
1315
|
-
axios
|
|
1497
|
+
axios
|
|
1498
|
+
.get(url)
|
|
1316
1499
|
.then(response => {
|
|
1317
1500
|
const body = response && response.data;
|
|
1318
1501
|
if (!body || !response || response.status !== 200) {
|
|
1319
|
-
adapter.log.error(
|
|
1502
|
+
adapter.log.error(
|
|
1503
|
+
`Cannot make a call to ${user}: ${body || (response && response.status) || 'Unknown error'}`,
|
|
1504
|
+
);
|
|
1320
1505
|
} else {
|
|
1321
|
-
adapter.log.debug(
|
|
1506
|
+
adapter.log.debug(
|
|
1507
|
+
`Call to ${user} was made: ${body.substring(body.indexOf('<p>')).replace(/<p>/g, ' ')}`,
|
|
1508
|
+
);
|
|
1322
1509
|
}
|
|
1323
1510
|
setImmediate(callUsers, users, text, lang, file, repeats, cb);
|
|
1324
1511
|
})
|
|
@@ -1340,7 +1527,7 @@ function storeUser(id, firstName, userName) {
|
|
|
1340
1527
|
}
|
|
1341
1528
|
});
|
|
1342
1529
|
|
|
1343
|
-
users[id] = {firstName, userName, sysMessages: false};
|
|
1530
|
+
users[id] = { firstName, userName, sysMessages: false };
|
|
1344
1531
|
|
|
1345
1532
|
if (adapter.config.rememberUsers) {
|
|
1346
1533
|
adapter.setState('communicate.users', JSON.stringify(users), true);
|
|
@@ -1356,9 +1543,13 @@ function getListOfCommands() {
|
|
|
1356
1543
|
if (!commands[id].readOnly) {
|
|
1357
1544
|
if (commands[id].type === 'boolean') {
|
|
1358
1545
|
if (commands[id].writeOnly) {
|
|
1359
|
-
lines.push(
|
|
1546
|
+
lines.push(
|
|
1547
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}|${commands[id].offCommand || _('OFF-Command')}`,
|
|
1548
|
+
);
|
|
1360
1549
|
} else {
|
|
1361
|
-
lines.push(
|
|
1550
|
+
lines.push(
|
|
1551
|
+
`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}|${commands[id].offCommand || _('OFF-Command')}|?`,
|
|
1552
|
+
);
|
|
1362
1553
|
}
|
|
1363
1554
|
} else {
|
|
1364
1555
|
if (commands[id].writeOnly) {
|
|
@@ -1389,19 +1580,17 @@ function getCommandsKeyboard(chatId) {
|
|
|
1389
1580
|
keyboard.push([`${commands[id].alias} ${commands[id].onCommand || _('ON-Command')}`]);
|
|
1390
1581
|
!commands[id].writeOnly && keyboard.push([`${commands[id].alias} ?`]);
|
|
1391
1582
|
} else {
|
|
1392
|
-
commands[id].writeOnly
|
|
1393
|
-
keyboard.push([
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
`${commands[id].alias} ?`
|
|
1399
|
-
]);
|
|
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
|
+
]);
|
|
1400
1589
|
}
|
|
1401
1590
|
} else {
|
|
1402
1591
|
if (commands[id].buttons === 1) {
|
|
1403
|
-
keyboard.push([`${commands[id].alias} ${commands[id].onCommand
|
|
1404
|
-
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')}`]);
|
|
1405
1594
|
!commands[id].writeOnly && keyboard.push([`${commands[id].alias} ?`]);
|
|
1406
1595
|
} else if (commands[id].buttons === 2) {
|
|
1407
1596
|
keyboard.push([
|
|
@@ -1410,16 +1599,16 @@ function getCommandsKeyboard(chatId) {
|
|
|
1410
1599
|
]);
|
|
1411
1600
|
!commands[id].writeOnly && keyboard.push([`${commands[id].alias} ?`]);
|
|
1412
1601
|
} else {
|
|
1413
|
-
commands[id].writeOnly
|
|
1414
|
-
keyboard.push([
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
keyboard.push([
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
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
|
+
]);
|
|
1423
1612
|
}
|
|
1424
1613
|
}
|
|
1425
1614
|
} else if (commands[id].states) {
|
|
@@ -1447,7 +1636,9 @@ function getCommandsKeyboard(chatId) {
|
|
|
1447
1636
|
!commands[id].writeOnly && s.push(`${commands[id].alias} ?`);
|
|
1448
1637
|
keyboard.push(s);
|
|
1449
1638
|
} else {
|
|
1450
|
-
adapter.log.warn(
|
|
1639
|
+
adapter.log.warn(
|
|
1640
|
+
`Unsupported state type for keyboard: ${commands[id].type}. Only numbers and booleans are supported`,
|
|
1641
|
+
);
|
|
1451
1642
|
}
|
|
1452
1643
|
} else {
|
|
1453
1644
|
keyboard.push([`${commands[id].alias} ?`]);
|
|
@@ -1458,14 +1649,14 @@ function getCommandsKeyboard(chatId) {
|
|
|
1458
1649
|
reply_markup: {
|
|
1459
1650
|
keyboard,
|
|
1460
1651
|
resize_keyboard: true,
|
|
1461
|
-
one_time_keyboard: true
|
|
1652
|
+
one_time_keyboard: true,
|
|
1462
1653
|
},
|
|
1463
|
-
chatId
|
|
1654
|
+
chatId,
|
|
1464
1655
|
})
|
|
1465
1656
|
.then(() => {
|
|
1466
1657
|
adapter.log.debug('Message sent');
|
|
1467
1658
|
})
|
|
1468
|
-
.catch(
|
|
1659
|
+
.catch(error => {
|
|
1469
1660
|
adapter.log.error(`Send message error: ${error}`);
|
|
1470
1661
|
});
|
|
1471
1662
|
}
|
|
@@ -1475,8 +1666,15 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1475
1666
|
const now = Date.now();
|
|
1476
1667
|
const chatId = msg.chat && msg.chat.id;
|
|
1477
1668
|
|
|
1478
|
-
let question =
|
|
1479
|
-
|
|
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));
|
|
1480
1678
|
question = question || adapter._questions.find(q => now - q.ts < adapter.config.answerTimeoutSec);
|
|
1481
1679
|
|
|
1482
1680
|
// user have 60 seconds for answer
|
|
@@ -1488,7 +1686,10 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1488
1686
|
|
|
1489
1687
|
// Remove keyboard
|
|
1490
1688
|
if (bot && question?.chatId && question?.msgId) {
|
|
1491
|
-
bot.editMessageReplyMarkup(
|
|
1689
|
+
bot.editMessageReplyMarkup(
|
|
1690
|
+
{ inline_keyboard: [] },
|
|
1691
|
+
{ chat_id: question.chatId, message_id: question.msgId },
|
|
1692
|
+
);
|
|
1492
1693
|
adapter.log.debug(`removed inline_keyboard for msg ${question.msgId}: ${JSON.stringify(question)}`);
|
|
1493
1694
|
}
|
|
1494
1695
|
}
|
|
@@ -1503,23 +1704,22 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1503
1704
|
function garbageCollector() {
|
|
1504
1705
|
const now = Date.now() - 5 * 60000; // last 5 minutes
|
|
1505
1706
|
|
|
1506
|
-
Object.keys(protection)
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
break;
|
|
1513
|
-
}
|
|
1514
|
-
}
|
|
1515
|
-
// remove all old entries
|
|
1516
|
-
if (a < protection[user].length && a) {
|
|
1517
|
-
protection[user].splice(0, a);
|
|
1518
|
-
}
|
|
1519
|
-
if (!protection[user].length) {
|
|
1520
|
-
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;
|
|
1521
1713
|
}
|
|
1522
|
-
}
|
|
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
|
+
});
|
|
1523
1723
|
|
|
1524
1724
|
if (!Object.keys(protection).length) {
|
|
1525
1725
|
gcInterval && clearInterval(gcInterval);
|
|
@@ -1533,7 +1733,11 @@ function processTelegramText(msg) {
|
|
|
1533
1733
|
// see https://core.telegram.org/bots/api#message
|
|
1534
1734
|
adapter.log.debug(JSON.stringify(msg));
|
|
1535
1735
|
|
|
1536
|
-
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;
|
|
1537
1741
|
|
|
1538
1742
|
const now = Date.now();
|
|
1539
1743
|
let pollingInterval = 0;
|
|
@@ -1543,13 +1747,15 @@ function processTelegramText(msg) {
|
|
|
1543
1747
|
|
|
1544
1748
|
// ignore all messages older than 30 seconds + polling interval
|
|
1545
1749
|
if (now - msg.date * 1000 > pollingInterval + 30000) {
|
|
1546
|
-
adapter.log.warn(
|
|
1547
|
-
|
|
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)
|
|
1548
1755
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1549
|
-
|
|
1550
1756
|
}
|
|
1551
1757
|
|
|
1552
|
-
msg.text = (msg.text ||'').trim();
|
|
1758
|
+
msg.text = (msg.text || '').trim();
|
|
1553
1759
|
|
|
1554
1760
|
// sometimes telegram sends messages like "message@user_name"
|
|
1555
1761
|
const pos = msg.text.lastIndexOf('@');
|
|
@@ -1558,7 +1764,8 @@ function processTelegramText(msg) {
|
|
|
1558
1764
|
}
|
|
1559
1765
|
|
|
1560
1766
|
if (msg.text === '/password' && !adapter.config.doNotAcceptNewUser) {
|
|
1561
|
-
return bot
|
|
1767
|
+
return bot
|
|
1768
|
+
.sendMessage(msg.from.id, _('Please enter password in form "/password phrase"', systemLang))
|
|
1562
1769
|
.catch(error => adapter.log.error(`send Message Error:${error}`));
|
|
1563
1770
|
}
|
|
1564
1771
|
|
|
@@ -1571,7 +1778,11 @@ function processTelegramText(msg) {
|
|
|
1571
1778
|
garbageCollector();
|
|
1572
1779
|
|
|
1573
1780
|
if (protection[user] && protection[user].length >= 5) {
|
|
1574
|
-
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
|
+
)
|
|
1575
1786
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1576
1787
|
}
|
|
1577
1788
|
|
|
@@ -1585,22 +1796,23 @@ function processTelegramText(msg) {
|
|
|
1585
1796
|
adapter.log.warn(`User ${msg.from.first_name} hast not set an username in the Telegram App!!`);
|
|
1586
1797
|
}
|
|
1587
1798
|
|
|
1588
|
-
return bot
|
|
1799
|
+
return bot
|
|
1800
|
+
.sendMessage(msg.from.id, _('Welcome ', systemLang) + user)
|
|
1589
1801
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1590
|
-
}
|
|
1591
|
-
|
|
1592
|
-
|
|
1802
|
+
}
|
|
1803
|
+
protection[user] = protection[user] || [];
|
|
1804
|
+
protection[user].push(Date.now());
|
|
1593
1805
|
|
|
1594
|
-
|
|
1806
|
+
gcInterval = gcInterval || setInterval(() => garbageCollector(), 60000);
|
|
1595
1807
|
|
|
1596
|
-
|
|
1808
|
+
adapter.log.warn(`Got invalid password from ${user}: ${m[1]}`);
|
|
1597
1809
|
|
|
1598
|
-
|
|
1599
|
-
|
|
1810
|
+
bot.sendMessage(msg.from.id, _('Invalid password', systemLang)).catch(error =>
|
|
1811
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1812
|
+
);
|
|
1600
1813
|
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
}
|
|
1814
|
+
if (users[msg.from.id]) {
|
|
1815
|
+
delete users[msg.from.id];
|
|
1604
1816
|
}
|
|
1605
1817
|
}
|
|
1606
1818
|
}
|
|
@@ -1609,12 +1821,22 @@ function processTelegramText(msg) {
|
|
|
1609
1821
|
|
|
1610
1822
|
// If a user is not in the trusted list
|
|
1611
1823
|
if ((adapter.config.password || adapter.config.doNotAcceptNewUsers) && !users[msg.from.id]) {
|
|
1612
|
-
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
|
+
)
|
|
1613
1834
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1614
1835
|
}
|
|
1615
1836
|
|
|
1616
1837
|
if (msg.text === '/help') {
|
|
1617
|
-
return bot
|
|
1838
|
+
return bot
|
|
1839
|
+
.sendMessage(msg.from.id, getListOfCommands())
|
|
1618
1840
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1619
1841
|
}
|
|
1620
1842
|
|
|
@@ -1644,34 +1866,43 @@ function processTelegramText(msg) {
|
|
|
1644
1866
|
found = true;
|
|
1645
1867
|
if (sValue === '?') {
|
|
1646
1868
|
adapter.getForeignState(id, (err, state) =>
|
|
1647
|
-
bot
|
|
1648
|
-
.
|
|
1869
|
+
bot
|
|
1870
|
+
.sendMessage(msg.chat.id, getStatus(id, state))
|
|
1871
|
+
.catch(error => adapter.log.error(`send Message Error: ${error}`)),
|
|
1872
|
+
);
|
|
1649
1873
|
} else {
|
|
1650
1874
|
let value;
|
|
1651
1875
|
if (commands[id].states) {
|
|
1652
|
-
const sState = Object.keys(commands[id].states)
|
|
1653
|
-
|
|
1876
|
+
const sState = Object.keys(commands[id].states).find(
|
|
1877
|
+
val => commands[id].states[val] === sValue,
|
|
1878
|
+
);
|
|
1654
1879
|
if (sState !== null && sState !== undefined) {
|
|
1655
1880
|
sValue = sState;
|
|
1656
1881
|
}
|
|
1657
1882
|
}
|
|
1658
1883
|
|
|
1659
1884
|
if (commands[id].type === 'boolean') {
|
|
1660
|
-
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';
|
|
1661
1891
|
} else if (commands[id].type === 'number') {
|
|
1662
1892
|
sValue = sValue.replace('%', '').trim();
|
|
1663
1893
|
value = parseFloat(sValue);
|
|
1664
1894
|
if (sValue !== value.toString()) {
|
|
1665
|
-
bot.sendMessage(msg.chat.id, _('Invalid number %s', sValue))
|
|
1666
|
-
|
|
1895
|
+
bot.sendMessage(msg.chat.id, _('Invalid number %s', sValue)).catch(error =>
|
|
1896
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1897
|
+
);
|
|
1667
1898
|
continue;
|
|
1668
1899
|
}
|
|
1669
1900
|
} else {
|
|
1670
1901
|
value = sValue;
|
|
1671
1902
|
}
|
|
1672
1903
|
|
|
1673
|
-
adapter
|
|
1674
|
-
bot.sendMessage(msg.chat.id, _('Done')))
|
|
1904
|
+
adapter
|
|
1905
|
+
.setForeignState(id, value, false, () => bot.sendMessage(msg.chat.id, _('Done')))
|
|
1675
1906
|
.catch(error => adapter.log.error(`send Message Error: ${error}`));
|
|
1676
1907
|
}
|
|
1677
1908
|
}
|
|
@@ -1710,24 +1941,28 @@ function processTelegramText(msg) {
|
|
|
1710
1941
|
}
|
|
1711
1942
|
if (msg) {
|
|
1712
1943
|
if (err) {
|
|
1713
|
-
bot.sendMessage(msg.from.id, err)
|
|
1714
|
-
|
|
1944
|
+
bot.sendMessage(msg.from.id, err).catch(error =>
|
|
1945
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1946
|
+
);
|
|
1715
1947
|
}
|
|
1716
1948
|
if (state) {
|
|
1717
1949
|
adapter.setForeignState(id1, val1, false, err => {
|
|
1718
1950
|
if (msg) {
|
|
1719
1951
|
if (err) {
|
|
1720
|
-
bot.sendMessage(msg.from.id, err)
|
|
1721
|
-
|
|
1952
|
+
bot.sendMessage(msg.from.id, err).catch(error =>
|
|
1953
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1954
|
+
);
|
|
1722
1955
|
} else {
|
|
1723
|
-
bot.sendMessage(msg.from.id, _('Done', systemLang))
|
|
1724
|
-
|
|
1956
|
+
bot.sendMessage(msg.from.id, _('Done', systemLang)).catch(error =>
|
|
1957
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1958
|
+
);
|
|
1725
1959
|
}
|
|
1726
1960
|
}
|
|
1727
1961
|
});
|
|
1728
1962
|
} else {
|
|
1729
|
-
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id1))
|
|
1730
|
-
|
|
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
|
+
);
|
|
1731
1966
|
}
|
|
1732
1967
|
}
|
|
1733
1968
|
});
|
|
@@ -1754,15 +1989,18 @@ function processTelegramText(msg) {
|
|
|
1754
1989
|
}
|
|
1755
1990
|
if (msg) {
|
|
1756
1991
|
if (err) {
|
|
1757
|
-
bot.sendMessage(msg.from.id, err)
|
|
1758
|
-
|
|
1992
|
+
bot.sendMessage(msg.from.id, err).catch(error =>
|
|
1993
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1994
|
+
);
|
|
1759
1995
|
}
|
|
1760
1996
|
if (state) {
|
|
1761
|
-
bot.sendMessage(msg.from.id, state.val.toString())
|
|
1762
|
-
|
|
1997
|
+
bot.sendMessage(msg.from.id, state.val.toString()).catch(error =>
|
|
1998
|
+
adapter.log.error(`send Message Error: ${error}`),
|
|
1999
|
+
);
|
|
1763
2000
|
} else {
|
|
1764
|
-
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id2))
|
|
1765
|
-
|
|
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
|
+
);
|
|
1766
2004
|
}
|
|
1767
2005
|
}
|
|
1768
2006
|
});
|
|
@@ -1774,38 +2012,50 @@ function processTelegramText(msg) {
|
|
|
1774
2012
|
|
|
1775
2013
|
// Send to text2command
|
|
1776
2014
|
if (adapter.config.text2command) {
|
|
1777
|
-
adapter.sendTo(
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
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
|
+
}
|
|
1795
2038
|
}
|
|
1796
|
-
}
|
|
1797
2039
|
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
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
|
+
);
|
|
1803
2047
|
}
|
|
1804
2048
|
|
|
1805
2049
|
adapter.setState('communicate.requestChatId', { val: msg.chat.id, ack: true });
|
|
1806
2050
|
adapter.setState('communicate.requestMessageId', { val: msg.message_id, ack: true });
|
|
1807
|
-
adapter.setState('communicate.requestMessageThreadId', {
|
|
1808
|
-
|
|
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
|
+
});
|
|
1809
2059
|
adapter.setState('communicate.request', { val: `[${user}]${msg.text}`, ack: true });
|
|
1810
2060
|
}
|
|
1811
2061
|
|
|
@@ -1813,9 +2063,9 @@ function connect() {
|
|
|
1813
2063
|
if (bot) {
|
|
1814
2064
|
if (!adapter.config.server) {
|
|
1815
2065
|
try {
|
|
1816
|
-
if (bot.isPolling())
|
|
2066
|
+
if (bot.isPolling()) {
|
|
1817
2067
|
adapter.log.debug('bot polling OK');
|
|
1818
|
-
else {
|
|
2068
|
+
} else {
|
|
1819
2069
|
adapter.log.debug('bot restarting...');
|
|
1820
2070
|
bot.stopPolling().then(
|
|
1821
2071
|
() => {
|
|
@@ -1824,7 +2074,7 @@ function connect() {
|
|
|
1824
2074
|
},
|
|
1825
2075
|
error => {
|
|
1826
2076
|
adapter.log.error(`Error stop polling: ${error}`);
|
|
1827
|
-
}
|
|
2077
|
+
},
|
|
1828
2078
|
);
|
|
1829
2079
|
}
|
|
1830
2080
|
} catch {
|
|
@@ -1844,7 +2094,7 @@ function connect() {
|
|
|
1844
2094
|
const serverOptions = {
|
|
1845
2095
|
polling: false,
|
|
1846
2096
|
filepath: true,
|
|
1847
|
-
baseApiUrl: adapter.config.baseApiUrl
|
|
2097
|
+
baseApiUrl: adapter.config.baseApiUrl,
|
|
1848
2098
|
};
|
|
1849
2099
|
bot = new TelegramBot(adapter.config.token, serverOptions);
|
|
1850
2100
|
if (adapter.config.url[adapter.config.url.length - 1] === '/') {
|
|
@@ -1855,12 +2105,14 @@ function connect() {
|
|
|
1855
2105
|
// Setup polling way
|
|
1856
2106
|
const pollingOptions = {
|
|
1857
2107
|
polling: {
|
|
1858
|
-
interval: parseInt(adapter.config.pollingInterval, 10) || 300
|
|
2108
|
+
interval: parseInt(adapter.config.pollingInterval, 10) || 300,
|
|
1859
2109
|
},
|
|
1860
2110
|
filepath: true,
|
|
1861
|
-
baseApiUrl: adapter.config.baseApiUrl
|
|
2111
|
+
baseApiUrl: adapter.config.baseApiUrl,
|
|
1862
2112
|
};
|
|
1863
|
-
adapter.log.debug(
|
|
2113
|
+
adapter.log.debug(
|
|
2114
|
+
`Start polling with: ${pollingOptions.polling.interval}(${typeof pollingOptions.polling.interval}) ms interval`,
|
|
2115
|
+
);
|
|
1864
2116
|
bot = new TelegramBot(adapter.config.token, pollingOptions);
|
|
1865
2117
|
bot.setWebHook('').catch(error => {
|
|
1866
2118
|
adapter.log.error(`setWebHook Error:${error}`);
|
|
@@ -1868,22 +2120,28 @@ function connect() {
|
|
|
1868
2120
|
}
|
|
1869
2121
|
|
|
1870
2122
|
// Check connection
|
|
1871
|
-
bot.getMe()
|
|
1872
|
-
|
|
1873
|
-
|
|
2123
|
+
bot.getMe()
|
|
2124
|
+
.then(data => {
|
|
2125
|
+
adapter.log.debug(`getMe: ${JSON.stringify(data)}`);
|
|
2126
|
+
connectionState(true);
|
|
1874
2127
|
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
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
|
+
}
|
|
1881
2139
|
}
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
2140
|
+
})
|
|
2141
|
+
.catch(error => {
|
|
2142
|
+
adapter.log.error(`getMe Error:${error}`);
|
|
2143
|
+
connectionState(false);
|
|
2144
|
+
});
|
|
1887
2145
|
|
|
1888
2146
|
// Matches /echo [whatever]
|
|
1889
2147
|
bot.onText(/(.+)/, processTelegramText);
|
|
@@ -1906,15 +2164,38 @@ function connect() {
|
|
|
1906
2164
|
callbackQueryId[callbackQuery.from.id] = { id: callbackQuery.id, ts: Date.now() };
|
|
1907
2165
|
|
|
1908
2166
|
if (adapter.config.storeRawRequest) {
|
|
1909
|
-
adapter.setState(
|
|
1910
|
-
|
|
2167
|
+
adapter.setState(
|
|
2168
|
+
'communicate.requestRaw',
|
|
2169
|
+
JSON.stringify(callbackQuery),
|
|
2170
|
+
true,
|
|
2171
|
+
err => err && adapter.log.error(err),
|
|
2172
|
+
);
|
|
1911
2173
|
}
|
|
1912
2174
|
|
|
1913
|
-
adapter.setState(
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
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
|
+
);
|
|
1918
2199
|
|
|
1919
2200
|
isAnswerForQuestion(adapter, callbackQuery);
|
|
1920
2201
|
});
|
|
@@ -1937,7 +2218,7 @@ function connect() {
|
|
|
1937
2218
|
},
|
|
1938
2219
|
error => {
|
|
1939
2220
|
adapter.log.error(`Error stop polling: ${error}`);
|
|
1940
|
-
}
|
|
2221
|
+
},
|
|
1941
2222
|
);
|
|
1942
2223
|
});
|
|
1943
2224
|
}
|
|
@@ -1955,9 +2236,17 @@ async function updateUsers() {
|
|
|
1955
2236
|
Object.keys(users).forEach(id => {
|
|
1956
2237
|
if (typeof users[id] !== 'object') {
|
|
1957
2238
|
if (adapter.config.useUsername) {
|
|
1958
|
-
users[id] = {
|
|
2239
|
+
users[id] = {
|
|
2240
|
+
userName: users[id],
|
|
2241
|
+
firstName: users[id],
|
|
2242
|
+
sysMessages: users[id].sysMessages !== false,
|
|
2243
|
+
};
|
|
1959
2244
|
} else {
|
|
1960
|
-
users[id] = {
|
|
2245
|
+
users[id] = {
|
|
2246
|
+
firstName: users[id],
|
|
2247
|
+
userName: '',
|
|
2248
|
+
sysMessages: users[id].sysMessages !== false,
|
|
2249
|
+
};
|
|
1961
2250
|
}
|
|
1962
2251
|
}
|
|
1963
2252
|
});
|
|
@@ -1978,12 +2267,12 @@ async function readAllNames(ids) {
|
|
|
1978
2267
|
try {
|
|
1979
2268
|
const obj = await adapter.getForeignObjectAsync(ids[i]);
|
|
1980
2269
|
if (obj) {
|
|
1981
|
-
commands[ids[i]].alias
|
|
1982
|
-
commands[ids[i]].type
|
|
2270
|
+
commands[ids[i]].alias = getName(obj);
|
|
2271
|
+
commands[ids[i]].type = obj.common && obj.common.type;
|
|
1983
2272
|
commands[ids[i]].states = obj.common && parseStates(obj.common.states || undefined);
|
|
1984
|
-
commands[ids[i]].unit
|
|
1985
|
-
commands[ids[i]].min
|
|
1986
|
-
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;
|
|
1987
2276
|
// read actual state to detect changes
|
|
1988
2277
|
if (commands[ids[i]].reportChanges) {
|
|
1989
2278
|
const state = await adapter.getForeignStateAsync(ids[i]);
|
|
@@ -2020,7 +2309,10 @@ async function readEnums(name) {
|
|
|
2020
2309
|
name = name || 'rooms';
|
|
2021
2310
|
enums[name] = {};
|
|
2022
2311
|
try {
|
|
2023
|
-
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
|
+
});
|
|
2024
2316
|
if (doc && doc.rows) {
|
|
2025
2317
|
for (let i = 0, l = doc.rows.length; i < l; i++) {
|
|
2026
2318
|
if (doc.rows[i].value) {
|
|
@@ -2096,13 +2388,12 @@ async function main() {
|
|
|
2096
2388
|
}
|
|
2097
2389
|
}
|
|
2098
2390
|
|
|
2099
|
-
|
|
2100
2391
|
/**
|
|
2101
2392
|
* Process a `sendNotification` request
|
|
2102
2393
|
*
|
|
2103
|
-
* @param
|
|
2394
|
+
* @param obj
|
|
2104
2395
|
*/
|
|
2105
|
-
async function processNotification(
|
|
2396
|
+
async function processNotification(obj) {
|
|
2106
2397
|
adapter.log.info(`New notification received from ${obj.from}`);
|
|
2107
2398
|
|
|
2108
2399
|
const notificationMessage = buildMessageFromNotification(obj.message);
|
|
@@ -2116,20 +2407,21 @@ async function processNotification( obj) {
|
|
|
2116
2407
|
adapter.sendTo(obj.from, 'sendNotification', { sent: false }, obj.callback);
|
|
2117
2408
|
}
|
|
2118
2409
|
}
|
|
2119
|
-
|
|
2120
2410
|
}
|
|
2121
2411
|
|
|
2122
2412
|
/**
|
|
2123
2413
|
* Build up a mail object from the notification message
|
|
2124
2414
|
*
|
|
2125
|
-
* @param
|
|
2126
|
-
* @returns
|
|
2415
|
+
* @param message
|
|
2416
|
+
* @returns
|
|
2127
2417
|
*/
|
|
2128
2418
|
function buildMessageFromNotification(message) {
|
|
2129
2419
|
const subject = message.category.name;
|
|
2130
2420
|
const { instances } = message.category;
|
|
2131
2421
|
|
|
2132
|
-
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
|
+
);
|
|
2133
2425
|
|
|
2134
2426
|
const text = `${message.category.description}
|
|
2135
2427
|
${message.host}:
|
|
@@ -2142,8 +2434,8 @@ ${readableInstances.join('\n')}
|
|
|
2142
2434
|
/**
|
|
2143
2435
|
* Replace reserved characters in outgoing message
|
|
2144
2436
|
*
|
|
2145
|
-
* @param
|
|
2146
|
-
* @returns
|
|
2437
|
+
* @param text string to sanitize
|
|
2438
|
+
* @returns
|
|
2147
2439
|
*/
|
|
2148
2440
|
function replaceReservedCharacters(text) {
|
|
2149
2441
|
return text.replace(/([.!()-_*[\]~`><&#+=|{}])/g, '\\$1');
|
|
@@ -2152,11 +2444,11 @@ function replaceReservedCharacters(text) {
|
|
|
2152
2444
|
/**
|
|
2153
2445
|
* Extract the newest message out of a notification messages together with the localized date
|
|
2154
2446
|
*
|
|
2155
|
-
* @param
|
|
2156
|
-
* @
|
|
2447
|
+
* @param messages
|
|
2448
|
+
* @returns string
|
|
2157
2449
|
*/
|
|
2158
2450
|
function getNewestMessage(messages) {
|
|
2159
|
-
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];
|
|
2160
2452
|
|
|
2161
2453
|
return `${new Date(newestMessage.ts).toLocaleString()} ${newestMessage.message}`;
|
|
2162
2454
|
}
|