@adminforth/chat-surface-adapter-telegram 1.1.0 → 1.1.1

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type AdminUser, type ChatSurfaceAdapter, type ChatSurfaceEventSink, type ChatSurfaceIncomingMessage, type ChatSurfaceRequestContext, type IAdminForth } from "adminforth";
1
+ import { type ChatSurfaceAdapter, type ChatSurfaceEventSink, type ChatSurfaceIncomingMessage, type ChatSurfaceRequestContext } from "adminforth";
2
2
  import { AdapterOptions } from "./types.js";
3
3
  export type { AdapterOptions, TelegramStreamingMode } from "./types.js";
4
4
  export { getFinalMessageStreamPreview, renderFinalMessageImages, renderHtmlBlockToPng, renderTablePng, renderVegaLitePng, type RenderedMessage, type RenderedMessageImage, type RenderTableColumn, type RenderTablePngInput, type VegaLiteSpec, } from "./renderers.js";
@@ -17,9 +17,17 @@ type TelegramUpdate = {
17
17
  };
18
18
  };
19
19
  };
20
+ type ChatSurfaceConnectAction = {
21
+ type: "url";
22
+ label: string;
23
+ url: string;
24
+ };
20
25
  export declare class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
21
26
  private options;
22
27
  name: string;
28
+ createConnectAction?: (input: {
29
+ token: string;
30
+ }) => ChatSurfaceConnectAction;
23
31
  constructor(options: AdapterOptions);
24
32
  validate(): void;
25
33
  parseIncomingMessage(ctx: ChatSurfaceRequestContext): Promise<{
@@ -29,14 +37,11 @@ export declare class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
29
37
  externalUserId: string;
30
38
  userTimeZone: string;
31
39
  metadata: {
40
+ startPayload: string | null;
32
41
  telegramUpdate: TelegramUpdate;
33
42
  };
34
43
  } | null>;
35
44
  createEventSink(ctx: ChatSurfaceRequestContext, incoming: ChatSurfaceIncomingMessage): ChatSurfaceEventSink;
36
- resolveAdminUser(input: {
37
- adminforth: IAdminForth;
38
- incoming: ChatSurfaceIncomingMessage;
39
- }): Promise<AdminUser | null>;
40
45
  private sendMessage;
41
46
  private sendFinalMessage;
42
47
  private sendPhoto;
package/dist/index.js CHANGED
@@ -7,7 +7,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { AdminForthFilterOperators, } from "adminforth";
11
10
  import { getFinalMessageStreamPreview, renderFinalMessageImages } from "./renderers.js";
12
11
  import { randomInt } from "node:crypto";
13
12
  export { getFinalMessageStreamPreview, renderFinalMessageImages, renderHtmlBlockToPng, renderTablePng, renderVegaLitePng, } from "./renderers.js";
@@ -17,8 +16,8 @@ const TELEGRAM_MESSAGE_MAX_LENGTH = 4096;
17
16
  const TELEGRAM_DRAFT_MAX_LENGTH = 4096;
18
17
  const DEFAULT_DRAFT_UPDATE_INTERVAL_MS = 650;
19
18
  const DEFAULT_TYPING_INTERVAL_MS = 4000;
20
- const DEFAULT_ADMIN_USER_RESOURCE_ID = "adminuser";
21
- const DEFAULT_ADMIN_USER_TELEGRAM_ID_FIELD = "telegramId";
19
+ const TELEGRAM_START_COMMAND_PREFIX = "/start";
20
+ const TELEGRAM_COMMAND_PARTS_RE = /\s+/;
22
21
  function createTelegramDraftId() {
23
22
  return randomInt(1, 2147483647);
24
23
  }
@@ -46,10 +45,24 @@ function splitTelegramMessage(text) {
46
45
  }
47
46
  return chunks;
48
47
  }
