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
+60-38Lines changed: 60 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,23 @@ 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/oauth-adapter-telegram
14
17
```
15
18
16
-
Create a Telegram bot with BotFather and add the token to `.env`:
19
+
Create a Telegram bot with BotFather and copy the token:
20
+
21
+
1. Open [BotFather](https://t.me/BotFather).
22
+
2. Click **Open** and open it in the Telegram app.
The webhook secret confirms that the request came through Telegram.
25
35
26
-
## Admin user field`externalUserId`
36
+
## External identity`externalUserId`
27
37
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.
38
+
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
39
30
-
By default this field is named `externalUserId`. Add it to your `adminuser` resource:
40
+
Add the field to your external identities resource:
31
41
32
42
```ts
33
43
{
34
44
name: 'externalUserId',
35
-
type: AdminForthDataTypes.JSON,
45
+
type: AdminForthDataTypes.STRING,
36
46
},
37
47
```
38
48
39
49
Also add the matching column to your database schema and run a migration. For example, with Prisma and PostgreSQL:
40
50
41
51
```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 {
52
+
model AdminUserExternalIdentity {
52
53
// existing fields
53
54
externalUserId String?
54
55
}
55
56
```
56
57
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
58
Then create and apply the migration using your app's migration scripts:
Then register the Telegram chat surface adapter in the Agent plugin:
77
92
78
93
```ts
79
94
importAdminForthAgentfrom'@adminforth/agent';
@@ -103,28 +118,35 @@ new AdminForthAgent({
103
118
}),
104
119
//diff-add
105
120
],
106
-
// optional, defaults to 'externalUserId'
107
121
//diff-add
108
-
chatExternalIdsField: 'externalUserId',
122
+
chatExternalIdentityResource: {
123
+
//diff-add
124
+
resourceId: 'admin_user_external_identities',
125
+
//diff-add
126
+
surfaces: {
127
+
//diff-add
128
+
telegram: {
129
+
//diff-add
130
+
provider: 'AdminForthAdapterTelegramOauth2',
131
+
//diff-add
132
+
},
133
+
//diff-add
134
+
},
135
+
//diff-add
136
+
},
109
137
});
110
138
```
111
139
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.
140
+
External chat users are resolved through OAuth connected accounts. Configure OAuth `externalIdentityResource` and Agent `chatExternalIdentityResource`, then let users connect Telegram from **Connected Accounts**.
119
141
120
-
You can also prefill the JSON field manually if you do not want to use the connect page.
142
+
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
143
122
144
## Adapter options
123
145
124
146
All options for `new TelegramChatSurfaceAdapter(options)`:
125
147
126
148
-`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.
149
+
-`botUsername` (string, optional) — bot username. OAuth connected accounts are used for user linking.
128
150
-`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