@zhin.js/adapter-telegram 1.0.68 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +825 -16
- package/README.md +106 -77
- package/adapters/telegram.js +34 -0
- package/adapters/telegram.ts +39 -0
- package/agent/PERMITS.md +24 -0
- package/agent/tools/create_invite.ts +18 -0
- package/agent/tools/list_admins.ts +24 -0
- package/agent/tools/member_count.ts +16 -0
- package/agent/tools/pin_message.ts +19 -0
- package/agent/tools/react.ts +18 -0
- package/agent/tools/send_poll.ts +32 -0
- package/agent/tools/send_sticker.ts +17 -0
- package/agent/tools/set_description.ts +17 -0
- package/agent/tools/set_permissions.ts +31 -0
- package/agent/tools/unpin_message.ts +19 -0
- package/commands/endpoint/add/[id].js +3 -0
- package/commands/endpoint/add/[id].ts +3 -0
- package/commands/endpoint/list.js +3 -0
- package/commands/endpoint/list.ts +3 -0
- package/commands/endpoint/remove/[id].js +3 -0
- package/commands/endpoint/remove/[id].ts +3 -0
- package/lib/client.d.ts +12 -0
- package/lib/client.js +2 -0
- package/lib/endpoint.d.ts +112 -0
- package/lib/endpoint.js +535 -0
- package/lib/index.d.ts +4 -18
- package/lib/index.js +4 -455
- package/lib/markdown-to-html.d.ts +9 -0
- package/lib/markdown-to-html.js +75 -0
- package/lib/platform-permit.d.ts +17 -0
- package/lib/platform-permit.js +51 -0
- package/lib/polling.d.ts +9 -0
- package/lib/polling.js +57 -0
- package/lib/protocol.d.ts +299 -0
- package/lib/protocol.js +474 -0
- package/lib/telegram-endpoint-commands.d.ts +1 -0
- package/lib/telegram-endpoint-commands.js +16 -0
- package/lib/telegram-runtime-state.d.ts +1 -0
- package/lib/telegram-runtime-state.js +6 -0
- package/lib/webhook.d.ts +12 -0
- package/lib/webhook.js +55 -0
- package/package.json +59 -28
- package/plugin.js +19 -0
- package/schema.json +110 -0
- package/src/client.ts +16 -0
- package/src/endpoint.ts +679 -0
- package/src/index.ts +39 -426
- package/src/markdown-to-html.ts +86 -0
- package/src/platform-permit.ts +65 -0
- package/src/polling.ts +76 -0
- package/src/protocol.ts +767 -0
- package/src/telegram-endpoint-commands.ts +17 -0
- package/src/telegram-runtime-state.ts +7 -0
- package/src/webhook.ts +75 -0
- package/client/Dashboard.tsx +0 -295
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/client/utils/api.ts +0 -17
- package/dist/index.js +0 -32
- package/lib/adapter.d.ts +0 -18
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -55
- package/lib/adapter.js.map +0 -1
- package/lib/bot.d.ts +0 -140
- package/lib/bot.d.ts.map +0 -1
- package/lib/bot.js +0 -866
- package/lib/bot.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types.d.ts +0 -29
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/adapter.ts +0 -64
- package/src/bot.ts +0 -983
- package/src/types.ts +0 -32
- /package/{skills/telegram/SKILL.md → agent/skills/telegram.md} +0 -0
package/README.md
CHANGED
|
@@ -1,107 +1,136 @@
|
|
|
1
1
|
# @zhin.js/adapter-telegram
|
|
2
2
|
|
|
3
|
-
Telegram
|
|
3
|
+
Zhin.js Telegram Bot API 适配器(Plugin Runtime),默认通过 **长轮询 `getUpdates`** 收发消息(无需 host)。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 功能
|
|
6
|
+
|
|
7
|
+
- 长轮询 `getUpdates` 入站(默认;无需公网 IP / host-http)
|
|
8
|
+
- 解析 text / image / video / audio / voice / document / sticker / location / callback_query
|
|
9
|
+
- 支持私聊与群组
|
|
10
|
+
- 出站 `send({ conversation, payload })` → Bot API(Markdown→安全 HTML / media / keyboard)
|
|
11
|
+
- 约定式 `defineAdapter` / `definePlugin`(无需 `usePlugin`)
|
|
12
|
+
- Webhook 模式通过 `httpHostToken` 注册回调路由,支持 secretToken 校验
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
6
15
|
|
|
7
16
|
```bash
|
|
8
17
|
pnpm add @zhin.js/adapter-telegram
|
|
9
18
|
```
|
|
10
19
|
|
|
11
|
-
##
|
|
12
|
-
|
|
13
|
-
```typescript
|
|
14
|
-
import { defineConfig } from 'zhin.js'
|
|
15
|
-
|
|
16
|
-
export default defineConfig({
|
|
17
|
-
bots: [
|
|
18
|
-
{
|
|
19
|
-
name: 'my-telegram-bot',
|
|
20
|
-
context: 'telegram',
|
|
21
|
-
token: 'YOUR_BOT_TOKEN', // Get from @BotFather
|
|
22
|
-
polling: true, // Use long polling (default)
|
|
23
|
-
// OR use webhooks:
|
|
24
|
-
// polling: false,
|
|
25
|
-
// webhook: {
|
|
26
|
-
// domain: 'https://yourdomain.com',
|
|
27
|
-
// path: '/telegram-webhook',
|
|
28
|
-
// port: 8443
|
|
29
|
-
// }
|
|
30
|
-
}
|
|
31
|
-
]
|
|
32
|
-
})
|
|
33
|
-
```
|
|
20
|
+
## Plugin Runtime
|
|
34
21
|
|
|
35
|
-
|
|
22
|
+
- `@zhin.js/adapter` — 约定式 `adapters/telegram.ts`(`defineAdapter`)
|
|
23
|
+
- `@zhin.js/core` — `Endpoint.emit(...)` 入站、`outboundMessageToken` 出站
|
|
24
|
+
- `zhin.js` — `plugin.ts`(`definePlugin`)
|
|
25
|
+
- 配置经插件 `schema.json` 落到 `plugins.<instanceKey>`
|
|
26
|
+
- **无需** `@zhin.js/host-http`(polling 路径)
|
|
36
27
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
- ✅ Message formatting (bold, italic, code, links)
|
|
40
|
-
- ✅ Reply to messages
|
|
41
|
-
- ✅ Mentions (@username)
|
|
42
|
-
- ✅ Stickers and locations
|
|
43
|
-
- ✅ Callback queries (inline buttons)
|
|
44
|
-
- ✅ Long polling and webhook modes
|
|
45
|
-
- ✅ Private and group chats
|
|
28
|
+
入站:`gateway.receive({ conversation, message, content, sender, metadata })`(`conversation` 为 ConversationRef,由 `telegramInboundConversation` 归一化)
|
|
29
|
+
出站:`send({ conversation, payload })` → Telegram Bot API
|
|
46
30
|
|
|
47
|
-
|
|
31
|
+
### 平台权限(platform permit)
|
|
48
32
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
3. Copy the bot token provided
|
|
52
|
-
4. Add the token to your configuration
|
|
33
|
+
- sender role 已恢复:群消息入站时经 `getChatMember`(60s 缓存)解析,写入 `metadata.senderRole` / `metadata.senderPermissions`。
|
|
34
|
+
- `plugin.ts` 在 generation setup 注册 checker,并在 dispose 注销;Plugin Runtime CapabilityIngress 与 ToolSystem 统一经 Core `canAccessTool()` 消费 `permissions`。
|
|
53
35
|
|
|
54
|
-
##
|
|
36
|
+
## 前置条件
|
|
55
37
|
|
|
56
|
-
|
|
38
|
+
| 要求 | 说明 |
|
|
39
|
+
|------|------|
|
|
40
|
+
| **Bot Token** | 通过 [@BotFather](https://t.me/botfather) 创建并获取 Token |
|
|
41
|
+
| **Polling(默认)** | 本地/生产均可;主动拉取更新,无需公网 HTTPS |
|
|
42
|
+
| **网络** | 出站可访问 `api.telegram.org` |
|
|
43
|
+
| **host-http** | Polling **不需要**;Webhook **需要** CLI HTTP Host |
|
|
57
44
|
|
|
58
|
-
|
|
59
|
-
import { usePlugin, MessageCommand } from 'zhin.js'
|
|
45
|
+
必填字段(`endpoints[i]`):`id`、`token`。
|
|
60
46
|
|
|
61
|
-
|
|
47
|
+
## 最小配置
|
|
62
48
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
49
|
+
```yaml
|
|
50
|
+
# zhin.config.yml(Plugin Runtime)
|
|
51
|
+
plugins:
|
|
52
|
+
telegram:
|
|
53
|
+
# polling: true # 默认
|
|
54
|
+
endpoints:
|
|
55
|
+
- id: my-telegram-bot
|
|
56
|
+
token: ${TELEGRAM_TOKEN}
|
|
68
57
|
```
|
|
69
58
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
59
|
+
根插件 `zhin.plugins`(或项目图)需引用 `@zhin.js/adapter-telegram`(`instanceKey: telegram`)。
|
|
60
|
+
|
|
61
|
+
## 环境变量
|
|
62
|
+
|
|
63
|
+
| 变量 | 说明 |
|
|
64
|
+
|------|------|
|
|
65
|
+
| `TELEGRAM_TOKEN` / `TELEGRAM_BOT_TOKEN` | Bot Token |
|
|
66
|
+
| `TELEGRAM_BOT_NAME` | 可选,默认 endpoint 名 |
|
|
67
|
+
|
|
68
|
+
## Webhook
|
|
69
|
+
|
|
70
|
+
配置 `polling: false`,启用 CLI HTTP Host,并提供公网 HTTPS 回调地址。
|
|
71
|
+
`webhook.path` 必须与反向代理转发路径一致,多账号使用不同路径。
|
|
72
|
+
|
|
73
|
+
```yaml
|
|
74
|
+
http:
|
|
75
|
+
host: 127.0.0.1
|
|
76
|
+
port: 8086
|
|
77
|
+
plugins:
|
|
78
|
+
telegram:
|
|
79
|
+
polling: false
|
|
80
|
+
endpoints:
|
|
81
|
+
- id: my-telegram-bot
|
|
82
|
+
token: ${TELEGRAM_TOKEN}
|
|
83
|
+
webhook:
|
|
84
|
+
domain: https://bot.example.com
|
|
85
|
+
path: /telegram/webhook
|
|
86
|
+
secretToken: ${TELEGRAM_WEBHOOK_SECRET}
|
|
81
87
|
```
|
|
82
88
|
|
|
83
|
-
|
|
89
|
+
启动时注册 Host 路由并调用 `setWebhook`;停止时释放路由。
|
|
90
|
+
配置 `secretToken` 后,回调须携带匹配的 `X-Telegram-Bot-Api-Secret-Token`,否则返回 403。
|
|
84
91
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
## 稳定化范围
|
|
93
|
+
|
|
94
|
+
当前为 **Advanced / 首批升档候选**,完整适配器测试已纳入 `pnpm check:stable`。
|
|
95
|
+
长轮询停止会中止请求与退避;即使传输层在中止后返回更新,也不会派发或推进 offset。
|
|
96
|
+
轮询 offset 仅保存在当前 Endpoint 内存中,不承诺跨重启去重或业务处理恰好一次。
|
|
97
|
+
Webhook 接收确认不等于业务处理完成;需要幂等性的业务应自行持久化去重。
|
|
98
|
+
详见[平台稳定验收](https://github.com/zhinjs/zhin/blob/main/docs/contributing/platform-acceptance.md)。
|
|
99
|
+
|
|
100
|
+
## 消息类型映射
|
|
101
|
+
|
|
102
|
+
| Telegram | 入站 content(文本摘要) | 出站 wire |
|
|
103
|
+
|----------|--------------------------|-----------|
|
|
104
|
+
| text | 原文 | sendMessage |
|
|
105
|
+
| markdown | 原文 | sendMessage(`parse_mode: HTML`) |
|
|
106
|
+
| photo | `[image]` / caption | sendPhoto(`file_id` / `url`) |
|
|
107
|
+
| video | `[video]` | sendVideo |
|
|
108
|
+
| audio / voice | `[audio]` / `[voice]` | sendAudio / sendVoice |
|
|
109
|
+
| document | `[file: name]` | sendDocument |
|
|
110
|
+
| sticker | `[sticker: …]` | sendSticker |
|
|
111
|
+
| location | `[location: lat,lon]` | sendLocation |
|
|
112
|
+
| callback_query | `[action: data]` | — |
|
|
113
|
+
|
|
114
|
+
## AI tools
|
|
92
115
|
|
|
93
|
-
|
|
116
|
+
| Kind | Path |
|
|
117
|
+
|------|------|
|
|
118
|
+
| Platform tools (10) | `agent/tools/`(invite / pin / admins / sticker / poll 等) |
|
|
119
|
+
| Skill doc | `agent/skills/telegram.md` |
|
|
94
120
|
|
|
95
|
-
|
|
121
|
+
## 故障排查
|
|
96
122
|
|
|
97
|
-
|
|
123
|
+
| 现象 | 排查 |
|
|
124
|
+
|------|------|
|
|
125
|
+
| 收不到消息 | Token 是否正确;进程已 `open()`;同一 Token 勿多进程同时 polling |
|
|
126
|
+
| Polling 报错 | 检查能否访问 `api.telegram.org`;查看日志 `op: poll` |
|
|
127
|
+
| Webhook 配置报错 | 检查 HTTP Host、公网 HTTPS、反向代理路径与 secretToken |
|
|
128
|
+
| 发送失败 | Token 是否被撤销;查看 Bot API 错误描述 |
|
|
98
129
|
|
|
99
|
-
|
|
130
|
+
## Documentation
|
|
100
131
|
|
|
101
|
-
|
|
102
|
-
-
|
|
103
|
-
- URL
|
|
104
|
-
- Local file path
|
|
132
|
+
- [Telegram adapter on zhin.js.org](https://zhin.js.org/adapters/telegram)
|
|
133
|
+
- [Adapters overview](https://zhin.js.org/essentials/adapters)
|
|
105
134
|
|
|
106
135
|
## License
|
|
107
136
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Generated by build-plugin-runtime-entries.mjs. Do not edit.
|
|
2
|
+
/**
|
|
3
|
+
* Convention entry: discover `adapters/telegram.ts` → defineAdapter.
|
|
4
|
+
*/
|
|
5
|
+
import { defineAdapter } from 'zhin.js/adapter';
|
|
6
|
+
import { httpHostToken } from '@zhin.js/host-http';
|
|
7
|
+
import { TelegramEndpoint } from "../lib/endpoint.js";
|
|
8
|
+
import { resolveTelegramConfig, } from "../lib/protocol.js";
|
|
9
|
+
import { telegramRuntimeStateToken } from "../lib/telegram-runtime-state.js";
|
|
10
|
+
export { TelegramEndpoint } from "../lib/endpoint.js";
|
|
11
|
+
export default defineAdapter({
|
|
12
|
+
capabilities: ['inbound', 'outbound'],
|
|
13
|
+
operations: ['recall'],
|
|
14
|
+
// 媒体 url / file_id 直发,base64 / 本地路径经 multipart attach:// 上传物化;
|
|
15
|
+
// inline keyboard 原生按钮承载交互段。
|
|
16
|
+
segments: {
|
|
17
|
+
outboundMedia: ['url', 'upload'],
|
|
18
|
+
interactive: 'native',
|
|
19
|
+
markdown: 'native',
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
const config = resolveTelegramConfig(context.config);
|
|
23
|
+
// 注册到插件运行时状态(telegram.endpoint list 的"运行中"数据源)
|
|
24
|
+
context.use(telegramRuntimeStateToken).endpoints.set(config.id, {
|
|
25
|
+
id: config.id,
|
|
26
|
+
mode: config.mode,
|
|
27
|
+
});
|
|
28
|
+
return new TelegramEndpoint({
|
|
29
|
+
id: context.id,
|
|
30
|
+
config,
|
|
31
|
+
http: config.mode === 'webhook' ? context.use(httpHostToken) : undefined,
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convention entry: discover `adapters/telegram.ts` → defineAdapter.
|
|
3
|
+
*/
|
|
4
|
+
import { defineAdapter } from 'zhin.js/adapter';
|
|
5
|
+
import { httpHostToken } from '@zhin.js/host-http';
|
|
6
|
+
import { TelegramEndpoint } from '../src/endpoint.js';
|
|
7
|
+
import {
|
|
8
|
+
resolveTelegramConfig,
|
|
9
|
+
type TelegramAdapterConfig,
|
|
10
|
+
} from '../src/protocol.js';
|
|
11
|
+
import { telegramRuntimeStateToken } from '../src/telegram-runtime-state.js';
|
|
12
|
+
|
|
13
|
+
export { TelegramEndpoint } from '../src/endpoint.js';
|
|
14
|
+
export type { TelegramEndpointOptions, TelegramFetch } from '../src/endpoint.js';
|
|
15
|
+
|
|
16
|
+
export default defineAdapter<TelegramAdapterConfig>({
|
|
17
|
+
capabilities: ['inbound', 'outbound'],
|
|
18
|
+
operations: ['recall'],
|
|
19
|
+
// 媒体 url / file_id 直发,base64 / 本地路径经 multipart attach:// 上传物化;
|
|
20
|
+
// inline keyboard 原生按钮承载交互段。
|
|
21
|
+
segments: {
|
|
22
|
+
outboundMedia: ['url', 'upload'],
|
|
23
|
+
interactive: 'native',
|
|
24
|
+
markdown: 'native',
|
|
25
|
+
},
|
|
26
|
+
create(context) {
|
|
27
|
+
const config = resolveTelegramConfig(context.config);
|
|
28
|
+
// 注册到插件运行时状态(telegram.endpoint list 的"运行中"数据源)
|
|
29
|
+
context.use(telegramRuntimeStateToken).endpoints.set(config.id, {
|
|
30
|
+
id: config.id,
|
|
31
|
+
mode: config.mode,
|
|
32
|
+
});
|
|
33
|
+
return new TelegramEndpoint({
|
|
34
|
+
id: context.id,
|
|
35
|
+
config,
|
|
36
|
+
http: config.mode === 'webhook' ? context.use(httpHostToken) : undefined,
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
});
|
package/agent/PERMITS.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Telegram Platform Permits
|
|
2
|
+
|
|
3
|
+
## 入站字段
|
|
4
|
+
|
|
5
|
+
群/超级群消息经 `getChatMember`(TTL 60s)写入:
|
|
6
|
+
|
|
7
|
+
| 字段 | 取值 |
|
|
8
|
+
|------|------|
|
|
9
|
+
| `$sender.role` | `creator` · `administrator` · `member` |
|
|
10
|
+
| `$sender.permissions` | `chat_creator` · `chat_administrator` · `restrict_members` · `pin_messages` · `manage_chat` · … |
|
|
11
|
+
|
|
12
|
+
## Permit 词汇表
|
|
13
|
+
|
|
14
|
+
| `platform(telegram,…)` | 含义 |
|
|
15
|
+
|------------------------|------|
|
|
16
|
+
| `chat_creator` | 群主 |
|
|
17
|
+
| `chat_administrator` | 管理员 |
|
|
18
|
+
| `restrict_members` | 可禁言成员 |
|
|
19
|
+
| `pin_messages` | 可置顶 |
|
|
20
|
+
| `manage_chat` | 可改群默认权限 |
|
|
21
|
+
|
|
22
|
+
## 工厂映射
|
|
23
|
+
|
|
24
|
+
`scene_admin` → `chat_administrator` · `scene_owner` → `chat_creator`
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ chat_id: string }>({
|
|
6
|
+
description: '创建 Telegram 群组邀请链接',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
9
|
+
}),
|
|
10
|
+
adapter: 'telegram',
|
|
11
|
+
tags: ['telegram'],
|
|
12
|
+
permissions: [platformPermit('chat_administrator')],
|
|
13
|
+
async execute({ chat_id }: { chat_id: string }, context) {
|
|
14
|
+
const endpoint = context.$client;
|
|
15
|
+
const link = await endpoint.createInviteLink(Number(chat_id));
|
|
16
|
+
return { invite_link: link, message: `邀请链接: ${link}` };
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export default defineAgentTool<{ chat_id: string }>({
|
|
5
|
+
description: '获取 Telegram 群组管理员列表',
|
|
6
|
+
inputSchema: z.object({
|
|
7
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
8
|
+
}),
|
|
9
|
+
adapter: 'telegram',
|
|
10
|
+
tags: ['telegram'],
|
|
11
|
+
async execute({ chat_id }: { chat_id: string }, context) {
|
|
12
|
+
const endpoint = context.$client;
|
|
13
|
+
const admins = await endpoint.getChatAdmins(Number(chat_id));
|
|
14
|
+
return {
|
|
15
|
+
admins: admins.map((a: { user: { id: number; username?: string; first_name?: string }; status: string }) => ({
|
|
16
|
+
user_id: a.user.id,
|
|
17
|
+
username: a.user.username,
|
|
18
|
+
first_name: a.user.first_name,
|
|
19
|
+
status: a.status,
|
|
20
|
+
})),
|
|
21
|
+
count: admins.length,
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export default defineAgentTool<{ chat_id: string }>({
|
|
5
|
+
description: '获取 Telegram 群组成员数量',
|
|
6
|
+
inputSchema: z.object({
|
|
7
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
8
|
+
}),
|
|
9
|
+
adapter: 'telegram',
|
|
10
|
+
tags: ['telegram'],
|
|
11
|
+
async execute({ chat_id }: { chat_id: string }, context) {
|
|
12
|
+
const endpoint = context.$client;
|
|
13
|
+
const count = await endpoint.getChatMemberCount(Number(chat_id));
|
|
14
|
+
return { count, message: `群组共有 ${count} 名成员` };
|
|
15
|
+
},
|
|
16
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ chat_id: string; message_id: string }>({
|
|
6
|
+
description: '置顶 Telegram 群组消息',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
9
|
+
message_id: z.string().describe('消息 ID'),
|
|
10
|
+
}),
|
|
11
|
+
adapter: 'telegram',
|
|
12
|
+
tags: ['telegram'],
|
|
13
|
+
permissions: [platformPermit('pin_messages')],
|
|
14
|
+
async execute({ chat_id, message_id }: { chat_id: string; message_id: string }, context) {
|
|
15
|
+
const endpoint = context.$client;
|
|
16
|
+
const success = await endpoint.pinMessage(Number(chat_id), Number(message_id));
|
|
17
|
+
return { success, message: success ? '消息已置顶' : '操作失败' };
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export default defineAgentTool<{ chat_id: string; message_id: string; reaction: string }>({
|
|
5
|
+
description: '对 Telegram 消息添加表情反应',
|
|
6
|
+
inputSchema: z.object({
|
|
7
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
8
|
+
message_id: z.string().describe('消息 ID'),
|
|
9
|
+
reaction: z.string().describe('反应表情(如 👍、❤️、🔥)'),
|
|
10
|
+
}),
|
|
11
|
+
adapter: 'telegram',
|
|
12
|
+
tags: ['telegram'],
|
|
13
|
+
async execute({ chat_id, message_id, reaction }: { chat_id: string; message_id: string; reaction: string }, context) {
|
|
14
|
+
const endpoint = context.$client;
|
|
15
|
+
const success = await endpoint.setMessageReaction(Number(chat_id), Number(message_id), reaction);
|
|
16
|
+
return { success, message: success ? `已添加反应 ${reaction}` : '操作失败' };
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export default defineAgentTool<{ chat_id: string; question: string; options: string; is_anonymous?: boolean; allows_multiple?: boolean }>({
|
|
5
|
+
description: '在 Telegram 群组中发起投票',
|
|
6
|
+
inputSchema: z.object({
|
|
7
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
8
|
+
question: z.string().describe('投票问题'),
|
|
9
|
+
options: z.string().describe('选项 JSON 数组,如 ["A","B","C"]'),
|
|
10
|
+
is_anonymous: z.boolean().optional().describe('是否匿名投票,默认 true'),
|
|
11
|
+
allows_multiple: z.boolean().optional().describe('是否允许多选,默认 false'),
|
|
12
|
+
}),
|
|
13
|
+
adapter: 'telegram',
|
|
14
|
+
tags: ['telegram'],
|
|
15
|
+
async execute({ chat_id, question, options, is_anonymous, allows_multiple }: { chat_id: string; question: string; options: string; is_anonymous?: boolean; allows_multiple?: boolean }, context) {
|
|
16
|
+
const endpoint = context.$client;
|
|
17
|
+
let optList: string[];
|
|
18
|
+
try {
|
|
19
|
+
optList = JSON.parse(options);
|
|
20
|
+
} catch {
|
|
21
|
+
return { success: false, message: 'options 格式错误,应为 JSON 数组' };
|
|
22
|
+
}
|
|
23
|
+
if (!Array.isArray(optList) || optList.length < 2) {
|
|
24
|
+
return { success: false, message: '至少需要 2 个选项' };
|
|
25
|
+
}
|
|
26
|
+
const result = await endpoint.sendPoll(
|
|
27
|
+
Number(chat_id), question, optList,
|
|
28
|
+
is_anonymous ?? true, allows_multiple ?? false,
|
|
29
|
+
);
|
|
30
|
+
return { success: true, message_id: result.message_id, message: '投票已发送' };
|
|
31
|
+
},
|
|
32
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export default defineAgentTool<{ chat_id: string; sticker: string }>({
|
|
5
|
+
description: '发送 Telegram 贴纸',
|
|
6
|
+
inputSchema: z.object({
|
|
7
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
8
|
+
sticker: z.string().describe('贴纸 file_id 或 URL'),
|
|
9
|
+
}),
|
|
10
|
+
adapter: 'telegram',
|
|
11
|
+
tags: ['telegram'],
|
|
12
|
+
async execute({ chat_id, sticker }: { chat_id: string; sticker: string }, context) {
|
|
13
|
+
const endpoint = context.$client;
|
|
14
|
+
const result = await endpoint.sendStickerMessage(Number(chat_id), sticker);
|
|
15
|
+
return { success: true, message_id: result.message_id, message: '贴纸已发送' };
|
|
16
|
+
},
|
|
17
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export default defineAgentTool<{ chat_id: string; description: string }>({
|
|
5
|
+
description: '设置 Telegram 群组描述',
|
|
6
|
+
inputSchema: z.object({
|
|
7
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
8
|
+
description: z.string().describe('群描述文字'),
|
|
9
|
+
}),
|
|
10
|
+
adapter: 'telegram',
|
|
11
|
+
tags: ['telegram'],
|
|
12
|
+
async execute({ chat_id, description }: { chat_id: string; description: string }, context) {
|
|
13
|
+
const endpoint = context.$client;
|
|
14
|
+
const success = await endpoint.setChatDescription(Number(chat_id), description);
|
|
15
|
+
return { success, message: success ? '群描述已更新' : '操作失败' };
|
|
16
|
+
},
|
|
17
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ chat_id: string; can_send_messages?: boolean; can_send_photos?: boolean; can_send_videos?: boolean; can_send_polls?: boolean; can_send_other_messages?: boolean; can_add_web_page_previews?: boolean; can_change_info?: boolean; can_invite_users?: boolean; can_pin_messages?: boolean }>({
|
|
6
|
+
description: '设置 Telegram 群组的默认成员权限',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
9
|
+
can_send_messages: z.boolean().optional().describe('是否可以发消息'),
|
|
10
|
+
can_send_photos: z.boolean().optional().describe('是否可以发图片'),
|
|
11
|
+
can_send_videos: z.boolean().optional().describe('是否可以发视频'),
|
|
12
|
+
can_send_polls: z.boolean().optional().describe('是否可以发投票'),
|
|
13
|
+
can_send_other_messages: z.boolean().optional().describe('是否可以发贴纸/GIF等'),
|
|
14
|
+
can_add_web_page_previews: z.boolean().optional().describe('是否可以添加网页预览'),
|
|
15
|
+
can_change_info: z.boolean().optional().describe('是否可以改群信息'),
|
|
16
|
+
can_invite_users: z.boolean().optional().describe('是否可以邀请用户'),
|
|
17
|
+
can_pin_messages: z.boolean().optional().describe('是否可以置顶消息'),
|
|
18
|
+
}),
|
|
19
|
+
adapter: 'telegram',
|
|
20
|
+
tags: ['telegram'],
|
|
21
|
+
permissions: [platformPermit('manage_chat')],
|
|
22
|
+
async execute({ chat_id, ...perms }: { chat_id: string; can_send_messages?: boolean; can_send_photos?: boolean; can_send_videos?: boolean; can_send_polls?: boolean; can_send_other_messages?: boolean; can_add_web_page_previews?: boolean; can_change_info?: boolean; can_invite_users?: boolean; can_pin_messages?: boolean }, context) {
|
|
23
|
+
const endpoint = context.$client;
|
|
24
|
+
const permissions: Record<string, boolean> = {};
|
|
25
|
+
for (const [k, v] of Object.entries(perms)) {
|
|
26
|
+
if (typeof v === 'boolean') permissions[k] = v;
|
|
27
|
+
}
|
|
28
|
+
const success = await endpoint.setChatPermissionsAll(Number(chat_id), permissions);
|
|
29
|
+
return { success, message: success ? '群权限已更新' : '操作失败' };
|
|
30
|
+
},
|
|
31
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ chat_id: string; message_id?: string }>({
|
|
6
|
+
description: '取消置顶 Telegram 群组消息',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
9
|
+
message_id: z.string().optional().describe('消息 ID(可选,不提供则取消所有置顶)'),
|
|
10
|
+
}),
|
|
11
|
+
adapter: 'telegram',
|
|
12
|
+
tags: ['telegram'],
|
|
13
|
+
permissions: [platformPermit('pin_messages')],
|
|
14
|
+
async execute({ chat_id, message_id }: { chat_id: string; message_id?: string }, context) {
|
|
15
|
+
const endpoint = context.$client;
|
|
16
|
+
const success = await endpoint.unpinMessage(Number(chat_id), message_id ? Number(message_id) : undefined);
|
|
17
|
+
return { success, message: success ? '已取消置顶' : '操作失败' };
|
|
18
|
+
},
|
|
19
|
+
});
|
package/lib/client.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TelegramClient } from './endpoint.js';
|
|
2
|
+
import type { TelegramUpdate } from './protocol.js';
|
|
3
|
+
export type TelegramClientEventMap = Record<string, TelegramUpdate>;
|
|
4
|
+
declare module '@zhin.js/feature-kit' {
|
|
5
|
+
interface AdapterClientRegistry {
|
|
6
|
+
readonly telegram: {
|
|
7
|
+
readonly client: TelegramClient;
|
|
8
|
+
readonly events: TelegramClientEventMap;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare const telegramClient: import("@zhin.js/adapter").EndpointClientToken<TelegramClient, TelegramClientEventMap>;
|
package/lib/client.js
ADDED