Skip to content

Commit 78ff50d

Browse files
committed
fix: clean up imports and enhance TelegramChatSurfaceAdapter with connect action and start payload parsing
1 parent 3228729 commit 78ff50d

2 files changed

Lines changed: 37 additions & 44 deletions

File tree

index.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {
2-
AdminForthFilterOperators,
3-
type AdminUser,
42
type ChatSurfaceAdapter,
53
type ChatSurfaceEventSink,
64
type ChatSurfaceIncomingMessage,
75
type ChatSurfaceRequestContext,
8-
type IAdminForth,
96
} from "adminforth";
107
import { AdapterOptions } from "./types.js";
118
import { getFinalMessageStreamPreview, renderFinalMessageImages } from "./renderers.js";
@@ -40,14 +37,20 @@ type TelegramUpdate = {
4037
};
4138
};
4239

40+
type ChatSurfaceConnectAction = {
41+
type: "url";
42+
label: string;
43+
url: string;
44+
};
45+
4346
const TELEGRAM_API_BASE_URL = "https://api.telegram.org";
4447
const TELEGRAM_SECRET_HEADER = "x-telegram-bot-api-secret-token";
4548
const TELEGRAM_MESSAGE_MAX_LENGTH = 4096;
4649
const TELEGRAM_DRAFT_MAX_LENGTH = 4096;
4750
const DEFAULT_DRAFT_UPDATE_INTERVAL_MS = 650;
4851
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+/;
5154

5255
function createTelegramDraftId() {
5356
return randomInt(1, 2147483647);
@@ -90,10 +93,29 @@ function splitTelegramMessage(text: string) {
9093
return chunks;
9194
}
9295

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+
93106
export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
94107
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+
}
97119

98120
validate() {
99121
if (!this.options.botToken) {
@@ -118,13 +140,16 @@ export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
118140
return null;
119141
}
120142

143+
const startPayload = parseTelegramStartPayload(text);
144+
121145
return {
122146
surface: this.name,
123147
prompt: text,
124148
externalConversationId: String(chatId),
125149
externalUserId: String(userId),
126150
userTimeZone: "UTC",
127151
metadata: {
152+
startPayload,
128153
telegramUpdate: update,
129154
},
130155
};
@@ -267,31 +292,6 @@ export class TelegramChatSurfaceAdapter implements ChatSurfaceAdapter {
267292
};
268293
}
269294

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-
295295
private async sendMessage(chatId: string, text: string) {
296296
if (!text) {
297297
return;

types.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export type AdapterOptions = {
66
*/
77
botToken: string;
88

9+
/**
10+
* Telegram bot username used to build the AdminForth account-link URL.
11+
*/
12+
botUsername?: string;
13+
914
/**
1015
* Optional secret token configured in Telegram setWebhook.
1116
*/
@@ -22,16 +27,4 @@ export type AdapterOptions = {
2227
* Default is 650ms.
2328
*/
2429
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;
3730
};

0 commit comments

Comments
 (0)