iobroker.telegram 6.2.1 → 6.4.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 +6 -12
- package/admin/i18n/de.json +6 -0
- package/admin/i18n/en.json +6 -0
- package/admin/i18n/es.json +6 -0
- package/admin/i18n/fr.json +6 -0
- package/admin/i18n/it.json +6 -0
- package/admin/i18n/nl.json +6 -0
- package/admin/i18n/pl.json +6 -0
- package/admin/i18n/pt.json +6 -0
- package/admin/i18n/ru.json +6 -0
- package/admin/i18n/uk.json +6 -0
- package/admin/i18n/zh-cn.json +6 -0
- package/admin/jsonConfig.json +37 -0
- package/admin/rules/@mf-types.zip +0 -0
- package/build/main.js +192 -14
- package/build/main.js.map +1 -1
- package/build/types.d.ts +6 -0
- package/i18n/de.json +2 -1
- package/i18n/en.json +2 -1
- package/i18n/es.json +2 -1
- package/i18n/fr.json +2 -1
- package/i18n/it.json +2 -1
- package/i18n/nl.json +2 -1
- package/i18n/pl.json +2 -1
- package/i18n/pt.json +2 -1
- package/i18n/ru.json +2 -1
- package/i18n/uk.json +2 -1
- package/i18n/zh-cn.json +2 -1
- package/io-package.json +54 -27
- package/package.json +1 -1
package/build/main.js
CHANGED
|
@@ -47,6 +47,8 @@ const actions = [
|
|
|
47
47
|
* a limit a request lost on the way (e.g. the assistant restarted meanwhile) would leave the user without any reply.
|
|
48
48
|
*/
|
|
49
49
|
const ASSISTANT_TIMEOUT_MS = 120_000;
|
|
50
|
+
/** How long the speech recognition (ioBroker.ai-parakeet) may take, including loading the model after idle. */
|
|
51
|
+
const SPEECH_TO_TEXT_TIMEOUT_MS = 90_000;
|
|
50
52
|
/** Rejected by `sendToIfAlive` when the instance did not answer in time. */
|
|
51
53
|
class SendToTimeoutError extends Error {
|
|
52
54
|
}
|
|
@@ -63,6 +65,10 @@ const ASSISTANT_IMAGE_EDGE = 1600;
|
|
|
63
65
|
* 8.0+, else in its file storage; older ones are deleted.
|
|
64
66
|
*/
|
|
65
67
|
const ASSISTANT_VIDEOS_KEPT = 20;
|
|
68
|
+
/** Folders of received files that `deleteFilesAfterDays` cleans up (`assistant/` keeps its newest videos itself). */
|
|
69
|
+
const RECEIVED_FILE_FOLDERS = ['voice', 'photo', 'video', 'audio', 'document'];
|
|
70
|
+
/** How often old received files are looked for. */
|
|
71
|
+
const DELETE_OLD_FILES_INTERVAL_MS = 60 * 60 * 1000;
|
|
66
72
|
/**
|
|
67
73
|
* The size of a photo that goes to the assistant: the largest one up to {@link ASSISTANT_IMAGE_EDGE}, else the
|
|
68
74
|
* smallest. Enough to see what is on it, and far below the limit of the models.
|
|
@@ -194,6 +200,7 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
194
200
|
tmpDirName = '';
|
|
195
201
|
questions = [];
|
|
196
202
|
garbageCollectorInterval;
|
|
203
|
+
deleteOldFilesInterval;
|
|
197
204
|
isServer = false;
|
|
198
205
|
/**
|
|
199
206
|
* In-memory queue of outgoing sends that failed because telegram was unreachable. They are resent (FIFO)
|
|
@@ -357,7 +364,7 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
357
364
|
*/
|
|
358
365
|
async migrateInstanceIds() {
|
|
359
366
|
const migrated = {};
|
|
360
|
-
for (const key of ['text2command', 'assistantInstance']) {
|
|
367
|
+
for (const key of ['text2command', 'assistantInstance', 'speechToText']) {
|
|
361
368
|
const value = this.config[key];
|
|
362
369
|
if (typeof value === 'string' && value.startsWith('system.adapter.')) {
|
|
363
370
|
const shortId = value.substring('system.adapter.'.length);
|
|
@@ -419,6 +426,10 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
419
426
|
this.log.error(`Cannot create tmp directory: ${this.tmpDirName}: ${err}`);
|
|
420
427
|
}
|
|
421
428
|
}
|
|
429
|
+
if (this.deleteFilesAfterDays() > 0) {
|
|
430
|
+
void this.deleteOldFiles();
|
|
431
|
+
this.deleteOldFilesInterval = this.setInterval(() => void this.deleteOldFiles(), DELETE_OLD_FILES_INTERVAL_MS);
|
|
432
|
+
}
|
|
422
433
|
if (this.isServer) {
|
|
423
434
|
this.config.port = parseInt(String(this.config.port), 10);
|
|
424
435
|
// Load certificates
|
|
@@ -496,6 +507,10 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
496
507
|
this.clearInterval(this.garbageCollectorInterval);
|
|
497
508
|
this.garbageCollectorInterval = undefined;
|
|
498
509
|
}
|
|
510
|
+
if (this.deleteOldFilesInterval) {
|
|
511
|
+
this.clearInterval(this.deleteOldFilesInterval);
|
|
512
|
+
this.deleteOldFilesInterval = undefined;
|
|
513
|
+
}
|
|
499
514
|
if (this.pollingRestartTimer) {
|
|
500
515
|
this.clearTimeout(this.pollingRestartTimer);
|
|
501
516
|
this.pollingRestartTimer = undefined;
|
|
@@ -1706,33 +1721,108 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
1706
1721
|
}).catch(err => this.log.error(`send Message Error: ${err}`));
|
|
1707
1722
|
}
|
|
1708
1723
|
/**
|
|
1709
|
-
*
|
|
1710
|
-
*
|
|
1711
|
-
*
|
|
1724
|
+
* The checks of the text pipeline for a voice message, done before the recording is recognised: an old message
|
|
1725
|
+
* is dropped, and a user that is not allowed gets the same answer as for a text message. Without it anyone could
|
|
1726
|
+
* use the speech recognition (or talk to the assistant) just by sending a voice message.
|
|
1712
1727
|
*
|
|
1713
1728
|
* @param msg the voice message
|
|
1714
|
-
* @
|
|
1729
|
+
* @returns whether the voice message may be processed
|
|
1715
1730
|
*/
|
|
1716
|
-
async
|
|
1717
|
-
const bot = this.bot;
|
|
1731
|
+
async acceptVoiceMessage(msg) {
|
|
1718
1732
|
const from = msg.from;
|
|
1719
|
-
const user = !this.config.useUsername ? from.first_name : !from.username ? from.first_name : from.username;
|
|
1720
1733
|
const pollingInterval = parseInt(String(this.config.pollingInterval ?? 0), 10) || 0;
|
|
1721
1734
|
if (Date.now() - msg.date * 1000 > pollingInterval + 30000) {
|
|
1722
1735
|
this.log.warn(`Voice message from ${from.first_name} ignored, because too old`);
|
|
1723
|
-
return;
|
|
1736
|
+
return false;
|
|
1724
1737
|
}
|
|
1725
1738
|
if ((this.config.password || this.config.doNotAcceptNewUsers) && !this.storedUsers[from.id]) {
|
|
1726
|
-
await bot.api
|
|
1727
|
-
.sendMessage({
|
|
1739
|
+
await this.bot.api.sendMessage({
|
|
1728
1740
|
chat_id: from.id,
|
|
1729
1741
|
text: adapter_core_1.I18n.translate(this.config.doNotAcceptNewUsers
|
|
1730
1742
|
? 'User is not in the list'
|
|
1731
1743
|
: 'Please enter password in form "/password phrase"'),
|
|
1744
|
+
}).catch(error => this.log.error(`send Message Error: ${error}`));
|
|
1745
|
+
return false;
|
|
1746
|
+
}
|
|
1747
|
+
return true;
|
|
1748
|
+
}
|
|
1749
|
+
/** How long the speech recognition may take (a field so that tests can shorten it). */
|
|
1750
|
+
speechToTextTimeoutMs = SPEECH_TO_TEXT_TIMEOUT_MS;
|
|
1751
|
+
/**
|
|
1752
|
+
* Recognise a voice message with the speech recognition instance (ioBroker.ai-parakeet, `transcribe`) and process
|
|
1753
|
+
* the recognised text like a text message: `communicate.request`, the commands of this adapter, text2command and
|
|
1754
|
+
* the assistant all get it.
|
|
1755
|
+
*
|
|
1756
|
+
* @param msg the voice message
|
|
1757
|
+
* @param download the running download of the recording (OGG/Opus)
|
|
1758
|
+
*/
|
|
1759
|
+
async transcribeVoice(msg, download) {
|
|
1760
|
+
const bot = this.bot;
|
|
1761
|
+
const instance = this.config.speechToText;
|
|
1762
|
+
if (!(await this.acceptVoiceMessage(msg))) {
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
const content = await download;
|
|
1766
|
+
const options = msg.is_topic_message ? { message_thread_id: msg.message_thread_id } : undefined;
|
|
1767
|
+
bot.api.sendChatAction({ ...options, chat_id: msg.chat.id, action: 'typing' }).catch(() => { });
|
|
1768
|
+
// the names of the commands help the recognition to write them as configured (e.g. "ioBroker")
|
|
1769
|
+
const prompt = Object.values(this.commands)
|
|
1770
|
+
.map(command => command.alias)
|
|
1771
|
+
.filter(alias => typeof alias === 'string' && alias)
|
|
1772
|
+
.join(', ');
|
|
1773
|
+
this.log.debug(`Recognise voice message (${content.length} bytes) with ${instance}`);
|
|
1774
|
+
let response;
|
|
1775
|
+
try {
|
|
1776
|
+
response = (await this.sendToIfAlive(instance, 'transcribe', { audio: content.toString('base64'), ...(prompt ? { prompt } : {}) }, this.speechToTextTimeoutMs));
|
|
1777
|
+
}
|
|
1778
|
+
catch (error) {
|
|
1779
|
+
this.log.warn(`Cannot recognise voice message with ${instance}: ${error instanceof Error ? error.message : String(error)}`);
|
|
1780
|
+
response = { error: String(error) };
|
|
1781
|
+
}
|
|
1782
|
+
if (!response) {
|
|
1783
|
+
// the instance is not running: already logged
|
|
1784
|
+
return;
|
|
1785
|
+
}
|
|
1786
|
+
// the model writes a full stop at the end, which would break e.g. "<alias> on"
|
|
1787
|
+
const text = (response.text || '').trim().replace(/[.!]+$/, '');
|
|
1788
|
+
if (response.error || !text) {
|
|
1789
|
+
if (response.error) {
|
|
1790
|
+
this.log.warn(`${instance} cannot recognise the voice message: ${response.error}`);
|
|
1791
|
+
}
|
|
1792
|
+
await bot.api
|
|
1793
|
+
.sendMessage({
|
|
1794
|
+
...options,
|
|
1795
|
+
chat_id: msg.chat.id,
|
|
1796
|
+
text: adapter_core_1.I18n.translate('The voice message could not be recognised.'),
|
|
1732
1797
|
})
|
|
1733
1798
|
.catch(error => this.log.error(`send Message Error: ${error}`));
|
|
1734
1799
|
return;
|
|
1735
1800
|
}
|
|
1801
|
+
this.log.debug(`Recognised voice message: ${text}`);
|
|
1802
|
+
this.setStateSafe('communicate.requestVoiceText', { val: text, ack: true });
|
|
1803
|
+
if (this.config.speechToTextEcho !== false) {
|
|
1804
|
+
// show what was understood, so a wrong answer can be told apart from a misheard command
|
|
1805
|
+
await bot.api
|
|
1806
|
+
.sendMessage({ ...options, chat_id: msg.chat.id, text: `🎤 ${text}` })
|
|
1807
|
+
.catch(error => this.log.error(`send Message Error: ${error}`));
|
|
1808
|
+
}
|
|
1809
|
+
await this.processTelegramText({ ...msg, text });
|
|
1810
|
+
}
|
|
1811
|
+
/**
|
|
1812
|
+
* Forward a voice message to the ioBroker.assistant instance: it recognises the speech, answers like a text
|
|
1813
|
+
* question (same `source`, so the conversation continues) and returns what it heard. The same user check as for
|
|
1814
|
+
* text messages applies — without it anyone could talk to the assistant just by sending a voice message.
|
|
1815
|
+
*
|
|
1816
|
+
* @param msg the voice message
|
|
1817
|
+
* @param download the running download of the recording (OGG/Opus)
|
|
1818
|
+
*/
|
|
1819
|
+
async askAssistantByVoice(msg, download) {
|
|
1820
|
+
const bot = this.bot;
|
|
1821
|
+
const from = msg.from;
|
|
1822
|
+
const user = !this.config.useUsername ? from.first_name : !from.username ? from.first_name : from.username;
|
|
1823
|
+
if (!(await this.acceptVoiceMessage(msg))) {
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1736
1826
|
const content = await download;
|
|
1737
1827
|
const options = msg.is_topic_message ? { message_thread_id: msg.message_thread_id } : undefined;
|
|
1738
1828
|
// recognition + answer take a few seconds: show that something is happening
|
|
@@ -2010,6 +2100,90 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
2010
2100
|
this.log.debug(`Cannot delete old videos of the assistant: ${error}`);
|
|
2011
2101
|
}
|
|
2012
2102
|
}
|
|
2103
|
+
/** After how many days received files are deleted; 0 = never */
|
|
2104
|
+
deleteFilesAfterDays() {
|
|
2105
|
+
const days = Number(this.config.deleteFilesAfterDays);
|
|
2106
|
+
return Number.isFinite(days) && days > 0 ? days : 0;
|
|
2107
|
+
}
|
|
2108
|
+
/**
|
|
2109
|
+
* Delete received files (voice, photo, video, audio, document) that are older than `deleteFilesAfterDays`, where
|
|
2110
|
+
* `saveFilesTo` stores them: in the file system or in the file storage of the instance.
|
|
2111
|
+
*
|
|
2112
|
+
* @param now the current time in ms
|
|
2113
|
+
* @returns how many files were deleted
|
|
2114
|
+
*/
|
|
2115
|
+
async deleteOldFiles(now = Date.now()) {
|
|
2116
|
+
const days = this.deleteFilesAfterDays();
|
|
2117
|
+
if (!days) {
|
|
2118
|
+
return 0;
|
|
2119
|
+
}
|
|
2120
|
+
const limit = now - days * 24 * 60 * 60 * 1000;
|
|
2121
|
+
let deleted = 0;
|
|
2122
|
+
let failed = 0;
|
|
2123
|
+
let lastError;
|
|
2124
|
+
for (const folder of RECEIVED_FILE_FOLDERS) {
|
|
2125
|
+
for (const file of await this.receivedFiles(folder)) {
|
|
2126
|
+
// a file without a known time is kept
|
|
2127
|
+
if (file.mtime === undefined || file.mtime >= limit) {
|
|
2128
|
+
continue;
|
|
2129
|
+
}
|
|
2130
|
+
try {
|
|
2131
|
+
await file.delete();
|
|
2132
|
+
deleted++;
|
|
2133
|
+
}
|
|
2134
|
+
catch (error) {
|
|
2135
|
+
failed++;
|
|
2136
|
+
lastError = error;
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
if (deleted) {
|
|
2141
|
+
this.log.info(`Deleted ${deleted} received file(s) older than ${days} day(s)`);
|
|
2142
|
+
}
|
|
2143
|
+
if (failed) {
|
|
2144
|
+
this.log.warn(`Cannot delete ${failed} received file(s) older than ${days} day(s): ${String(lastError)}`);
|
|
2145
|
+
}
|
|
2146
|
+
return deleted;
|
|
2147
|
+
}
|
|
2148
|
+
/**
|
|
2149
|
+
* The files in one folder of received files, with their modification time
|
|
2150
|
+
*
|
|
2151
|
+
* @param folder e.g. `video`
|
|
2152
|
+
*/
|
|
2153
|
+
async receivedFiles(folder) {
|
|
2154
|
+
try {
|
|
2155
|
+
if (this.config.saveFilesTo === 'filesystem') {
|
|
2156
|
+
const dir = (0, node_path_1.join)(this.tmpDirName, folder);
|
|
2157
|
+
if (!(0, node_fs_1.existsSync)(dir)) {
|
|
2158
|
+
return [];
|
|
2159
|
+
}
|
|
2160
|
+
const files = [];
|
|
2161
|
+
for (const name of await (0, promises_1.readdir)(dir)) {
|
|
2162
|
+
const file = (0, node_path_1.join)(dir, name);
|
|
2163
|
+
const stat = await (0, promises_1.lstat)(file);
|
|
2164
|
+
// only files, no folders or links
|
|
2165
|
+
if (stat.isFile()) {
|
|
2166
|
+
files.push({ mtime: stat.mtimeMs, delete: () => (0, promises_1.unlink)(file) });
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
return files;
|
|
2170
|
+
}
|
|
2171
|
+
if (this.config.saveFilesTo === 'iobroker') {
|
|
2172
|
+
const entries = await this.readDirAsync(this.namespace, `/${folder}`);
|
|
2173
|
+
return entries
|
|
2174
|
+
.filter(entry => !entry.isDir)
|
|
2175
|
+
.map(entry => ({
|
|
2176
|
+
mtime: entry.modifiedAt ?? entry.createdAt,
|
|
2177
|
+
delete: () => this.delFileAsync(this.namespace, `/${folder}/${entry.file}`),
|
|
2178
|
+
}));
|
|
2179
|
+
}
|
|
2180
|
+
}
|
|
2181
|
+
catch (error) {
|
|
2182
|
+
// e.g. the folder does not exist in the file storage
|
|
2183
|
+
this.log.debug(`Cannot list the received files in ${folder}: ${String(error)}`);
|
|
2184
|
+
}
|
|
2185
|
+
return [];
|
|
2186
|
+
}
|
|
2013
2187
|
/**
|
|
2014
2188
|
* Store a downloaded file where `saveFilesTo` says (file system or ioBroker file storage).
|
|
2015
2189
|
*
|
|
@@ -2090,10 +2264,14 @@ class Telegram extends adapter_core_1.Adapter {
|
|
|
2090
2264
|
this.queueAssistantMedia(msg);
|
|
2091
2265
|
}
|
|
2092
2266
|
if (msg.voice) {
|
|
2093
|
-
// Downloaded once: stored as before (communicate.pathFile) and
|
|
2094
|
-
// and
|
|
2267
|
+
// Downloaded once: stored as before (communicate.pathFile) and recognised: with a speech recognition
|
|
2268
|
+
// instance here, and the text goes on like a text message; else with an assistant instance there.
|
|
2269
|
+
// A voice message with a caption is answered by its caption (text pipeline) instead.
|
|
2095
2270
|
const download = this.downloadFile(msg.voice.file_id);
|
|
2096
|
-
if (this.config.
|
|
2271
|
+
if (this.config.speechToText && msg.from && !msg.caption) {
|
|
2272
|
+
this.transcribeVoice(msg, download).catch(err => this.log.error(`Cannot recognise voice message with ${this.config.speechToText}: ${err}`));
|
|
2273
|
+
}
|
|
2274
|
+
else if (this.config.assistantInstance && msg.from && !msg.caption) {
|
|
2097
2275
|
this.askAssistantByVoice(msg, download).catch(err => this.log.error(`Cannot send voice message to ${this.config.assistantInstance}: ${err}`));
|
|
2098
2276
|
}
|
|
2099
2277
|
download
|