48
+ function parseTelegramStartPayload(text) {
49
+ const [command, ...payloadParts] = text.trim().split(TELEGRAM_COMMAND_PARTS_RE);
50
+ if (command !== TELEGRAM_START_COMMAND_PREFIX && !command.startsWith(`${TELEGRAM_START_COMMAND_PREFIX}@`)) {
51
+ return null;
52
+ }
53
+ return payloadParts.join(" ") || null;
54
+ }
49
55
  export class TelegramChatSurfaceAdapter {
50
56
  constructor(options) {
51
57
  this.options = options;
52
58
  this.name = "telegram";
59
+ if (options.botUsername) {
60
+ this.createConnectAction = ({ token }) => ({
61
+ type: "url",
62
+ label: "Connect Telegram",
63
+ url: `https://t.me/${options.botUsername}?start=${encodeURIComponent(token)}`,
64
+ });
65
+ }
53
66
  }
54
67
  validate() {
55
68
  if (!this.options.botToken) {
@@ -70,6 +83,7 @@ export class TelegramChatSurfaceAdapter {
70
83
  if (!text || chatId === undefined || userId === undefined) {
71
84
  return null;
72
85
  }
86
+ const startPayload = parseTelegramStartPayload(text);
73
87
  return {
74
88
  surface: this.name,
75
89
  prompt: text,
@@ -77,6 +91,7 @@ export class TelegramChatSurfaceAdapter {
77
91
  externalUserId: String(userId),
78
92
  userTimeZone: "UTC",
79
93
  metadata: {
94
+ startPayload,
80
95
  telegramUpdate: update,
81
96
  },
82
97
  };
@@ -181,28 +196,6 @@ export class TelegramChatSurfaceAdapter {
181
196
  }),
182
197
  };
183
198
  }
184
- resolveAdminUser(input) {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- var _a, _b;
187
- const adminUserResourceId = (_a = this.options.adminUserResourceId) !== null && _a !== void 0 ? _a : DEFAULT_ADMIN_USER_RESOURCE_ID;
188
- const telegramIdField = (_b = this.options.adminUserTelegramIdField) !== null && _b !== void 0 ? _b : DEFAULT_ADMIN_USER_TELEGRAM_ID_FIELD;
189
- const adminUser = yield input.adminforth.resource(adminUserResourceId).get([
190
- {
191
- field: telegramIdField,
192
- operator: AdminForthFilterOperators.EQ,
193
- value: input.incoming.externalUserId,
194
- },
195
- ]);
196
- if (!adminUser) {
197
- return null;
198
- }
199
- return {
200
- pk: adminUser.id,
201
- username: adminUser[input.adminforth.config.auth.usernameField],
202
- dbUser: adminUser,
203
- };
204
- });
205
- }
206
199
  sendMessage(chatId, text) {
207
200
  return __awaiter(this, void 0, void 0, function* () {
208
201
  if (!text) {
package/dist/types.d.ts CHANGED
@@ -4,6 +4,10 @@ export type AdapterOptions = {
4
4
  * Telegram bot token from BotFather.
5
5
  */
6
6
  botToken: string;
7
+ /**
8
+ * Telegram bot username used to build the AdminForth account-link URL.
9
+ */
10
+ botUsername?: string;
7
11
  /**
8
12
  * Optional secret token configured in Telegram setWebhook.
9
13
  */
@@ -18,14 +22,4 @@ export type AdapterOptions = {
18
22
  * Default is 650ms.
19
23
  */
20
24
  draftUpdateIntervalMs?: number;
21
- /**
22
- * AdminForth admin user field that stores Telegram user id.
23
- * Default is `telegramId`.
24
- */
25
- adminUserTelegramIdField?: string;
26
- /**
27
- * AdminForth admin users resource id.
28
- * Default is `adminuser`.
29
- */
30
- adminUserResourceId?: string;
31
25
  };
package/index.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  import {
2
- AdminForthFilterOperators,
3
- type AdminUser,
4
2
  type ChatSurfaceAdapter,
5
3
  type ChatSurfaceEventSink,
6
4
  type ChatSurfaceIncomingMessage,
7
5
  type ChatSurfaceRequestContext,
8
- type IAdminForth,
9
6
  } from "adminforth";
10
7
  import { AdapterOptions } from "./types.js";
11
8
  import { getFinalMessageStreamPreview, renderFinalMessageImages } from "./renderers.js";
@@ -40,14 +37,20 @@ type TelegramUpdate = {
40
37
  };
41
38
  };
42
39
 
40
+ type ChatSurfaceConnectAction = {
41
+ type: "url";
42
+ label: string;
43
+ url: string;
44
+ };
45
+
43
46
  const TELEGRAM_API_BASE_URL = "https://api.telegram.org";
44
47
  const TELEGRAM_SECRET_HEADER = "x-telegram-bot-api-secret-token";
45
48
  const TELEGRAM_MESSAGE_MAX_LENGTH = 4096;
46
49
  const TELEGRAM_DRAFT_MAX_LENGTH = 4096;
47
50
  const DEFAULT_DRAFT_UPDATE_INTERVAL_MS = 650;
48
51
  const DEFAULT_TYPING_INTERVAL_MS = 4000;
49
- const DEFAULT_ADMIN_USER_RESOURCE_ID = "adminuser";
50
- const DEFAULT_ADMIN_USER_TELEGRAM_ID_FIELD = "telegramId";
52
+ const TELEGRAM_START_COMMAND_PREFIX = "/start";
53
+ const TELEGRAM_COMMAND_PARTS_RE = /\s+/;
51
54
 
52
55
  function createTelegramDraftId() {
53
56
  return randomInt(1, 2147483647);
@@ -90,10 +93,29 @@ function splitTelegramMessage(text: string) {
90
93
  return chunks;
91
94
  }
92
95
 
96
+ function parseTelegramStartPayload(text: string) {
97
+ const [command, ...payloadParts] = text.trim().split(TELEGRAM_COMMAND_PARTS_RE);
98
+
99
+ if (command !== TELEGRAM_START_COMMAND_PREFIX && !command.startsWith(`${TELEGRAM_START_COMMAND_PREFIX}@`)) {
100
+ return null;
101
+ }
102
+
103
+ return payloadParts.join(" ") || null;
104
+ }
105
+
93
106
  export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
94
107
  name = "telegram";
95
-
96
- constructor(private options: AdapterOptions) {}
108
+ createConnectAction?: (input: { token: string }) => ChatSurfaceConnectAction;
109
+
110
+ constructor(private options: AdapterOptions) {
111
+ if (options.botUsername) {
112
+ this.createConnectAction = ({ token }) => ({
113
+ type: "url",
114
+ label: "Connect Telegram",
115
+ url: `https://t.me/${options.botUsername}?start=${encodeURIComponent(token)}`,
116
+ });
117
+ }
118
+ }
97
119
 
98
120
  validate() {
99
121
  if (!this.options.botToken) {
@@ -118,6 +140,8 @@ export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
118
140
  return null;
119
141
  }
120
142
 
143
+ const startPayload = parseTelegramStartPayload(text);
144
+
121
145
  return {
122
146
  surface: this.name,
123
147
  prompt: text,
@@ -125,6 +149,7 @@ export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
125
149
  externalUserId: String(userId),
126
150
  userTimeZone: "UTC",
127
151
  metadata: {
152
+ startPayload,
128
153
  telegramUpdate: update,
129
154
  },
130
155
  };
@@ -267,31 +292,6 @@ export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
267
292
  };
268
293
  }
269
294
 
270
- async resolveAdminUser(input: {
271
- adminforth: IAdminForth;
272
- incoming: ChatSurfaceIncomingMessage;
273
- }): Promise<AdminUser | null> {
274
- const adminUserResourceId = this.options.adminUserResourceId ?? DEFAULT_ADMIN_USER_RESOURCE_ID;
275
- const telegramIdField = this.options.adminUserTelegramIdField ?? DEFAULT_ADMIN_USER_TELEGRAM_ID_FIELD;
276
- const adminUser = await input.adminforth.resource(adminUserResourceId).get([
277
- {
278
- field: telegramIdField,
279
- operator: AdminForthFilterOperators.EQ,
280
- value: input.incoming.externalUserId,
281
- },
282
- ]);
283
-
284
- if (!adminUser) {
285
- return null;
286
- }
287
-
288
- return {
289
- pk: adminUser.id,
290
- username: adminUser[input.adminforth.config.auth!.usernameField],
291
- dbUser: adminUser,
292
- };
293
- }
294
-
295
295
  private async sendMessage(chatId: string, text: string) {
296
296
  if (!text) {
297
297
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/chat-surface-adapter-telegram",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@adminforth/agent": ">=1.0.2",
37
- "adminforth": ">=2.59.0"
37
+ "adminforth": ">=2.60.0"
38
38
  },
39
39
  "release": {
40
40
  "plugins": [
package/types.ts CHANGED
@@ -6,6 +6,11 @@ export type AdapterOptions = {
6
6
  */
7
7
  botToken: string;
8
8
 
9
+ /**
10
+ * Telegram bot username used to build the AdminForth account-link URL.
11
+ */
12
+ botUsername?: string;
13
+
9
14
  /**
10
15
  * Optional secret token configured in Telegram setWebhook.
11
16
  */
@@ -22,16 +27,4 @@ export type AdapterOptions = {
22
27
  * Default is 650ms.
23
28
  */
24
29
  draftUpdateIntervalMs?: number;
25
-
26
- /**
27
- * AdminForth admin user field that stores Telegram user id.
28
- * Default is `telegramId`.
29
- */
30
- adminUserTelegramIdField?: string;
31
-
32
- /**
33
- * AdminForth admin users resource id.
34
- * Default is `adminuser`.
35
- */
36
- adminUserResourceId?: string;
37
30
  };