iobroker.telegram 1.10.1 → 1.11.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/README.md +5 -0
- package/admin/blockly.js +17 -4
- package/admin/words.js +24 -0
- package/io-package.json +26 -14
- package/main.js +173 -114
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -703,6 +703,11 @@ msg.payload = {
|
|
|
703
703
|
-->
|
|
704
704
|
|
|
705
705
|
## Changelog
|
|
706
|
+
### 1.11.0 (2022-01-26)
|
|
707
|
+
* (bluefox) Added bruteforce protection
|
|
708
|
+
* (bluefox) Extended blockly with `disable_web_preview` option
|
|
709
|
+
* (bluefox) added `communicate.responseSilent` state to answer silently
|
|
710
|
+
|
|
706
711
|
### 1.10.1 (2022-01-26)
|
|
707
712
|
* (bluefox) Updated telegram library
|
|
708
713
|
|
package/admin/blockly.js
CHANGED
|
@@ -31,7 +31,7 @@ Blockly.Words['telegram_log_warn'] = {'en': 'warning',
|
|
|
31
31
|
Blockly.Words['telegram_log_error'] = {'en': 'error', 'pt': 'erro', 'pl': 'błąd', 'nl': 'fout', 'it': 'errore', 'es': 'error', 'fr': 'Erreur', 'de': 'error', 'ru': 'ошибка'};
|
|
32
32
|
Blockly.Words['telegram_help'] = {'en': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'pt': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'pl': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'nl': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'it': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'es': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'fr': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'de': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md', 'ru': 'https://github.com/ioBroker/ioBroker.telegram/blob/master/README.md'};
|
|
33
33
|
Blockly.Words['telegram_silent'] = {'en': 'without notification', 'de': 'ohne Benachrichtigung', 'ru': 'без уведомления', 'pt': 'sem notificação', 'nl': 'zonder kennisgeving', 'fr': 'sans notification', 'it': 'senza notifica', 'es': 'sin notificación', 'pl': 'bez powiadomienia'};
|
|
34
|
-
|
|
34
|
+
Blockly.Words['telegram_disable_web_preview'] = {'en': 'disable web preview', 'de': 'Deaktivieren Sie die Webvorschau', 'ru': 'отключить предварительный просмотр в Интернете', 'pt': 'desativar a visualização da web', 'nl': 'webvoorbeeld uitschakelen', 'fr': 'désactiver l\'aperçu Web','it': 'disabilitare l\'anteprima web', 'es': 'deshabilitar la vista previa web', 'pl': 'wyłącz podgląd internetowy', 'zh-cn': '禁用网页预览'};
|
|
35
35
|
Blockly.Sendto.blocks['telegram'] =
|
|
36
36
|
'<block type="telegram">'
|
|
37
37
|
+ ' <value name="INSTANCE">'
|
|
@@ -51,6 +51,8 @@ Blockly.Sendto.blocks['telegram'] =
|
|
|
51
51
|
+ ' </value>'
|
|
52
52
|
+ ' <value name="PARSEMODE">'
|
|
53
53
|
+ ' </value>'
|
|
54
|
+
+ ' <value name="DISABLE_WEB_PAGE_PREVIEW">'
|
|
55
|
+
+ ' </value>'
|
|
54
56
|
+ '</block>';
|
|
55
57
|
|
|
56
58
|
Blockly.Blocks['telegram'] = {
|
|
@@ -86,7 +88,7 @@ Blockly.Blocks['telegram'] = {
|
|
|
86
88
|
.setCheck('String')
|
|
87
89
|
.appendField(Blockly.Translate('telegram_username'));
|
|
88
90
|
|
|
89
|
-
var
|
|
91
|
+
var input1 = this.appendValueInput('CHATID')
|
|
90
92
|
.setCheck('String')
|
|
91
93
|
.appendField(Blockly.Translate('telegram_chatid'));
|
|
92
94
|
|
|
@@ -106,9 +108,18 @@ Blockly.Blocks['telegram'] = {
|
|
|
106
108
|
|
|
107
109
|
this.appendDummyInput('PARSEMODE')
|
|
108
110
|
.appendField('Parsemode')
|
|
109
|
-
.appendField(new Blockly.FieldDropdown([['default', 'default'], ['HTML', 'HTML'], ['
|
|
111
|
+
.appendField(new Blockly.FieldDropdown([['default', 'default'], ['HTML', 'HTML'], ['MarkdownV2', 'MarkdownV2']]), 'PARSEMODE');
|
|
110
112
|
|
|
111
|
-
|
|
113
|
+
this.appendDummyInput('DISABLE_WEB_PAGE_PREVIEW')
|
|
114
|
+
.appendField(Blockly.Translate('telegram_disable_web_preview'))
|
|
115
|
+
.appendField(new Blockly.FieldCheckbox('FALSE'), 'DISABLE_WEB_PAGE_PREVIEW');
|
|
116
|
+
|
|
117
|
+
if (input.connection) {
|
|
118
|
+
input.connection._optional = true;
|
|
119
|
+
}
|
|
120
|
+
if (input1.connection) {
|
|
121
|
+
input1.connection._optional = true;
|
|
122
|
+
}
|
|
112
123
|
|
|
113
124
|
this.setInputsInline(false);
|
|
114
125
|
this.setPreviousStatement(true, null);
|
|
@@ -127,6 +138,7 @@ Blockly.JavaScript['telegram'] = function(block) {
|
|
|
127
138
|
var value_username = Blockly.JavaScript.valueToCode(block, 'USERNAME', Blockly.JavaScript.ORDER_ATOMIC);
|
|
128
139
|
var value_chatid = Blockly.JavaScript.valueToCode(block, 'CHATID', Blockly.JavaScript.ORDER_ATOMIC);
|
|
129
140
|
var silent = block.getFieldValue('SILENT');
|
|
141
|
+
var disable_web_page_preview = block.getFieldValue('DISABLE_WEB_PAGE_PREVIEW');
|
|
130
142
|
var parsemode = block.getFieldValue('PARSEMODE');
|
|
131
143
|
|
|
132
144
|
var logText;
|
|
@@ -140,6 +152,7 @@ Blockly.JavaScript['telegram'] = function(block) {
|
|
|
140
152
|
value_message + (value_username ? ', \n user: ' + value_username : '') +
|
|
141
153
|
(value_chatid ? ', \n chatId: ' + value_chatid : '') +
|
|
142
154
|
(silent === 'TRUE' ? ', \n disable_notification: true' : '') +
|
|
155
|
+
(disable_web_page_preview === 'TRUE' ? ', \n disable_web_page_preview: true' : '') +
|
|
143
156
|
(parsemode !== 'default' ? ', \n parse_mode: "' + parsemode + '"': '') +
|
|
144
157
|
'\n});\n' +
|
|
145
158
|
logText;
|
package/admin/words.js
CHANGED
|
@@ -79,4 +79,28 @@ systemDictionary = {
|
|
|
79
79
|
"value as boolean": { "en": "value as boolean", "de": "Wert als Boolean", "ru": "логическое значение", "pt": "valor como booleano", "nl": "waarde als Boolean", "fr": "valeur en tant que booléen", "it": "valore come booleano", "es": "valor como booleano", "pl": "wartość jako boolean", "zh-cn": "布尔值"},
|
|
80
80
|
"value as number": { "en": "value as number", "de": "Wert als Zahl", "ru": "значение в виде число", "pt": "valor como numero", "nl": "waarde als nummer", "fr": "valeur en nombre", "it": "valore come numero", "es": "valor como numero", "pl": "wartość jako liczba", "zh-cn": "价值作为数字"},
|
|
81
81
|
"value as string": { "en": "value as string", "de": "Wert als String", "ru": "значение в виде строки", "pt": "valor como string", "nl": "waarde als tekenreeks", "fr": "valeur sous forme de chaîne", "it": "valore come stringa", "es": "valor como cadena", "pl": "wartość jako ciąg", "zh-cn": "值作为字符串"},
|
|
82
|
+
"Too many attempts. Blocked for": {
|
|
83
|
+
"en": "Too many attempts. Blocked for",
|
|
84
|
+
"de": "Zu viele Versuche. Gesperrt für",
|
|
85
|
+
"ru": "Слишком много попыток. Заблокировано для",
|
|
86
|
+
"pt": "Muitas tentativas. Bloqueado por",
|
|
87
|
+
"nl": "Te veel pogingen. Geblokkeerd voor",
|
|
88
|
+
"fr": "Trop d'essais. Bloqué pour",
|
|
89
|
+
"it": "Troppi tentativi. Bloccato per",
|
|
90
|
+
"es": "Demasiados intentos. bloqueado por",
|
|
91
|
+
"pl": "Zbyt wiele prób. Zablokowany przez",
|
|
92
|
+
"zh-cn": "太多的尝试。被阻止"
|
|
93
|
+
},
|
|
94
|
+
"seconds": {
|
|
95
|
+
"en": "seconds",
|
|
96
|
+
"de": "Sekunden",
|
|
97
|
+
"ru": "секунд/ы",
|
|
98
|
+
"pt": "segundos",
|
|
99
|
+
"nl": "seconden",
|
|
100
|
+
"fr": "secondes",
|
|
101
|
+
"it": "secondi",
|
|
102
|
+
"es": "segundos",
|
|
103
|
+
"pl": "sekundy",
|
|
104
|
+
"zh-cn": "秒"
|
|
105
|
+
}
|
|
82
106
|
};
|
package/io-package.json
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"common": {
|
|
3
3
|
"name": "telegram",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.11.0",
|
|
5
5
|
"news": {
|
|
6
|
+
"1.11.0": {
|
|
7
|
+
"en": "Added bruteforce protection\nExtended blockly with `disable_web_preview` option\nadded `communicate.responseSilent` state to answer silently",
|
|
8
|
+
"de": "Bruteforce-Schutz hinzugefügt\nBlockweise erweitert mit der Option `disable_web_preview`\n`communicate.responseSilent`-Zustand hinzugefügt, um still zu antworten",
|
|
9
|
+
"ru": "Добавлена защита от брутфорса\nРасширен блочно с опцией `disable_web_preview`\nдобавлено состояние `communicate.responseSilent` для молчаливого ответа",
|
|
10
|
+
"pt": "Proteção de força bruta adicionada\nEstendido em bloco com a opção `disable_web_preview`\nadicionado o estado `communicate.responseSilent` para responder silenciosamente",
|
|
11
|
+
"nl": "Bruteforce-bescherming toegevoegd\nBlockly uitgebreid met de optie 'disable_web_preview'\nstatus `communicate.responseSilent` toegevoegd om stil te antwoorden",
|
|
12
|
+
"fr": "Protection contre la force brute ajoutée\nÉtendu en bloc avec l'option `disable_web_preview`\najout de l'état `communicate.responseSilent` pour répondre en silence",
|
|
13
|
+
"it": "Aggiunta protezione dalla forza bruta\nEsteso in blocco con l'opzione `disable_web_preview`\naggiunto lo stato `communicate.responseSilent` per rispondere in silenzio",
|
|
14
|
+
"es": "Protección de fuerza bruta agregada\nExtendido en bloques con la opción `disable_web_preview`\nSe agregó el estado `communicate.responseSilent` para responder en silencio.",
|
|
15
|
+
"pl": "Dodano ochronę przed bruteforce\nRozszerzony blokowo z opcją `disable_web_preview`\ndodano stan `communicate.responseSilent` do cichej odpowiedzi",
|
|
16
|
+
"zh-cn": "添加了蛮力保护\n使用 `disable_web_preview` 选项扩展块\n添加了`communicate.responseSilent`状态以静默回答"
|
|
17
|
+
},
|
|
6
18
|
"1.10.1": {
|
|
7
19
|
"en": "Updated telegram library",
|
|
8
20
|
"de": "Aktualisierte Telegrammbibliothek",
|
|
@@ -66,18 +78,6 @@
|
|
|
66
78
|
"es": "agregó el soporte admin5",
|
|
67
79
|
"pl": "dodano obsługę admin5",
|
|
68
80
|
"zh-cn": "添加了admin5支持"
|
|
69
|
-
},
|
|
70
|
-
"1.8.0": {
|
|
71
|
-
"en": "(Apollon77/Nahasapeemapetilon) catch several API error cases to hopefully get around adapter crashes on network errors\nadd support for media groups and multiple image qualities",
|
|
72
|
-
"de": "(Apollon77 / Nahasapeemapetilon) fängt mehrere API-Fehlerfälle ab, um hoffentlich Adapterabstürze bei Netzwerkfehlern zu umgehen\nUnterstützung für Mediengruppen und mehrere Bildqualitäten hinzufügen",
|
|
73
|
-
"ru": "(Apollon77 / Nahasapeemapetilon) отлавливает несколько случаев ошибок API, чтобы, надеюсь, обойти сбои адаптера при сетевых ошибках\nдобавить поддержку медиа-групп и различных качеств изображений",
|
|
74
|
-
"pt": "(Apollon77 / Nahasapeemapetilon) captura vários casos de erro de API para contornar travamentos do adaptador em erros de rede\nadicionar suporte para grupos de mídia e várias qualidades de imagem",
|
|
75
|
-
"nl": "(Apollon77 / Nahasapeemapetilon) vangen verschillende API-foutgevallen op om hopelijk adaptercrashes door netwerkfouten te omzeilen\nondersteuning toevoegen voor mediagroepen en meerdere beeldkwaliteiten",
|
|
76
|
-
"fr": "(Apollon77 / Nahasapeemapetilon) attrapez plusieurs cas d'erreur d'API pour, espérons-le, contourner les plantages d'adaptateur en cas d'erreurs réseau\najouter la prise en charge des groupes de médias et de multiples qualités d'image",
|
|
77
|
-
"it": "(Apollon77 / Nahasapeemapetilon) rileva diversi casi di errore API per aggirare, si spera, i crash dell'adattatore in caso di errori di rete\naggiungere il supporto per i gruppi di media e più qualità di immagine",
|
|
78
|
-
"es": "(Apollon77 / Nahasapeemapetilon) detecta varios casos de error de API para, con suerte, evitar fallas del adaptador en errores de red\nagregar soporte para grupos de medios y múltiples calidades de imagen",
|
|
79
|
-
"pl": "(Apollon77 / Nahasapeemapetilon) Wyłap kilka przypadków błędów interfejsu API, aby mieć nadzieję na obejście awarii karty w przypadku błędów sieciowych\ndodać obsługę grup medialnych i wielu jakości obrazu",
|
|
80
|
-
"zh-cn": "(Apollon77 / Nahasapeemapetilon)捕获了几种API错误案例,以期解决因网络错误引起的适配器崩溃\n添加对媒体组和多种图像质量的支持"
|
|
81
81
|
}
|
|
82
82
|
},
|
|
83
83
|
"title": "Telegram",
|
|
@@ -289,7 +289,19 @@
|
|
|
289
289
|
"type": "state",
|
|
290
290
|
"common": {
|
|
291
291
|
"role": "text",
|
|
292
|
-
"name": "Send text through telegram",
|
|
292
|
+
"name": "Send text through telegram with notification",
|
|
293
|
+
"type": "string",
|
|
294
|
+
"read": true,
|
|
295
|
+
"write": true
|
|
296
|
+
},
|
|
297
|
+
"native": {}
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
"_id": "communicate.responseSilent",
|
|
301
|
+
"type": "state",
|
|
302
|
+
"common": {
|
|
303
|
+
"role": "text",
|
|
304
|
+
"name": "Send text through telegram silently",
|
|
293
305
|
"type": "string",
|
|
294
306
|
"read": true,
|
|
295
307
|
"write": true
|
package/main.js
CHANGED
|
@@ -39,6 +39,8 @@ let isConnected = null;
|
|
|
39
39
|
let lastMessageTime = 0;
|
|
40
40
|
let lastMessageText = '';
|
|
41
41
|
const enums = {};
|
|
42
|
+
const protection = {};
|
|
43
|
+
let gcInterval = null;
|
|
42
44
|
|
|
43
45
|
const commands = {};
|
|
44
46
|
const callbackQueryId = {};
|
|
@@ -223,12 +225,14 @@ function startAdapter(options) {
|
|
|
223
225
|
});
|
|
224
226
|
adapter.log.info('https server listening on port ' + port);
|
|
225
227
|
|
|
226
|
-
main()
|
|
228
|
+
main()
|
|
229
|
+
.then(() => {});
|
|
227
230
|
});
|
|
228
231
|
}
|
|
229
232
|
});
|
|
230
233
|
} else {
|
|
231
|
-
main()
|
|
234
|
+
main()
|
|
235
|
+
.then(() => {});
|
|
232
236
|
}
|
|
233
237
|
});
|
|
234
238
|
|
|
@@ -236,6 +240,9 @@ function startAdapter(options) {
|
|
|
236
240
|
reconnectTimer && clearInterval(reconnectTimer);
|
|
237
241
|
reconnectTimer = null;
|
|
238
242
|
|
|
243
|
+
gcInterval && clearInterval(gcInterval);
|
|
244
|
+
gcInterval = null;
|
|
245
|
+
|
|
239
246
|
pollConnectionStatus && clearInterval(pollConnectionStatus);
|
|
240
247
|
pollConnectionStatus = null;
|
|
241
248
|
|
|
@@ -267,12 +274,15 @@ function startAdapter(options) {
|
|
|
267
274
|
adapter.on('stateChange', (id, state) => {
|
|
268
275
|
if (state) {
|
|
269
276
|
if (!state.ack) {
|
|
270
|
-
if (id.
|
|
277
|
+
if (id.endsWith('communicate.response')) {
|
|
271
278
|
// Send to someone this message
|
|
272
279
|
sendMessage(state.val)
|
|
273
|
-
.then(data =>
|
|
274
|
-
|
|
275
|
-
|
|
280
|
+
.then(data => adapter.setState('communicate.response', state.val, true));
|
|
281
|
+
} else
|
|
282
|
+
if (id.endsWith('communicate.responseSilent')) {
|
|
283
|
+
// Send to someone this message
|
|
284
|
+
sendMessage(state.val, null, null, {disable_notification: true})
|
|
285
|
+
.then(data => adapter.setState('communicate.responseSilent', state.val, true));
|
|
276
286
|
}
|
|
277
287
|
} else {
|
|
278
288
|
if (commands[id] && commands[id].report) {
|
|
@@ -323,7 +333,7 @@ function sendSystemMessage(text) {
|
|
|
323
333
|
.filter(id => users[id].sysMessages !== false)
|
|
324
334
|
.map(id => adapter.config.useUsername ? users[id].userName : users[id].firstName);
|
|
325
335
|
|
|
326
|
-
sendMessage(text, _users);
|
|
336
|
+
sendMessage(text, _users, null, {disable_notification: true});
|
|
327
337
|
}
|
|
328
338
|
|
|
329
339
|
function getStatus(id, state) {
|
|
@@ -939,7 +949,9 @@ function sendMessage(text, user, chatId, options) {
|
|
|
939
949
|
delete options.user;
|
|
940
950
|
}
|
|
941
951
|
}
|
|
952
|
+
|
|
942
953
|
options = options || {};
|
|
954
|
+
|
|
943
955
|
let tPromiseList = [];
|
|
944
956
|
// convert
|
|
945
957
|
if (text !== undefined && text !== null && typeof text !== 'object') {
|
|
@@ -1024,31 +1036,33 @@ function sendMessage(text, user, chatId, options) {
|
|
|
1024
1036
|
.catch(e => -1);
|
|
1025
1037
|
}
|
|
1026
1038
|
|
|
1027
|
-
function saveFile(
|
|
1028
|
-
bot.getFileLink(
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1039
|
+
function saveFile(fileID, fileName, callback) {
|
|
1040
|
+
bot.getFileLink(fileID)
|
|
1041
|
+
.then(url => {
|
|
1042
|
+
adapter.log.debug('Received message: ' + url);
|
|
1043
|
+
https.get(url, res => {
|
|
1044
|
+
if (res.statusCode === 200) {
|
|
1045
|
+
const buf = [];
|
|
1046
|
+
res.on('data', data => buf.push(data));
|
|
1047
|
+
res.on('end', () =>
|
|
1048
|
+
fs.writeFile(tmpDirName + fileName, Buffer.concat(buf), true, err => {
|
|
1049
|
+
if (err) {
|
|
1050
|
+
throw err;
|
|
1051
|
+
}
|
|
1052
|
+
callback({
|
|
1053
|
+
info: 'It\'s saved! : ' + tmpDirName + fileName,
|
|
1054
|
+
path: tmpDirName + fileName
|
|
1055
|
+
});
|
|
1056
|
+
}));
|
|
1044
1057
|
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1058
|
+
res.on('error', true, err =>
|
|
1059
|
+
callback({error: 'Error: ' + err}));
|
|
1060
|
+
} else {
|
|
1061
|
+
callback({error: 'Error: statusCode !== 200'});
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
1064
|
+
})
|
|
1065
|
+
.catch(err => callback({error: 'Error bot.getFileLink: ' + err}));
|
|
1052
1066
|
}
|
|
1053
1067
|
|
|
1054
1068
|
function getMessage(msg) {
|
|
@@ -1104,6 +1118,7 @@ function getMessage(msg) {
|
|
|
1104
1118
|
} while (fs.existsSync(tmpDirName + fileName));
|
|
1105
1119
|
}
|
|
1106
1120
|
}
|
|
1121
|
+
|
|
1107
1122
|
saveFile(item.file_id, fileName, res => {
|
|
1108
1123
|
if (!res.error) {
|
|
1109
1124
|
adapter.log.info(res.info);
|
|
@@ -1158,7 +1173,7 @@ function processMessage(obj) {
|
|
|
1158
1173
|
// filter out double messages
|
|
1159
1174
|
const json = JSON.stringify(obj);
|
|
1160
1175
|
if (lastMessageTime && lastMessageText === JSON.stringify(obj) && Date.now() - lastMessageTime < 1200) {
|
|
1161
|
-
return adapter.log.debug(
|
|
1176
|
+
return adapter.log.debug(`Filter out double message [first was for ${Date.now() - lastMessageTime}ms]: ${json}`);
|
|
1162
1177
|
}
|
|
1163
1178
|
|
|
1164
1179
|
lastMessageTime = Date.now();
|
|
@@ -1475,6 +1490,33 @@ function isAnswerForQuestion(adapter, msg) {
|
|
|
1475
1490
|
}
|
|
1476
1491
|
}
|
|
1477
1492
|
|
|
1493
|
+
function garbageCollector() {
|
|
1494
|
+
const now = Date.now() - 5 * 60000; // last 5 minutes
|
|
1495
|
+
|
|
1496
|
+
Object.keys(protection)
|
|
1497
|
+
.forEach(user => {
|
|
1498
|
+
let a;
|
|
1499
|
+
for (a = 0; a < protection[user].length; a++) {
|
|
1500
|
+
// find first entry newer than 5 minutes
|
|
1501
|
+
if (protection[user][a] > now) {
|
|
1502
|
+
break;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
// remove all old entries
|
|
1506
|
+
if (a < protection[user].length && a) {
|
|
1507
|
+
protection[user].splice(0, a);
|
|
1508
|
+
}
|
|
1509
|
+
if (!protection[user].length) {
|
|
1510
|
+
delete protection[user];
|
|
1511
|
+
}
|
|
1512
|
+
});
|
|
1513
|
+
|
|
1514
|
+
if (!Object.keys(protection).length) {
|
|
1515
|
+
gcInterval && clearInterval(gcInterval);
|
|
1516
|
+
gcInterval = null;
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1478
1520
|
function processTelegramText(msg) {
|
|
1479
1521
|
connectionState(true);
|
|
1480
1522
|
|
|
@@ -1493,7 +1535,7 @@ function processTelegramText(msg) {
|
|
|
1493
1535
|
if (now - msg.date * 1000 > pollingInterval + 30000) {
|
|
1494
1536
|
adapter.log.warn(`Message from ${msg.from.name} ignored, because too old: (${pollingInterval + 30000}) ${msg.text}`);
|
|
1495
1537
|
return bot.sendMessage(msg.from.id, _('Message ignored: ', systemLang) + msg.text)
|
|
1496
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1538
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1497
1539
|
|
|
1498
1540
|
}
|
|
1499
1541
|
|
|
@@ -1507,9 +1549,7 @@ function processTelegramText(msg) {
|
|
|
1507
1549
|
|
|
1508
1550
|
if (msg.text === '/password' && !adapter.config.doNotAcceptNewUser) {
|
|
1509
1551
|
return bot.sendMessage(msg.from.id, _('Please enter password in form "/password phrase"', systemLang))
|
|
1510
|
-
.catch(error =>
|
|
1511
|
-
adapter.log.error('send Message Error:' + error);
|
|
1512
|
-
});
|
|
1552
|
+
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1513
1553
|
}
|
|
1514
1554
|
|
|
1515
1555
|
if (adapter.config.password && !adapter.config.doNotAcceptNewUsers) {
|
|
@@ -1518,17 +1558,36 @@ function processTelegramText(msg) {
|
|
|
1518
1558
|
m = m || msg.text.match(/^\/p (.+)$/);
|
|
1519
1559
|
|
|
1520
1560
|
if (m) {
|
|
1561
|
+
garbageCollector();
|
|
1562
|
+
|
|
1563
|
+
if (protection[user] && protection[user].length >= 5) {
|
|
1564
|
+
return bot.sendMessage(msg.from.id, _('Too many attempts. Blocked for', systemLang) + ' ' + Math.round((now - protection[user][protection[user].length - 1]) / 1000) + ' ' + _('seconds', systemLang))
|
|
1565
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1521
1568
|
if (adapter.config.password === m[1]) {
|
|
1569
|
+
if (protection[user]) {
|
|
1570
|
+
delete protection[user];
|
|
1571
|
+
}
|
|
1522
1572
|
storeUser(msg.from.id, msg.from.first_name, msg.from.username);
|
|
1573
|
+
|
|
1523
1574
|
if (!msg.from.username) {
|
|
1524
1575
|
adapter.log.warn(`User ${msg.from.first_name} hast not set an username in the Telegram App!!`);
|
|
1525
1576
|
}
|
|
1577
|
+
|
|
1526
1578
|
return bot.sendMessage(msg.from.id, _('Welcome ', systemLang) + user)
|
|
1527
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1579
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1528
1580
|
} else {
|
|
1581
|
+
protection[user] = protection[user] || [];
|
|
1582
|
+
protection[user].push(Date.now());
|
|
1583
|
+
|
|
1584
|
+
gcInterval = gcInterval || setInterval(() => garbageCollector(), 60000);
|
|
1585
|
+
|
|
1529
1586
|
adapter.log.warn(`Got invalid password from ${user}: ${m[1]}`);
|
|
1587
|
+
|
|
1530
1588
|
bot.sendMessage(msg.from.id, _('Invalid password', systemLang))
|
|
1531
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1589
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1590
|
+
|
|
1532
1591
|
if (users[msg.from.id]) {
|
|
1533
1592
|
delete users[msg.from.id];
|
|
1534
1593
|
}
|
|
@@ -1541,12 +1600,12 @@ function processTelegramText(msg) {
|
|
|
1541
1600
|
// If user is not in the trusted list
|
|
1542
1601
|
if ((adapter.config.password || adapter.config.doNotAcceptNewUsers) && !users[msg.from.id]) {
|
|
1543
1602
|
return bot.sendMessage(msg.from.id, _(adapter.config.doNotAcceptNewUsers ? 'User is not in the list' : 'Please enter password in form "/password phrase"', systemLang))
|
|
1544
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1603
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1545
1604
|
}
|
|
1546
1605
|
|
|
1547
1606
|
if (msg.text === '/help') {
|
|
1548
1607
|
return bot.sendMessage(msg.from.id, getListOfCommands())
|
|
1549
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1608
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1550
1609
|
}
|
|
1551
1610
|
|
|
1552
1611
|
isAnswerForQuestion(adapter, msg);
|
|
@@ -1576,7 +1635,7 @@ function processTelegramText(msg) {
|
|
|
1576
1635
|
if (sValue === '?') {
|
|
1577
1636
|
adapter.getForeignState(id, (err, state) =>
|
|
1578
1637
|
bot.sendMessage(msg.chat.id, getStatus(id, state))
|
|
1579
|
-
.catch(error => adapter.log.error('send Message Error:' + error)));
|
|
1638
|
+
.catch(error => adapter.log.error('send Message Error: ' + error)));
|
|
1580
1639
|
} else {
|
|
1581
1640
|
let value;
|
|
1582
1641
|
if (commands[id].states) {
|
|
@@ -1594,7 +1653,7 @@ function processTelegramText(msg) {
|
|
|
1594
1653
|
value = parseFloat(sValue);
|
|
1595
1654
|
if (sValue !== value.toString()) {
|
|
1596
1655
|
bot.sendMessage(msg.chat.id, _('Invalid number %s', sValue))
|
|
1597
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1656
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1598
1657
|
continue;
|
|
1599
1658
|
}
|
|
1600
1659
|
} else {
|
|
@@ -1603,7 +1662,7 @@ function processTelegramText(msg) {
|
|
|
1603
1662
|
|
|
1604
1663
|
adapter.setForeignState(id, value, false, err =>
|
|
1605
1664
|
bot.sendMessage(msg.chat.id, _('Done')))
|
|
1606
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1665
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1607
1666
|
}
|
|
1608
1667
|
}
|
|
1609
1668
|
}
|
|
@@ -1624,6 +1683,7 @@ function processTelegramText(msg) {
|
|
|
1624
1683
|
if (m) {
|
|
1625
1684
|
let id1 = m[1];
|
|
1626
1685
|
let val1 = m[2];
|
|
1686
|
+
|
|
1627
1687
|
// clear by timeout id
|
|
1628
1688
|
let memoryLeak1 = setTimeout(() => {
|
|
1629
1689
|
msg = null;
|
|
@@ -1640,22 +1700,22 @@ function processTelegramText(msg) {
|
|
|
1640
1700
|
}
|
|
1641
1701
|
if (msg) {
|
|
1642
1702
|
if (err) bot.sendMessage(msg.from.id, err)
|
|
1643
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1703
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1644
1704
|
if (state) {
|
|
1645
1705
|
adapter.setForeignState(id1, val1, false, err => {
|
|
1646
1706
|
if (msg) {
|
|
1647
1707
|
if (err) {
|
|
1648
1708
|
bot.sendMessage(msg.from.id, err)
|
|
1649
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1709
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1650
1710
|
} else {
|
|
1651
1711
|
bot.sendMessage(msg.from.id, _('Done', systemLang))
|
|
1652
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1712
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1653
1713
|
}
|
|
1654
1714
|
}
|
|
1655
1715
|
});
|
|
1656
1716
|
} else {
|
|
1657
1717
|
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id1))
|
|
1658
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1718
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1659
1719
|
}
|
|
1660
1720
|
}
|
|
1661
1721
|
});
|
|
@@ -1666,12 +1726,14 @@ function processTelegramText(msg) {
|
|
|
1666
1726
|
m = msg.text.match(/^\/state (.+)$/);
|
|
1667
1727
|
if (m) {
|
|
1668
1728
|
let id2 = m[1];
|
|
1729
|
+
|
|
1669
1730
|
// clear by timeout id
|
|
1670
1731
|
let memoryLeak2 = setTimeout(() => {
|
|
1671
1732
|
id2 = null;
|
|
1672
1733
|
msg = null;
|
|
1673
1734
|
memoryLeak2 = null;
|
|
1674
1735
|
}, 1000);
|
|
1736
|
+
|
|
1675
1737
|
adapter.getForeignState(id2, (err, state) => {
|
|
1676
1738
|
if (memoryLeak2) {
|
|
1677
1739
|
clearTimeout(memoryLeak2);
|
|
@@ -1680,13 +1742,13 @@ function processTelegramText(msg) {
|
|
|
1680
1742
|
}
|
|
1681
1743
|
if (msg) {
|
|
1682
1744
|
if (err) bot.sendMessage(msg.from.id, err)
|
|
1683
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1745
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1684
1746
|
if (state) {
|
|
1685
1747
|
bot.sendMessage(msg.from.id, state.val.toString())
|
|
1686
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1748
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1687
1749
|
} else {
|
|
1688
1750
|
bot.sendMessage(msg.from.id, _('ID "%s" not found.', systemLang).replace('%s', id2))
|
|
1689
|
-
.catch(error => adapter.log.error('send Message Error:' + error));
|
|
1751
|
+
.catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1690
1752
|
}
|
|
1691
1753
|
}
|
|
1692
1754
|
});
|
|
@@ -1705,7 +1767,7 @@ function processTelegramText(msg) {
|
|
|
1705
1767
|
}, response => {
|
|
1706
1768
|
if (response && response.response) {
|
|
1707
1769
|
adapter.log.debug('Send response: ' + response.response);
|
|
1708
|
-
bot.sendMessage(response.id, response.response).catch(error => adapter.log.error('send Message Error:' + error));
|
|
1770
|
+
bot.sendMessage(response.id, response.response).catch(error => adapter.log.error('send Message Error: ' + error));
|
|
1709
1771
|
}
|
|
1710
1772
|
});
|
|
1711
1773
|
}
|
|
@@ -1814,7 +1876,7 @@ function connect() {
|
|
|
1814
1876
|
baseApiUrl: adapter.config.baseApiUrl
|
|
1815
1877
|
};
|
|
1816
1878
|
if (agent) {
|
|
1817
|
-
pollingOptions.request = { agent
|
|
1879
|
+
pollingOptions.request = { agent };
|
|
1818
1880
|
}
|
|
1819
1881
|
adapter.log.debug(`Start polling with: ${pollingOptions.polling.interval}(${typeof pollingOptions.polling.interval}) ms interval`);
|
|
1820
1882
|
bot = new TelegramBot(adapter.config.token, pollingOptions);
|
|
@@ -1908,10 +1970,10 @@ function connect() {
|
|
|
1908
1970
|
}
|
|
1909
1971
|
}
|
|
1910
1972
|
|
|
1911
|
-
function updateUsers(
|
|
1973
|
+
async function updateUsers() {
|
|
1912
1974
|
if (adapter.config.rememberUsers) {
|
|
1913
|
-
|
|
1914
|
-
|
|
1975
|
+
try {
|
|
1976
|
+
const state = await adapter.getState('communicate.users');
|
|
1915
1977
|
if (state && state.val) {
|
|
1916
1978
|
try {
|
|
1917
1979
|
users = JSON.parse(state.val);
|
|
@@ -1931,10 +1993,9 @@ function updateUsers(cb) {
|
|
|
1931
1993
|
adapter.log.error('Cannot parse stored user IDs!');
|
|
1932
1994
|
}
|
|
1933
1995
|
}
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
cb && cb();
|
|
1996
|
+
} catch (err) {
|
|
1997
|
+
adapter.log.error(err);
|
|
1998
|
+
}
|
|
1938
1999
|
}
|
|
1939
2000
|
}
|
|
1940
2001
|
|
|
@@ -2005,79 +2066,77 @@ function readEnums(name) {
|
|
|
2005
2066
|
});
|
|
2006
2067
|
}
|
|
2007
2068
|
|
|
2008
|
-
function main() {
|
|
2069
|
+
async function main() {
|
|
2009
2070
|
if (!adapter.config.token) {
|
|
2010
|
-
adapter.log.error('Token is not set!');
|
|
2011
|
-
return;
|
|
2071
|
+
return adapter.log.error('Token is not set!');
|
|
2012
2072
|
}
|
|
2013
2073
|
|
|
2014
2074
|
// if old encoding detected
|
|
2015
2075
|
if (!adapter.config.token.includes(':') || adapter.config.token[0] < '0' || adapter.config.token[0] > '9') {
|
|
2016
2076
|
// decode password and token
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
}));
|
|
2077
|
+
const obj = await adapter.getForeignObjectAsync('system.adapter.' + adapter.namespace);
|
|
2078
|
+
const data = await adapter.getForeignObjectAsync('system.config');
|
|
2079
|
+
let systemSecret;
|
|
2080
|
+
if (data && data.native) {
|
|
2081
|
+
systemSecret = data.native.secret;
|
|
2082
|
+
}
|
|
2083
|
+
systemSecret = systemSecret || DEFAULT_SECRET;
|
|
2084
|
+
if (obj.native.passwordRepeat) {
|
|
2085
|
+
obj.native.password = encrypt(systemSecret, obj.native.passwordRepeat);
|
|
2086
|
+
delete obj.native.passwordRepeat;
|
|
2087
|
+
} else {
|
|
2088
|
+
const password = decrypt('Zgfr56gFe87jJON', obj.native.password || '');
|
|
2089
|
+
obj.native.password = encrypt(systemSecret, password);
|
|
2090
|
+
}
|
|
2091
|
+
obj.native.token = encrypt(systemSecret, obj.native.token);
|
|
2092
|
+
await adapter.setForeignObjectAsync(obj._id, obj);
|
|
2093
|
+
return;
|
|
2035
2094
|
}
|
|
2036
2095
|
|
|
2037
|
-
adapter.
|
|
2096
|
+
await adapter.setStateAsync('info.connection', false, true);
|
|
2038
2097
|
|
|
2039
|
-
adapter.
|
|
2040
|
-
adapter.
|
|
2098
|
+
await adapter.subscribeStatesAsync('communicate.request');
|
|
2099
|
+
await adapter.subscribeStatesAsync('communicate.response');
|
|
2100
|
+
await adapter.subscribeStatesAsync('communicate.responseSilent');
|
|
2041
2101
|
|
|
2042
2102
|
// clear states
|
|
2043
|
-
adapter.
|
|
2044
|
-
adapter.
|
|
2045
|
-
adapter.
|
|
2103
|
+
await adapter.setStateAsync('communicate.request', '', true);
|
|
2104
|
+
await adapter.setStateAsync('communicate.response', '', true);
|
|
2105
|
+
await adapter.setStateAsync('communicate.responseSilent', '', true);
|
|
2106
|
+
await adapter.setStateAsync('communicate.pathFile', '', true);
|
|
2046
2107
|
|
|
2047
2108
|
adapter.config.password = adapter.config.password || '';
|
|
2048
2109
|
adapter.config.keyboard = adapter.config.keyboard || '/cmds';
|
|
2049
2110
|
|
|
2050
|
-
updateUsers(
|
|
2051
|
-
|
|
2052
|
-
|
|
2111
|
+
await updateUsers();
|
|
2112
|
+
if (adapter.config.allowStates !== undefined) {
|
|
2113
|
+
adapter.config.allowStates = true;
|
|
2114
|
+
}
|
|
2115
|
+
adapter.config.answerTimeoutSec = parseInt(adapter.config.answerTimeoutSec, 10) || 60;
|
|
2116
|
+
adapter.config.answerTimeoutSec *= 1000;
|
|
2117
|
+
adapter.config.rememberUsers = adapter.config.rememberUsers === 'true' || adapter.config.rememberUsers === true;
|
|
2118
|
+
|
|
2119
|
+
try {
|
|
2120
|
+
const obj = await adapter.getForeignObjectAsync('system.config');
|
|
2121
|
+
|
|
2122
|
+
if (obj) {
|
|
2123
|
+
systemLang = obj.common.language || 'en';
|
|
2053
2124
|
}
|
|
2054
|
-
adapter.config.answerTimeoutSec = parseInt(adapter.config.answerTimeoutSec, 10) || 60;
|
|
2055
|
-
adapter.config.answerTimeoutSec *= 1000;
|
|
2056
|
-
adapter.config.rememberUsers = adapter.config.rememberUsers === 'true' || adapter.config.rememberUsers === true;
|
|
2057
2125
|
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2126
|
+
await readStatesCommands()
|
|
2127
|
+
if (adapter.config.rooms) {
|
|
2128
|
+
await readEnums();
|
|
2129
|
+
}
|
|
2130
|
+
// init polling every hour
|
|
2131
|
+
reconnectTimer = setInterval(connect, 3600000);
|
|
2063
2132
|
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
})
|
|
2072
|
-
.then(() => {
|
|
2073
|
-
// init polling every hour
|
|
2074
|
-
reconnectTimer = setInterval(connect, 3600000);
|
|
2075
|
-
connect();
|
|
2076
|
-
// detect changes of objects
|
|
2077
|
-
adapter.subscribeForeignObjects('*');
|
|
2078
|
-
});
|
|
2079
|
-
});
|
|
2080
|
-
});
|
|
2133
|
+
connect();
|
|
2134
|
+
|
|
2135
|
+
// detect changes of objects
|
|
2136
|
+
await adapter.subscribeForeignObjectsAsync('*');
|
|
2137
|
+
} catch (err) {
|
|
2138
|
+
adapter.log.error(err);
|
|
2139
|
+
}
|
|
2081
2140
|
}
|
|
2082
2141
|
|
|
2083
2142
|
// If started as allInOne/compact mode => return function to create instance
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "iobroker.telegram",
|
|
3
3
|
"description": "The adapter allows to send and receive telegram messages from ioBroker and to be a broker.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.11.0",
|
|
5
5
|
"author": "bluefox <dogafox@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/iobroker-community-adapters/ioBroker.telegram",
|
|
7
7
|
"repository": {
|