Skip to content

Commit fb5e87e

Browse files
committed
docs: update Telegram Chat Surface Adapter tutorial with externalUserId field and account linking instructions
1 parent 94a8b37 commit fb5e87e

2 files changed

Lines changed: 53 additions & 30 deletions

File tree

adminforth/documentation/docs/tutorial/05-Adapters/09-chat-surface-adapters.md

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,61 @@ Create a Telegram bot with BotFather and add the token to `.env`:
1717

1818
```env title=".env"
1919
TELEGRAM_BOT_TOKEN=your_bot_token
20+
TELEGRAM_BOT_USERNAME=your_bot_username_without_at
2021
TELEGRAM_WEBHOOK_SECRET=your_random_secret
2122
```
2223

23-
The webhook secret confirms that the request came through Telegram. Your app should still map the Telegram user id to a real AdminForth admin user before running the agent.
24+
The webhook secret confirms that the request came through Telegram.
2425

25-
## Admin user field `telegramId`
26+
## Admin user field `externalUserId`
2627

27-
To map Telegram users to AdminForth admin users, the adapter looks up an admin user record by Telegram user id.
28-
By default it expects the admin user resource to have a field named `telegramId`.
28+
External chat accounts are linked by the Agent plugin, not by the Telegram adapter directly. The plugin stores linked external user ids in a JSON field on the AdminForth auth user resource.
2929

30-
Add this field to your `adminuser` resource:
30+
By default this field is named `externalUserId`. Add it to your `adminuser` resource:
3131

3232
```ts
3333
{
34-
name: 'telegramId',
35-
type: AdminForthDataTypes.STRING,
36-
showIn: ['show', 'edit', 'create'],
34+
name: 'externalUserId',
35+
type: AdminForthDataTypes.JSON,
3736
},
3837
```
3938

40-
Also add the matching column to your database schema and run a migration. For example, with Prisma:
39+
Also add the matching column to your database schema and run a migration. For example, with Prisma and PostgreSQL:
4140

4241
```prisma title="schema.prisma"
4342
model adminuser {
4443
// existing fields
45-
telegramId String?
44+
externalUserId Json?
4645
}
4746
```
4847

48+
For Prisma SQLite, store the same field as text:
49+
50+
```prisma title="schema.prisma"
51+
model adminuser {
52+
// existing fields
53+
externalUserId String?
54+
}
55+
```
56+
57+
AdminForth should still define this resource column as `AdminForthDataTypes.JSON`; the SQLite connector serializes it into the text column and parses it back.
58+
4959
Then create and apply the migration using your app's migration scripts:
5060

5161
```bash
52-
pnpm makemigration --name add-adminuser-telegram-id
62+
pnpm makemigration --name add-adminuser-external-user-id
5363
pnpm migrate:local
5464
```
5565

56-
After the migration, set `telegramId` on the admin user record to the numeric Telegram user id that should be allowed to use the bot.
66+
When a Telegram account is linked, the field stores data like this:
5767

58-
If your field is named differently, configure `adminUserTelegramIdField` option (see below).
68+
```json
69+
{
70+
"telegram": "123456789"
71+
}
72+
```
73+
74+
If your field is named differently, configure `chatExternalIdsField` on the Agent plugin.
5975

6076
Register the adapter in the Agent plugin:
6177

