@wabot-dev/framework 0.9.21 → 0.9.23
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/dist/src/addon/chat-controller/cmd/CmdChannel.js +4 -0
- package/dist/src/addon/chat-controller/cmd/CmdChannelServer.js +17 -0
- package/dist/src/addon/chat-controller/cmd/runCmdClient.js +15 -1
- package/dist/src/addon/chat-controller/telegram/TelegramChannel.js +43 -1
- package/dist/src/index.d.ts +9 -0
- package/package.json +1 -1
|
@@ -151,6 +151,23 @@ let CmdChannelServer = class CmdChannelServer {
|
|
|
151
151
|
});
|
|
152
152
|
return;
|
|
153
153
|
}
|
|
154
|
+
case 'clear': {
|
|
155
|
+
if (!this.activeRoute) {
|
|
156
|
+
this.sendToClient({ type: 'error', message: 'no channel selected' });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const handlers = this.channels.get(this.activeRoute);
|
|
160
|
+
if (!handlers) {
|
|
161
|
+
this.sendToClient({ type: 'error', message: 'channel no longer available' });
|
|
162
|
+
this.activeRoute = null;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (handlers.onClear) {
|
|
166
|
+
await handlers.onClear();
|
|
167
|
+
}
|
|
168
|
+
this.sendToClient({ type: 'cleared', route: this.activeRoute });
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
154
171
|
}
|
|
155
172
|
}
|
|
156
173
|
sendToClient(msg) {
|
|
@@ -11,10 +11,11 @@ const green = ansi('1;32');
|
|
|
11
11
|
const greenText = ansi('32');
|
|
12
12
|
const red = ansi('1;31');
|
|
13
13
|
const yellow = ansi('33');
|
|
14
|
-
const COMMANDS = ['/channels', '/help', '/exit'];
|
|
14
|
+
const COMMANDS = ['/channels', '/clear', '/help', '/exit'];
|
|
15
15
|
const HELP_LINES = [
|
|
16
16
|
'Commands:',
|
|
17
17
|
' /channels list channels and switch',
|
|
18
|
+
' /clear start a fresh conversation on the current channel',
|
|
18
19
|
' /help show this help',
|
|
19
20
|
' /exit quit',
|
|
20
21
|
];
|
|
@@ -96,6 +97,10 @@ function runCmdClient() {
|
|
|
96
97
|
process.stdout.write(`\n${green(`[${msg.senderName ?? 'bot'}]:`)} ${greenText(msg.text)}\n\n`);
|
|
97
98
|
rl.prompt();
|
|
98
99
|
return;
|
|
100
|
+
case 'cleared':
|
|
101
|
+
process.stdout.write(green(`[conversation cleared on ${msg.route}]`) + '\n');
|
|
102
|
+
rl.prompt();
|
|
103
|
+
return;
|
|
99
104
|
case 'error':
|
|
100
105
|
process.stderr.write(red('error: ') + red(msg.message) + '\n');
|
|
101
106
|
rl.prompt();
|
|
@@ -202,6 +207,15 @@ function runCmdClient() {
|
|
|
202
207
|
send({ type: 'hello' });
|
|
203
208
|
return;
|
|
204
209
|
}
|
|
210
|
+
if (trimmed === '/clear') {
|
|
211
|
+
if (state !== 'chatting') {
|
|
212
|
+
process.stderr.write(red('select a channel before clearing the conversation.') + '\n');
|
|
213
|
+
rl.prompt();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
send({ type: 'clear' });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
205
219
|
if (state === 'disconnected') {
|
|
206
220
|
process.stderr.write(red('not connected to framework — waiting for server...') + '\n');
|
|
207
221
|
rl.prompt();
|
|
@@ -2,6 +2,7 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import { Bot } from 'grammy';
|
|
3
3
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
4
4
|
import { injectable } from '../../../core/injection/index.js';
|
|
5
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
5
6
|
import { markdownToTelegramHtml } from './markdownToTelegramHtml.js';
|
|
6
7
|
import { telegramChannelName } from './telegramChannelName.js';
|
|
7
8
|
|
|
@@ -11,6 +12,7 @@ let TelegramChannel = class TelegramChannel {
|
|
|
11
12
|
config;
|
|
12
13
|
static channelName = telegramChannelName;
|
|
13
14
|
bot;
|
|
15
|
+
logger = new Logger('wabot:telegram-channel');
|
|
14
16
|
constructor(config) {
|
|
15
17
|
this.config = config;
|
|
16
18
|
this.bot = new Bot(this.config.botToken);
|
|
@@ -25,13 +27,16 @@ let TelegramChannel = class TelegramChannel {
|
|
|
25
27
|
chatType: ctx.message.chat.type === 'private' ? 'PRIVATE' : 'GROUP',
|
|
26
28
|
channelName: TelegramChannel_1.channelName,
|
|
27
29
|
};
|
|
30
|
+
const { images, documents } = await this.extractMedia(ctx.api, ctx.message);
|
|
28
31
|
await callback({
|
|
29
32
|
channel: telegramChannelName,
|
|
30
33
|
chatConnection,
|
|
31
34
|
message: {
|
|
32
35
|
senderId: ctx.from.id.toString(),
|
|
33
36
|
senderName: ctx.from.first_name,
|
|
34
|
-
text: ctx.message.text,
|
|
37
|
+
text: ctx.message.text ?? ctx.message.caption,
|
|
38
|
+
images: images.length > 0 ? images : undefined,
|
|
39
|
+
documents: documents.length > 0 ? documents : undefined,
|
|
35
40
|
},
|
|
36
41
|
reply: async (replyMessage) => {
|
|
37
42
|
if (!replyMessage.text)
|
|
@@ -43,6 +48,43 @@ let TelegramChannel = class TelegramChannel {
|
|
|
43
48
|
});
|
|
44
49
|
});
|
|
45
50
|
}
|
|
51
|
+
async extractMedia(api, message) {
|
|
52
|
+
const images = [];
|
|
53
|
+
const documents = [];
|
|
54
|
+
if (message.photo && message.photo.length > 0) {
|
|
55
|
+
// Photos arrive in multiple sizes; the last entry is the highest resolution.
|
|
56
|
+
const largest = message.photo[message.photo.length - 1];
|
|
57
|
+
const file = await this.downloadChatFile(api, largest.file_id, largest.file_unique_id, 'image/jpeg');
|
|
58
|
+
if (file)
|
|
59
|
+
images.push(file);
|
|
60
|
+
}
|
|
61
|
+
if (message.document) {
|
|
62
|
+
const doc = message.document;
|
|
63
|
+
const mimeType = doc.mime_type ?? 'application/octet-stream';
|
|
64
|
+
const file = await this.downloadChatFile(api, doc.file_id, doc.file_unique_id, mimeType, doc.file_name);
|
|
65
|
+
// An image sent as an uncompressed file still belongs with the images.
|
|
66
|
+
if (file)
|
|
67
|
+
(mimeType.startsWith('image/') ? images : documents).push(file);
|
|
68
|
+
}
|
|
69
|
+
return { images, documents };
|
|
70
|
+
}
|
|
71
|
+
async downloadChatFile(api, fileId, id, mimeType, name) {
|
|
72
|
+
try {
|
|
73
|
+
const file = await api.getFile(fileId);
|
|
74
|
+
if (!file.file_path)
|
|
75
|
+
return null;
|
|
76
|
+
const url = `https://api.telegram.org/file/bot${this.config.botToken}/${file.file_path}`;
|
|
77
|
+
const res = await fetch(url);
|
|
78
|
+
if (!res.ok)
|
|
79
|
+
throw new Error(`${res.status} ${res.statusText}`);
|
|
80
|
+
const base64 = Buffer.from(await res.arrayBuffer()).toString('base64');
|
|
81
|
+
return { id, name, mimeType, base64Url: `data:${mimeType};base64,${base64}` };
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
this.logger.warn(`failed to download telegram file '${id}'`, err instanceof Error ? { message: err.message } : { err });
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
46
88
|
connect() {
|
|
47
89
|
this.bot.start();
|
|
48
90
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -2061,6 +2061,7 @@ interface ICmdChannelHandlers {
|
|
|
2061
2061
|
senderName?: string;
|
|
2062
2062
|
text: string;
|
|
2063
2063
|
}) => void) => Promise<void> | void;
|
|
2064
|
+
onClear?: () => Promise<void> | void;
|
|
2064
2065
|
}
|
|
2065
2066
|
declare class CmdChannelServer {
|
|
2066
2067
|
private server;
|
|
@@ -2123,6 +2124,8 @@ type CmdClientMessage = {
|
|
|
2123
2124
|
} | {
|
|
2124
2125
|
type: 'message';
|
|
2125
2126
|
text: string;
|
|
2127
|
+
} | {
|
|
2128
|
+
type: 'clear';
|
|
2126
2129
|
};
|
|
2127
2130
|
type CmdServerMessage = {
|
|
2128
2131
|
type: 'channels';
|
|
@@ -2134,6 +2137,9 @@ type CmdServerMessage = {
|
|
|
2134
2137
|
type: 'reply';
|
|
2135
2138
|
senderName?: string;
|
|
2136
2139
|
text: string;
|
|
2140
|
+
} | {
|
|
2141
|
+
type: 'cleared';
|
|
2142
|
+
route: string;
|
|
2137
2143
|
} | {
|
|
2138
2144
|
type: 'error';
|
|
2139
2145
|
message: string;
|
|
@@ -2228,8 +2234,11 @@ declare class TelegramChannel implements IChatChannel {
|
|
|
2228
2234
|
private config;
|
|
2229
2235
|
static channelName: "TelegramChannel";
|
|
2230
2236
|
private bot;
|
|
2237
|
+
private logger;
|
|
2231
2238
|
constructor(config: TelegramChannelConfig);
|
|
2232
2239
|
listen(callback: (message: ITelegramChannelMessage) => Promise<void>): void;
|
|
2240
|
+
private extractMedia;
|
|
2241
|
+
private downloadChatFile;
|
|
2233
2242
|
connect(): void;
|
|
2234
2243
|
disconnect(): void;
|
|
2235
2244
|
}
|