@wabot-dev/framework 0.9.22 → 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.
@@ -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
  }
@@ -2234,8 +2234,11 @@ declare class TelegramChannel implements IChatChannel {
2234
2234
  private config;
2235
2235
  static channelName: "TelegramChannel";
2236
2236
  private bot;
2237
+ private logger;
2237
2238
  constructor(config: TelegramChannelConfig);
2238
2239
  listen(callback: (message: ITelegramChannelMessage) => Promise<void>): void;
2240
+ private extractMedia;
2241
+ private downloadChatFile;
2239
2242
  connect(): void;
2240
2243
  disconnect(): void;
2241
2244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.9.22",
3
+ "version": "0.9.23",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",