@@ -80,31 +96,41 @@ new AdminForthAgent({
8096
//diff-add
8197
botToken: process.env.TELEGRAM_BOT_TOKEN as string,
8298
//diff-add
83-
webhookSecret: process.env.TELEGRAM_WEBHOOK_SECRET,
99+
botUsername: process.env.TELEGRAM_BOT_USERNAME,
84100
//diff-add
85-
adminUserTelegramIdField: 'telegramId',
101+
webhookSecret: process.env.TELEGRAM_WEBHOOK_SECRET,
86102
//diff-add
87103
}),
88104
//diff-add
89105
],
106+
// optional, defaults to 'externalUserId'
107+
//diff-add
108+
chatExternalIdsField: 'externalUserId',
90109
});
91110
```
92111

112+
When `botUsername` is configured, the Agent plugin adds **Chat Surfaces** to the user menu settings pages. A logged-in AdminForth user can open that page and click **Connect**. The Telegram adapter returns a URL like:
113+
114+
```txt
115+
https://t.me/<botUsername>?start=<link-token>
116+
```
117+
118+
After the user starts the bot with that token, AdminForth stores the Telegram user id in `externalUserId.telegram`. The same page also supports reconnecting and disconnecting the Telegram account.
119+
120+
You can also prefill the JSON field manually if you do not want to use the connect page.
121+
93122
## Adapter options
94123

95124
All options for `new TelegramChatSurfaceAdapter(options)`:
96125

97126
- `botToken` (string, required) — Telegram bot token from BotFather.
127+
- `botUsername` (string, optional) — bot username used to build the account-link URL for the **Chat Surfaces** settings page.
98128
- `webhookSecret` (string, optional) — secret token configured in Telegram `setWebhook`.
99129
- `streamingMode` (`draft` | `typing` | `off`, optional) — streaming behavior for Telegram responses.
100130
- Default: `draft`.
101131
- Note: Telegram drafts work only in private chats. In non-private chats the adapter automatically falls back from `draft` to `typing`.
102132
- `draftUpdateIntervalMs` (number, optional) — throttle for draft preview updates.
103133
- Default: `650`.
104-
- `adminUserTelegramIdField` (string, optional) — admin user field that stores Telegram user id.
105-
- Default: `telegramId`.
106-
- `adminUserResourceId` (string, optional) — AdminForth resource id that stores admin users.
107-
- Default: `adminuser`.
108134

109135
The plugin exposes this webhook endpoint:
110136

adminforth/documentation/docs/tutorial/08-Plugins/01-agent.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,30 +289,27 @@ The plugin adds a chat surface to the admin UI, keeps session history per admin
289289
By default, the Agent plugin exposes a chat surface inside the AdminForth admin UI.
290290
If you want to talk to the same agent from external chat products (Telegram, etc.), connect a **chat surface adapter**.
291291

292-
The adapter is registered in the plugin config via `chatSurfaceAdapters` and the plugin exposes an HTTP webhook endpoint for it.
293-
294-
Example (Telegram):
292+
Register adapters through `chatSurfaceAdapters`:
295293

296294
```ts
297295
import AdminForthAgent from '@adminforth/agent';
298-
import TelegramChatSurfaceAdapter from '@adminforth/chat-surface-adapter-telegram';
296+
import SomeChatSurfaceAdapter from '@adminforth/some-chat-surface-adapter';
299297

300298
new AdminForthAgent({
301299
// ...modes, sessionResource, turnResource, etc.
302300
chatSurfaceAdapters: [
303-
new TelegramChatSurfaceAdapter({
304-
botToken: process.env.TELEGRAM_BOT_TOKEN as string,
305-
webhookSecret: process.env.TELEGRAM_WEBHOOK_SECRET,
306-
// optional
307-
adminUserTelegramIdField: 'telegramId',
301+
new SomeChatSurfaceAdapter({
302+
// adapter-specific options
308303
}),
309304
],
310305
});
311306
```
312307

313-
Next steps (Telegram bot setup, webhook URL, required `telegramId` field on the admin user resource, and all adapter options) are documented here:
308+
When an adapter supports account linking, the Agent plugin adds a user menu settings page named **Chat Surfaces** where logged-in users can connect, reconnect, and disconnect external accounts.
309+
310+
For Telegram setup, including required user fields, webhook URL, environment variables, and adapter options, see:
314311

315-
- Telegram Chat Surface Adapter: `/docs/tutorial/Adapters/chat-surface-adapter-telegram`
312+
- [Telegram Chat Surface Adapter](https://adminforth.dev/docs/tutorial/Adapters/chat-surface-adapter-telegram/)
316313

317314
## Debugging agent turns
318315

0 commit comments

Comments
 (0)