@zhin.js/adapter-telegram 5.0.1 → 5.0.3
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 +56 -0
- package/README.md +65 -139
- package/adapters/telegram.ts +27 -0
- package/agent/tools/create_invite.ts +20 -0
- package/agent/tools/list_admins.ts +26 -0
- package/agent/tools/member_count.ts +18 -0
- package/agent/tools/pin_message.ts +21 -0
- package/agent/tools/react.ts +20 -0
- package/agent/tools/send_poll.ts +34 -0
- package/agent/tools/send_sticker.ts +19 -0
- package/agent/tools/set_description.ts +19 -0
- package/agent/tools/set_permissions.ts +33 -0
- package/agent/tools/unpin_message.ts +21 -0
- package/lib/endpoint.d.ts +58 -148
- package/lib/endpoint.js +273 -987
- package/lib/index.d.ts +4 -18
- package/lib/index.js +4 -462
- package/lib/platform-permit.d.ts +1 -2
- package/lib/platform-permit.js +4 -2
- package/lib/polling.d.ts +9 -0
- package/lib/polling.js +52 -0
- package/lib/protocol.d.ts +250 -0
- package/lib/protocol.js +324 -0
- package/lib/telegram-agent-deps.d.ts +28 -0
- package/lib/telegram-agent-deps.js +30 -0
- package/lib/webhook.d.ts +13 -0
- package/lib/webhook.js +45 -0
- package/package.json +46 -33
- package/plugin.ts +13 -0
- package/schema.json +39 -0
- package/src/endpoint.ts +339 -1103
- package/src/index.ts +39 -435
- package/src/platform-permit.ts +1 -2
- package/src/polling.ts +71 -0
- package/src/protocol.ts +573 -0
- package/src/telegram-agent-deps.ts +63 -0
- package/src/webhook.ts +67 -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 -30
- package/dist/index.js +0 -32
- package/lib/adapter.d.ts +0 -21
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -58
- package/lib/adapter.js.map +0 -1
- package/lib/endpoint.d.ts.map +0 -1
- package/lib/endpoint.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/platform-permit.d.ts.map +0 -1
- package/lib/platform-permit.js.map +0 -1
- package/lib/segment-mapper.d.ts +0 -2
- package/lib/segment-mapper.d.ts.map +0 -1
- package/lib/segment-mapper.js +0 -2
- package/lib/segment-mapper.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 -66
- package/src/segment-mapper.ts +0 -1
- package/src/types.ts +0 -32
- /package/{skills/telegram → agent}/PERMITS.md +0 -0
- /package/{skills/telegram/SKILL.md → agent/skills/telegram.md} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,61 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 5.0.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- cc5c94d: 约定式插件运行时迁移(breaking):插件与适配器由 `usePlugin()` / `extends Adapter` 迁移为 `definePlugin` / `defineAdapter` + `plugin.ts` + 约定目录(`adapters/`、`commands/`、`components/`、`tools/` 等)。
|
|
8
|
+
|
|
9
|
+
- 新增约定式运行时包:`@zhin.js/plugin-runtime`、`@zhin.js/adapter`、`@zhin.js/runtime`、`@zhin.js/host-http`(首版 1.0.0 走 init-publish,不在本 changeset 内 bump)。
|
|
10
|
+
- 全部 20 个平台适配器改为约定式 `defineAdapter`,旧 `usePlugin` / `extends Adapter` / `segment-mapper` 生产入口已删除;onebot11 反向 WSS、onebot12 webhook/wss、milky sse/webhook/wss、satori webhook、kook webhook、qq webhook/middleware 等 slice 1 推迟的连接模式已补齐。
|
|
11
|
+
- 游戏 / 工具 / 服务插件同步迁移到约定目录结构。
|
|
12
|
+
- CLI 增加 plugin-runtime host installer(http/database/outbound/schedule/console 等)。
|
|
13
|
+
|
|
14
|
+
后续加固(同批):
|
|
15
|
+
|
|
16
|
+
- CLI:`zhin runtime start --daemon`(pidfile/崩溃拉起/风暴保护),orphan watchdog 防僵尸进程;legacy `zhin dev` / `zhin start` 已移除(含 `zhin restart`),`zhin stop` 兼容新 daemon。
|
|
17
|
+
- 安全:builtin 工具统一走 `security/policy-facade.ts` 的 `runToolPolicies`(声明式策略表,deny 优先);审计日志 close flush + 背压队列;`splitCompoundCommand` 引号感知、`extractCommandName` 去引号堵绕过。
|
|
18
|
+
- 日志:Logger 双堆栈修复、本地时区、`getLogger` 挂树(`setLevel` 递归生效)、第三方库(log4js/discord)桥接、启动人读总结。
|
|
19
|
+
- 结构:`plugins/games/shared` 迁为 `packages/game-kit`(`@zhin.js/game-kit`);死目录 `plugins/adapters/common` 删除。
|
|
20
|
+
- 脚手架:`create-zhin-app` / `zhin new` / scaffold-wizard 生成物改为 Plugin Runtime 形态(minimal-bot 同构,新配置格式)。
|
|
21
|
+
- Console:endpoint.list 真实名称与 phase、schema:get-all 按 instanceKey 映射、db:\* 接 DatabaseHost。
|
|
22
|
+
|
|
23
|
+
注:按仓库发布惯例(见 1bb345dd2),本次 breaking 迁移统一使用 patch,避免 zhin.js 5.0 级联。
|
|
24
|
+
|
|
25
|
+
- 447f3e2: 迁移缺口修复(legacy 功能对齐):
|
|
26
|
+
|
|
27
|
+
- html 段出站规范化:经 `@zhin.js/html-renderer` 渲染为 image 段(sandbox 豁免、无渲染器时降级文本),修复真实平台 `[object Object]`。
|
|
28
|
+
- 群聊 @ 触发 AI:适配器入站标注 `metadata.mentioned`(icqq/qq/slack/onebot11/onebot12/napcat/milky/discord/telegram/kook/dingtalk/satori),`matchAiTrigger` 补齐 ignorePrefixes/respondToAt/respondToPrivate/keywords(默认值与 legacy 对齐)。
|
|
29
|
+
- im_transcripts 全量流水恢复写入(chat_history 工具可用);群聊旁听上下文回迁。
|
|
30
|
+
- `ai.trigger.timeout/thinkingMessage/errorTemplate` 生效;masters/trusted 角色解析对齐 legacy。
|
|
31
|
+
- `Message.sender` 统一为用户 ID(onebot11/12、napcat、milky 原误传显示名);quote_id 经 metadata 接入 AI 引用上下文。
|
|
32
|
+
|
|
33
|
+
- Updated dependencies [16ec4e8]
|
|
34
|
+
- Updated dependencies [cc5c94d]
|
|
35
|
+
- Updated dependencies [447f3e2]
|
|
36
|
+
- @zhin.js/core@1.3.5
|
|
37
|
+
- @zhin.js/agent@1.0.4
|
|
38
|
+
- @zhin.js/host-http@1.0.1
|
|
39
|
+
- zhin.js@4.1.3
|
|
40
|
+
- @zhin.js/logger@1.0.75
|
|
41
|
+
- @zhin.js/plugin-runtime@1.0.1
|
|
42
|
+
- @zhin.js/adapter@1.0.1
|
|
43
|
+
|
|
44
|
+
## 5.0.2
|
|
45
|
+
|
|
46
|
+
### Patch Changes
|
|
47
|
+
|
|
48
|
+
- 872c583: fix: 代码格式优化
|
|
49
|
+
- Updated dependencies [872c583]
|
|
50
|
+
- Updated dependencies [872c583]
|
|
51
|
+
- @zhin.js/agent@1.0.3
|
|
52
|
+
- @zhin.js/client@2.0.5
|
|
53
|
+
- @zhin.js/contract@1.0.3
|
|
54
|
+
- @zhin.js/host-api@2.0.5
|
|
55
|
+
- @zhin.js/host-router@2.0.3
|
|
56
|
+
- @zhin.js/logger@1.0.74
|
|
57
|
+
- zhin.js@4.1.2
|
|
58
|
+
|
|
3
59
|
## 5.0.1
|
|
4
60
|
|
|
5
61
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,179 +1,105 @@
|
|
|
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({ target, payload })` → Bot API(text / media / keyboard)
|
|
11
|
+
- 约定式 `defineAdapter` / `definePlugin`(无需 `usePlugin`)
|
|
12
|
+
- Webhook 模式延期(需 `httpHostToken`);配置 `polling: false` 会明确报错
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
6
15
|
|
|
7
16
|
```bash
|
|
8
17
|
pnpm add @zhin.js/adapter-telegram
|
|
9
18
|
```
|
|
10
19
|
|
|
11
|
-
##
|
|
20
|
+
## Plugin Runtime
|
|
12
21
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
| **host-router** | 不需要;本适配器自行处理 polling / webhook |
|
|
22
|
+
- `@zhin.js/adapter` — 约定式 `adapters/telegram.ts`(`defineAdapter`)
|
|
23
|
+
- `@zhin.js/core` — `messageGatewayToken` 入站/出站
|
|
24
|
+
- `@zhin.js/plugin-runtime` — `plugin.ts`(`definePlugin`)
|
|
25
|
+
- 配置经插件 `schema.json` 落到 `plugins.<instanceKey>`
|
|
26
|
+
- **无需** `@zhin.js/host-http`(polling 路径)
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
入站:`gateway.receive({ adapter, target: chatId, content: text, sender, metadata })`
|
|
29
|
+
出站:`send({ target, payload })` → Telegram Bot API
|
|
21
30
|
|
|
22
|
-
|
|
31
|
+
### 平台权限(platform permit)
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
- "@zhin.js/adapter-telegram"
|
|
33
|
+
- sender role 已恢复:群消息入站时经 `getChatMember`(60s 缓存)解析,写入 `metadata.senderRole` / `metadata.senderPermissions`。
|
|
34
|
+
- `plugin.ts` 在 generation setup 注册 checker,并在 dispose 注销;Plugin Runtime CapabilityIngress 与 ToolSystem 统一经 Core `canAccessTool()` 消费 `permissions`。
|
|
27
35
|
|
|
28
|
-
|
|
29
|
-
- context: telegram
|
|
30
|
-
name: my-telegram-bot
|
|
31
|
-
token: "${TELEGRAM_TOKEN}"
|
|
32
|
-
polling: true
|
|
33
|
-
```
|
|
36
|
+
## 前置条件
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
| 要求 | 说明 |
|
|
39
|
+
|------|------|
|
|
40
|
+
| **Bot Token** | 通过 [@BotFather](https://t.me/botfather) 创建并获取 Token |
|
|
41
|
+
| **Polling(默认)** | 本地/生产均可;主动拉取更新,无需公网 HTTPS |
|
|
42
|
+
| **网络** | 出站可访问 `api.telegram.org` |
|
|
43
|
+
| **host-http** | Polling **不需要**;Webhook 延期至下一棒 |
|
|
36
44
|
|
|
37
|
-
|
|
45
|
+
必填字段:`token`。
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
## 最小配置
|
|
40
48
|
|
|
41
49
|
```yaml
|
|
50
|
+
# zhin.config.yml(Plugin Runtime)
|
|
42
51
|
plugins:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
endpoints:
|
|
46
|
-
- context: telegram
|
|
52
|
+
telegram:
|
|
47
53
|
name: my-telegram-bot
|
|
48
|
-
token:
|
|
49
|
-
polling: true
|
|
54
|
+
token: ${TELEGRAM_TOKEN}
|
|
55
|
+
# polling: true # 默认
|
|
50
56
|
```
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
设置 `polling: false` 并配置 `webhook` 对象;Telegraf 在 `webhook.port` 上启动 HTTPS 服务:
|
|
55
|
-
|
|
56
|
-
```yaml
|
|
57
|
-
endpoints:
|
|
58
|
-
- context: telegram
|
|
59
|
-
name: my-telegram-bot
|
|
60
|
-
token: "${TELEGRAM_TOKEN}"
|
|
61
|
-
polling: false
|
|
62
|
-
webhook:
|
|
63
|
-
domain: https://bot.example.com
|
|
64
|
-
path: /telegram-webhook
|
|
65
|
-
port: 8443
|
|
66
|
-
```
|
|
58
|
+
根插件 `zhin.plugins`(或项目图)需引用 `@zhin.js/adapter-telegram`(`instanceKey: telegram`)。
|
|
67
59
|
|
|
68
|
-
|
|
60
|
+
## 环境变量
|
|
69
61
|
|
|
70
|
-
|
|
|
62
|
+
| 变量 | 说明 |
|
|
71
63
|
|------|------|
|
|
72
|
-
|
|
|
73
|
-
|
|
|
74
|
-
| **Bot Token** | 通过 [@BotFather](https://t.me/botfather) 获取 |
|
|
75
|
-
| **无需 host-router** | 本适配器由 Telegraf 自行监听 webhook,不依赖 `@zhin.js/host-router` |
|
|
76
|
-
|
|
77
|
-
> 本地开发建议先用 **polling**;上线后再切 webhook 并在 BotFather 或 launch 时注册 webhook URL。
|
|
78
|
-
|
|
79
|
-
TypeScript 等价配置:
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
import { defineConfig } from 'zhin.js'
|
|
83
|
-
|
|
84
|
-
export default defineConfig({
|
|
85
|
-
endpoints: [
|
|
86
|
-
{
|
|
87
|
-
name: 'my-telegram-bot',
|
|
88
|
-
context: 'telegram',
|
|
89
|
-
token: 'YOUR_BOT_TOKEN',
|
|
90
|
-
polling: true,
|
|
91
|
-
// polling: false,
|
|
92
|
-
// webhook: { domain: 'https://yourdomain.com', path: '/telegram-webhook', port: 8443 },
|
|
93
|
-
}
|
|
94
|
-
]
|
|
95
|
-
})
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Features
|
|
99
|
-
|
|
100
|
-
- ✅ Send and receive text messages
|
|
101
|
-
- ✅ Support for rich media (images, videos, audio, documents)
|
|
102
|
-
- ✅ Message formatting (bold, italic, code, links)
|
|
103
|
-
- ✅ Reply to messages
|
|
104
|
-
- ✅ Mentions (@username)
|
|
105
|
-
- ✅ Stickers and locations
|
|
106
|
-
- ✅ Callback queries (inline buttons)
|
|
107
|
-
- ✅ Long polling and webhook modes
|
|
108
|
-
- ✅ Private and group chats
|
|
64
|
+
| `TELEGRAM_TOKEN` / `TELEGRAM_BOT_TOKEN` | Bot Token |
|
|
65
|
+
| `TELEGRAM_BOT_NAME` | 可选,默认 endpoint 名 |
|
|
109
66
|
|
|
110
|
-
##
|
|
67
|
+
## Webhook(延期)
|
|
111
68
|
|
|
112
|
-
|
|
113
|
-
2. Send `/newbot` and follow the instructions
|
|
114
|
-
3. Copy the bot token provided
|
|
115
|
-
4. Add the token to your configuration
|
|
69
|
+
`polling: false` + `webhook` 目前会抛出明确错误:
|
|
116
70
|
|
|
117
|
-
|
|
71
|
+
> Telegram webhook mode is deferred until httpHostToken wiring; use polling: true (default) for now
|
|
118
72
|
|
|
119
|
-
|
|
73
|
+
下一棒将用 `httpHostToken` 注册 POST 路由,不再使用 Telegraf 自建监听或 legacy host-router。
|
|
120
74
|
|
|
121
|
-
|
|
122
|
-
import { usePlugin, MessageCommand } from 'zhin.js'
|
|
75
|
+
## 消息类型映射
|
|
123
76
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
addCommand(new MessageCommand('photo')
|
|
137
|
-
.action(async (message) => {
|
|
138
|
-
return [
|
|
139
|
-
{ type: 'image', data: { url: 'https://example.com/photo.jpg' } },
|
|
140
|
-
{ type: 'text', data: { text: 'Check out this photo!' } }
|
|
141
|
-
]
|
|
142
|
-
})
|
|
143
|
-
)
|
|
144
|
-
```
|
|
77
|
+
| Telegram | 入站 content(文本摘要) | 出站 wire |
|
|
78
|
+
|----------|--------------------------|-----------|
|
|
79
|
+
| text | 原文 | sendMessage |
|
|
80
|
+
| photo | `[image]` / caption | sendPhoto(`file_id` / `url`) |
|
|
81
|
+
| video | `[video]` | sendVideo |
|
|
82
|
+
| audio / voice | `[audio]` / `[voice]` | sendAudio / sendVoice |
|
|
83
|
+
| document | `[file: name]` | sendDocument |
|
|
84
|
+
| sticker | `[sticker: …]` | sendSticker |
|
|
85
|
+
| location | `[location: lat,lon]` | sendLocation |
|
|
86
|
+
| callback_query | `[action: data]` | — |
|
|
145
87
|
|
|
146
|
-
|
|
88
|
+
## AI tools
|
|
147
89
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
})
|
|
153
|
-
)
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## Telegram-Specific Features
|
|
157
|
-
|
|
158
|
-
### Callback Queries
|
|
159
|
-
|
|
160
|
-
The adapter automatically handles callback queries (from inline keyboards) as special messages.
|
|
161
|
-
|
|
162
|
-
### File Handling
|
|
163
|
-
|
|
164
|
-
You can send files using:
|
|
165
|
-
- `file_id` (from received messages)
|
|
166
|
-
- URL
|
|
167
|
-
- Local file path
|
|
90
|
+
| Kind | Path |
|
|
91
|
+
|------|------|
|
|
92
|
+
| Platform tools (10) | `agent/tools/`(invite / pin / admins / sticker / poll 等) |
|
|
93
|
+
| Skill doc | `agent/skills/telegram.md` |
|
|
168
94
|
|
|
169
|
-
##
|
|
95
|
+
## 故障排查
|
|
170
96
|
|
|
171
97
|
| 现象 | 排查 |
|
|
172
98
|
|------|------|
|
|
173
|
-
|
|
|
174
|
-
| Polling 报错 |
|
|
175
|
-
| Webhook
|
|
176
|
-
| 发送失败 | Token
|
|
99
|
+
| 收不到消息 | Token 是否正确;进程已 `open()`;同一 Token 勿多进程同时 polling |
|
|
100
|
+
| Polling 报错 | 检查能否访问 `api.telegram.org`;查看日志 `op: poll` |
|
|
101
|
+
| Webhook 配置报错 | 当前仅支持 polling;去掉 `polling: false` |
|
|
102
|
+
| 发送失败 | Token 是否被撤销;查看 Bot API 错误描述 |
|
|
177
103
|
|
|
178
104
|
## Documentation
|
|
179
105
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convention entry: discover `adapters/telegram.ts` → defineAdapter.
|
|
3
|
+
*/
|
|
4
|
+
import { defineAdapter } from '@zhin.js/adapter';
|
|
5
|
+
import { messageGatewayToken } from '@zhin.js/core/runtime';
|
|
6
|
+
import { httpHostToken } from '@zhin.js/host-http';
|
|
7
|
+
import { TelegramEndpoint } from '../src/endpoint.js';
|
|
8
|
+
import {
|
|
9
|
+
resolveTelegramConfig,
|
|
10
|
+
type TelegramAdapterConfig,
|
|
11
|
+
} from '../src/protocol.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
|
+
create(context) {
|
|
19
|
+
const config = resolveTelegramConfig(context.config);
|
|
20
|
+
return new TelegramEndpoint({
|
|
21
|
+
id: context.id,
|
|
22
|
+
gateway: context.use(messageGatewayToken),
|
|
23
|
+
config,
|
|
24
|
+
http: config.mode === 'webhook' ? context.use(httpHostToken) : undefined,
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
5
|
+
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string }>({
|
|
7
|
+
description: '创建 Telegram 群组邀请链接',
|
|
8
|
+
inputSchema: z.object({
|
|
9
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
10
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
11
|
+
}),
|
|
12
|
+
platforms: ['telegram'],
|
|
13
|
+
tags: ['telegram'],
|
|
14
|
+
permissions: [platformPermit('chat_administrator')],
|
|
15
|
+
async execute({ endpoint_id, chat_id }: { endpoint_id: string; chat_id: string }) {
|
|
16
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
17
|
+
const link = await endpoint.createInviteLink(Number(chat_id));
|
|
18
|
+
return { invite_link: link, message: `邀请链接: ${link}` };
|
|
19
|
+
},
|
|
20
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string }>({
|
|
6
|
+
description: '获取 Telegram 群组管理员列表',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
9
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
10
|
+
}),
|
|
11
|
+
platforms: ['telegram'],
|
|
12
|
+
tags: ['telegram'],
|
|
13
|
+
async execute({ endpoint_id, chat_id }: { endpoint_id: string; chat_id: string }) {
|
|
14
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
15
|
+
const admins = await endpoint.getChatAdmins(Number(chat_id));
|
|
16
|
+
return {
|
|
17
|
+
admins: admins.map((a: { user: { id: number; username?: string; first_name?: string }; status: string }) => ({
|
|
18
|
+
user_id: a.user.id,
|
|
19
|
+
username: a.user.username,
|
|
20
|
+
first_name: a.user.first_name,
|
|
21
|
+
status: a.status,
|
|
22
|
+
})),
|
|
23
|
+
count: admins.length,
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string }>({
|
|
6
|
+
description: '获取 Telegram 群组成员数量',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
9
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
10
|
+
}),
|
|
11
|
+
platforms: ['telegram'],
|
|
12
|
+
tags: ['telegram'],
|
|
13
|
+
async execute({ endpoint_id, chat_id }: { endpoint_id: string; chat_id: string }) {
|
|
14
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
15
|
+
const count = await endpoint.getChatMemberCount(Number(chat_id));
|
|
16
|
+
return { count, message: `群组共有 ${count} 名成员` };
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
5
|
+
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string; message_id: string }>({
|
|
7
|
+
description: '置顶 Telegram 群组消息',
|
|
8
|
+
inputSchema: z.object({
|
|
9
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
10
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
11
|
+
message_id: z.string().describe('消息 ID'),
|
|
12
|
+
}),
|
|
13
|
+
platforms: ['telegram'],
|
|
14
|
+
tags: ['telegram'],
|
|
15
|
+
permissions: [platformPermit('pin_messages')],
|
|
16
|
+
async execute({ endpoint_id, chat_id, message_id }: { endpoint_id: string; chat_id: string; message_id: string }) {
|
|
17
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
18
|
+
const success = await endpoint.pinMessage(Number(chat_id), Number(message_id));
|
|
19
|
+
return { success, message: success ? '消息已置顶' : '操作失败' };
|
|
20
|
+
},
|
|
21
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string; message_id: string; reaction: string }>({
|
|
6
|
+
description: '对 Telegram 消息添加表情反应',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
9
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
10
|
+
message_id: z.string().describe('消息 ID'),
|
|
11
|
+
reaction: z.string().describe('反应表情(如 👍、❤️、🔥)'),
|
|
12
|
+
}),
|
|
13
|
+
platforms: ['telegram'],
|
|
14
|
+
tags: ['telegram'],
|
|
15
|
+
async execute({ endpoint_id, chat_id, message_id, reaction }: { endpoint_id: string; chat_id: string; message_id: string; reaction: string }) {
|
|
16
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
17
|
+
const success = await endpoint.setMessageReaction(Number(chat_id), Number(message_id), reaction);
|
|
18
|
+
return { success, message: success ? `已添加反应 ${reaction}` : '操作失败' };
|
|
19
|
+
},
|
|
20
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string; question: string; options: string; is_anonymous?: boolean; allows_multiple?: boolean }>({
|
|
6
|
+
description: '在 Telegram 群组中发起投票',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
9
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
10
|
+
question: z.string().describe('投票问题'),
|
|
11
|
+
options: z.string().describe('选项 JSON 数组,如 ["A","B","C"]'),
|
|
12
|
+
is_anonymous: z.boolean().optional().describe('是否匿名投票,默认 true'),
|
|
13
|
+
allows_multiple: z.boolean().optional().describe('是否允许多选,默认 false'),
|
|
14
|
+
}),
|
|
15
|
+
platforms: ['telegram'],
|
|
16
|
+
tags: ['telegram'],
|
|
17
|
+
async execute({ endpoint_id, chat_id, question, options, is_anonymous, allows_multiple }: { endpoint_id: string; chat_id: string; question: string; options: string; is_anonymous?: boolean; allows_multiple?: boolean }) {
|
|
18
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
19
|
+
let optList: string[];
|
|
20
|
+
try {
|
|
21
|
+
optList = JSON.parse(options);
|
|
22
|
+
} catch {
|
|
23
|
+
return { success: false, message: 'options 格式错误,应为 JSON 数组' };
|
|
24
|
+
}
|
|
25
|
+
if (!Array.isArray(optList) || optList.length < 2) {
|
|
26
|
+
return { success: false, message: '至少需要 2 个选项' };
|
|
27
|
+
}
|
|
28
|
+
const result = await endpoint.sendPoll(
|
|
29
|
+
Number(chat_id), question, optList,
|
|
30
|
+
is_anonymous ?? true, allows_multiple ?? false,
|
|
31
|
+
);
|
|
32
|
+
return { success: true, message_id: result.message_id, message: '投票已发送' };
|
|
33
|
+
},
|
|
34
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string; sticker: string }>({
|
|
6
|
+
description: '发送 Telegram 贴纸',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
9
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
10
|
+
sticker: z.string().describe('贴纸 file_id 或 URL'),
|
|
11
|
+
}),
|
|
12
|
+
platforms: ['telegram'],
|
|
13
|
+
tags: ['telegram'],
|
|
14
|
+
async execute({ endpoint_id, chat_id, sticker }: { endpoint_id: string; chat_id: string; sticker: string }) {
|
|
15
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
16
|
+
const result = await endpoint.sendStickerMessage(Number(chat_id), sticker);
|
|
17
|
+
return { success: true, message_id: result.message_id, message: '贴纸已发送' };
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
4
|
+
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string; description: string }>({
|
|
6
|
+
description: '设置 Telegram 群组描述',
|
|
7
|
+
inputSchema: z.object({
|
|
8
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
9
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
10
|
+
description: z.string().describe('群描述文字'),
|
|
11
|
+
}),
|
|
12
|
+
platforms: ['telegram'],
|
|
13
|
+
tags: ['telegram'],
|
|
14
|
+
async execute({ endpoint_id, chat_id, description }: { endpoint_id: string; chat_id: string; description: string }) {
|
|
15
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
16
|
+
const success = await endpoint.setChatDescription(Number(chat_id), description);
|
|
17
|
+
return { success, message: success ? '群描述已更新' : '操作失败' };
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
5
|
+
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; 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 }>({
|
|
7
|
+
description: '设置 Telegram 群组的默认成员权限',
|
|
8
|
+
inputSchema: z.object({
|
|
9
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
10
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
11
|
+
can_send_messages: z.boolean().optional().describe('是否可以发消息'),
|
|
12
|
+
can_send_photos: z.boolean().optional().describe('是否可以发图片'),
|
|
13
|
+
can_send_videos: z.boolean().optional().describe('是否可以发视频'),
|
|
14
|
+
can_send_polls: z.boolean().optional().describe('是否可以发投票'),
|
|
15
|
+
can_send_other_messages: z.boolean().optional().describe('是否可以发贴纸/GIF等'),
|
|
16
|
+
can_add_web_page_previews: z.boolean().optional().describe('是否可以添加网页预览'),
|
|
17
|
+
can_change_info: z.boolean().optional().describe('是否可以改群信息'),
|
|
18
|
+
can_invite_users: z.boolean().optional().describe('是否可以邀请用户'),
|
|
19
|
+
can_pin_messages: z.boolean().optional().describe('是否可以置顶消息'),
|
|
20
|
+
}),
|
|
21
|
+
platforms: ['telegram'],
|
|
22
|
+
tags: ['telegram'],
|
|
23
|
+
permissions: [platformPermit('manage_chat')],
|
|
24
|
+
async execute({ endpoint_id, chat_id, ...perms }: { endpoint_id: string; 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 }) {
|
|
25
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
26
|
+
const permissions: Record<string, boolean> = {};
|
|
27
|
+
for (const [k, v] of Object.entries(perms)) {
|
|
28
|
+
if (typeof v === 'boolean') permissions[k] = v;
|
|
29
|
+
}
|
|
30
|
+
const success = await endpoint.setChatPermissionsAll(Number(chat_id), permissions);
|
|
31
|
+
return { success, message: success ? '群权限已更新' : '操作失败' };
|
|
32
|
+
},
|
|
33
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
|
+
import { getTelegramAgentDeps } from '../../src/telegram-agent-deps.js';
|
|
5
|
+
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; chat_id: string; message_id?: string }>({
|
|
7
|
+
description: '取消置顶 Telegram 群组消息',
|
|
8
|
+
inputSchema: z.object({
|
|
9
|
+
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
10
|
+
chat_id: z.string().describe('聊天 ID'),
|
|
11
|
+
message_id: z.string().optional().describe('消息 ID(可选,不提供则取消所有置顶)'),
|
|
12
|
+
}),
|
|
13
|
+
platforms: ['telegram'],
|
|
14
|
+
tags: ['telegram'],
|
|
15
|
+
permissions: [platformPermit('pin_messages')],
|
|
16
|
+
async execute({ endpoint_id, chat_id, message_id }: { endpoint_id: string; chat_id: string; message_id?: string }) {
|
|
17
|
+
const endpoint = getTelegramAgentDeps().getEndpoint(endpoint_id);
|
|
18
|
+
const success = await endpoint.unpinMessage(Number(chat_id), message_id ? Number(message_id) : undefined);
|
|
19
|
+
return { success, message: success ? '已取消置顶' : '操作失败' };
|
|
20
|
+
},
|
|
21
|
+
});
|