@imessaging/telegram-mtproto 0.3.0 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imessaging/telegram-mtproto",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Telegram MTProto user-account transport for imessaging",
5
5
  "keywords": [
6
6
  "messaging",
@@ -31,7 +31,7 @@
31
31
  "test": "bun test"
32
32
  },
33
33
  "dependencies": {
34
- "@imessaging/core": "0.3.0",
34
+ "@imessaging/core": "0.5.0",
35
35
  "big-integer": "^1.6.52"
36
36
  },
37
37
  "peerDependencies": {
@@ -1,4 +1,6 @@
1
1
  import type {
2
+ ButtonPress,
3
+ ButtonPressTransport,
2
4
  EditableMessage,
3
5
  EditableMessageTransport,
4
6
  EditResult,
@@ -13,6 +15,7 @@ import { isTelegramRecipient, MemoryPeerStore } from "@imessaging/core";
13
15
  import bigInt from "big-integer";
14
16
  import { Api, TelegramClient } from "telegram";
15
17
  import { CustomFile } from "telegram/client/uploads";
18
+ import { CallbackQuery } from "telegram/events/CallbackQuery";
16
19
  import type { TelegramClientParams } from "telegram/client/telegramBaseClient";
17
20
  import { StringSession } from "telegram/sessions";
18
21
 
@@ -54,7 +57,7 @@ function isChannel(chat: Api.TypeChat): chat is Api.Channel {
54
57
  return chat.className === "Channel";
55
58
  }
56
59
 
57
- export class TelegramMtprotoTransport implements EditableMessageTransport {
60
+ export class TelegramMtprotoTransport implements EditableMessageTransport, ButtonPressTransport {
58
61
  readonly id: string;
59
62
  private readonly client: TelegramClient;
60
63
  private readonly peerStore: TelegramPeerStore;
@@ -88,6 +91,37 @@ export class TelegramMtprotoTransport implements EditableMessageTransport {
88
91
  this.connected = true;
89
92
  }
90
93
 
94
+ /**
95
+ * ПРИЁМ НАЖАТИЙ на кнопки — на ТОМ ЖЕ клиенте, что и отправка.
96
+ *
97
+ * Второй клиент здесь был бы ошибкой: у него своя сессия, свой прокси и своя авторизация, а
98
+ * Telegram ограничивает бота за частую повторную авторизацию. К тому же приём по HTTP Bot API
99
+ * требует прямого доступа к `api.telegram.org`, которого в закрытом контуре нет — MTProto уже
100
+ * ходит через настроенный прокси.
101
+ *
102
+ * Подписка ставится независимо от `connect()`: gramjs доставляет события после подключения, а
103
+ * порядок вызовов принадлежит вызывающему.
104
+ */
105
+ onButtonPress(handler: (press: ButtonPress) => void | Promise<void>): () => void {
106
+ const listener = (event: unknown): void => {
107
+ const query = event as {
108
+ data?: Buffer | Uint8Array;
109
+ senderId?: { toJSNumber?: () => number };
110
+ answer?: (options: { message: string; alert: boolean }) => Promise<unknown>;
111
+ };
112
+ const press: ButtonPress = {
113
+ senderId: query.senderId?.toJSNumber?.() ?? null,
114
+ data: query.data === undefined ? "" : Buffer.from(query.data).toString("utf8"),
115
+ answer: async (text: string) => {
116
+ await query.answer?.({ message: text, alert: true });
117
+ },
118
+ };
119
+ void handler(press);
120
+ };
121
+ this.client.addEventHandler(listener, new CallbackQuery({}));
122
+ return () => this.client.removeEventHandler(listener, new CallbackQuery({}));
123
+ }
124
+
91
125
  async disconnect(): Promise<void> {
92
126
  await this.client.disconnect();
93
127
  this.connected = false;