You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adminforth/documentation/docs/tutorial/05-Adapters/09-chat-surface-adapters.md
+52-37Lines changed: 52 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,13 @@ sidebar_position: 8
7
7
8
8
# Telegram Chat Surface Adapter
9
9
10
-
Chat surface adapters connect external chat products to the [Agent plugin](/docs/tutorial/Plugins/agent/).
10
+
Chat surface adapters connect external chat products to the [Agent plugin](/docs/tutorial/Plugins/agent/). They only receive and send chat messages. User linking is handled through OAuth connected accounts.
11
+
12
+
For Telegram you need both adapters:
11
13
12
14
```bash
13
15
pnpm i @adminforth/chat-surface-adapter-telegram
16
+
pnpm i @adminforth/telegram-oauth-adapter
14
17
```
15
18
16
19
Create a Telegram bot with BotFather and add the token to `.env`:
The webhook secret confirms that the request came through Telegram.
25
28
26
-
## Admin user field`externalUserId`
29
+
## External identity`externalUserId`
27
30
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.
31
+
External chat accounts are resolved through OAuth connected accounts. The Telegram OAuth adapter writes the Telegram user id into `externalUserId` on the external identities resource. The Agent plugin then uses that field to map incoming Telegram messages to AdminForth users.
29
32
30
-
By default this field is named `externalUserId`. Add it to your `adminuser` resource:
33
+
Add the field to your external identities resource:
31
34
32
35
```ts
33
36
{
34
37
name: 'externalUserId',
35
-
type: AdminForthDataTypes.JSON,
38
+
type: AdminForthDataTypes.STRING,
36
39
},
37
40
```
38
41
39
42
Also add the matching column to your database schema and run a migration. For example, with Prisma and PostgreSQL:
40
43
41
44
```prisma title="schema.prisma"
42
-
model adminuser {
43
-
// existing fields
44
-
externalUserId Json?
45
-
}
46
-
```
47
-
48
-
For Prisma SQLite, store the same field as text:
49
-
50
-
```prisma title="schema.prisma"
51
-
model adminuser {
45
+
model AdminUserExternalIdentity {
52
46
// existing fields
53
47
externalUserId String?
54
48
}
55
49
```
56
50
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
-
59
51
Then create and apply the migration using your app's migration scripts:
Then register the Telegram chat surface adapter in the Agent plugin:
77
85
78
86
```ts
79
87
importAdminForthAgentfrom'@adminforth/agent';
@@ -103,28 +111,35 @@ new AdminForthAgent({
103
111
}),
104
112
//diff-add
105
113
],
106
-
// optional, defaults to 'externalUserId'
107
114
//diff-add
108
-
chatExternalIdsField: 'externalUserId',
115
+
chatExternalIdentityResource: {
116
+
//diff-add
117
+
resourceId: 'admin_user_external_identities',
118
+
//diff-add
119
+
surfaces: {
120
+
//diff-add
121
+
telegram: {
122
+
//diff-add
123
+
provider: 'AdminForthAdapterTelegramOauth2',
124
+
//diff-add
125
+
},
126
+
//diff-add
127
+
},
128
+
//diff-add
129
+
},
109
130
});
110
131
```
111
132
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.
133
+
External chat users are resolved through OAuth connected accounts. Configure OAuth `externalIdentityResource` and Agent `chatExternalIdentityResource`, then let users connect Telegram from **Connected Accounts**.
119
134
120
-
You can also prefill the JSON field manually if you do not want to use the connect page.
135
+
The `provider` value must match the Telegram OAuth adapter class name. Users must connect Telegram in **Settings -> Connected Accounts** before the Telegram bot can identify them.
121
136
122
137
## Adapter options
123
138
124
139
All options for `new TelegramChatSurfaceAdapter(options)`:
125
140
126
141
-`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.
142
+
-`botUsername` (string, optional) — bot username. OAuth connected accounts are used for user linking.
128
143
-`webhookSecret` (string, optional) — secret token configured in Telegram `setWebhook`.
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.
322
+
External chat users are resolved through the OAuth external identities resource. The `provider` value in `chatExternalIdentityResource.surfaces` must match the OAuth adapter class that writes connected accounts, for example `AdminForthAdapterTelegramOauth2`.
323
+
324
+
Users connect their Telegram account from **Settings -> Connected Accounts**. After that, messages from the Telegram bot can be matched to the AdminForth user through the external identity `externalUserId`.
309
325
310
326
For Telegram setup, including required user fields, webhook URL, environment variables, and adapter options, see:
0 commit comments