|
| 1 | +--- |
| 2 | +title: Telegram Chat Surface Adapter (WIP) |
| 3 | +description: "Reference page for AdminForth Agent chat surface adapters, including Telegram bot setup and webhook configuration." |
| 4 | +slug: /tutorial/Adapters/chat-surface-adapter-telegram |
| 5 | +sidebar_position: 8 |
| 6 | +--- |
| 7 | + |
| 8 | +# Telegram Chat Surface Adapter |
| 9 | + |
| 10 | +Chat surface adapters connect external chat products to the [Agent plugin](/docs/tutorial/Plugins/agent/). |
| 11 | + |
| 12 | +```bash |
| 13 | +pnpm i @adminforth/chat-surface-adapter-telegram |
| 14 | +``` |
| 15 | + |
| 16 | +Create a Telegram bot with BotFather and add the token to `.env`: |
| 17 | + |
| 18 | +```env title=".env" |
| 19 | +TELEGRAM_BOT_TOKEN=your_bot_token |
| 20 | +TELEGRAM_WEBHOOK_SECRET=your_random_secret |
| 21 | +``` |
| 22 | + |
| 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 | + |
| 25 | +## Admin user field `telegramId` |
| 26 | + |
| 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`. |
| 29 | + |
| 30 | +Add this field to your `adminuser` resource: |
| 31 | + |
| 32 | +```ts |
| 33 | +{ |
| 34 | + name: 'telegramId', |
| 35 | + type: AdminForthDataTypes.STRING, |
| 36 | + showIn: ['show', 'edit', 'create'], |
| 37 | +}, |
| 38 | +``` |
| 39 | + |
| 40 | +If your field is named differently, configure `adminUserTelegramIdField` option (see below). |
| 41 | + |
| 42 | +Register the adapter in the Agent plugin: |
| 43 | + |
| 44 | +```ts |
| 45 | +import AdminForthAgent from '@adminforth/agent'; |
| 46 | +import TelegramChatSurfaceAdapter from '@adminforth/chat-surface-adapter-telegram'; |
| 47 | + |
| 48 | +new AdminForthAgent({ |
| 49 | + modes: [ |
| 50 | + // your modes |
| 51 | + ], |
| 52 | + sessionResource: { |
| 53 | + // your session resource config |
| 54 | + }, |
| 55 | + turnResource: { |
| 56 | + // your turn resource config |
| 57 | + }, |
| 58 | + //diff-add |
| 59 | + chatSurfaceAdapters: [ |
| 60 | + //diff-add |
| 61 | + new TelegramChatSurfaceAdapter({ |
| 62 | + //diff-add |
| 63 | + botToken: process.env.TELEGRAM_BOT_TOKEN as string, |
| 64 | + //diff-add |
| 65 | + webhookSecret: process.env.TELEGRAM_WEBHOOK_SECRET, |
| 66 | + //diff-add |
| 67 | + adminUserTelegramIdField: 'telegramId', |
| 68 | + //diff-add |
| 69 | + }), |
| 70 | + //diff-add |
| 71 | + ], |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +## Adapter options |
| 76 | + |
| 77 | +All options for `new TelegramChatSurfaceAdapter(options)`: |
| 78 | + |
| 79 | +- `botToken` (string, required) — Telegram bot token from BotFather. |
| 80 | +- `webhookSecret` (string, optional) — secret token configured in Telegram `setWebhook`. |
| 81 | +- `streamingMode` (`draft` | `typing` | `off`, optional) — streaming behavior for Telegram responses. |
| 82 | + - Default: `draft`. |
| 83 | + - Note: Telegram drafts work only in private chats. In non-private chats the adapter automatically falls back from `draft` to `typing`. |
| 84 | +- `draftUpdateIntervalMs` (number, optional) — throttle for draft preview updates. |
| 85 | + - Default: `650`. |
| 86 | +- `adminUserTelegramIdField` (string, optional) — admin user field that stores Telegram user id. |
| 87 | + - Default: `telegramId`. |
| 88 | +- `adminUserResourceId` (string, optional) — AdminForth resource id that stores admin users. |
| 89 | + - Default: `adminuser`. |
| 90 | + |
| 91 | +The plugin exposes this webhook endpoint: |
| 92 | + |
| 93 | +```txt |
| 94 | +/adminapi/v1/agent/surface/telegram/webhook |
| 95 | +``` |
| 96 | + |
| 97 | +Set it in Telegram: |
| 98 | + |
| 99 | +```bash |
| 100 | +curl -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setWebhook" \ |
| 101 | + -H "Content-Type: application/json" \ |
| 102 | + -d '{ |
| 103 | + "url": "https://your-domain.com/adminapi/v1/agent/surface/telegram/webhook", |
| 104 | + "secret_token": "'"${TELEGRAM_WEBHOOK_SECRET}"'" |
| 105 | + }' |
| 106 | +``` |
| 107 | + |
| 108 | +Telegram does not provide a user time zone in message updates, so the adapter sends `UTC` as `userTimeZone`. |
0 commit comments