@zhin.js/adapter-telegram 1.0.45 → 1.0.48
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 +31 -0
- package/lib/adapter.d.ts +0 -4
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +1 -285
- package/lib/adapter.js.map +1 -1
- package/lib/bot.d.ts.map +1 -1
- package/lib/bot.js +1 -0
- package/lib/bot.js.map +1 -1
- package/lib/index.js +260 -2
- package/lib/index.js.map +1 -1
- package/package.json +5 -5
- package/skills/telegram/SKILL.md +60 -3
- package/src/adapter.ts +0 -285
- package/src/bot.ts +1 -0
- package/src/index.ts +266 -2
package/src/adapter.ts
CHANGED
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
import {
|
|
5
5
|
Adapter,
|
|
6
6
|
Plugin,
|
|
7
|
-
createGroupManagementTools,
|
|
8
|
-
type IGroupManagement,
|
|
9
7
|
} from "zhin.js";
|
|
10
8
|
import { TelegramBot } from "./bot.js";
|
|
11
9
|
import type { TelegramBotConfig } from "./types.js";
|
|
@@ -60,290 +58,7 @@ export class TelegramAdapter extends Adapter<TelegramBot> {
|
|
|
60
58
|
// ── 生命周期 ───────────────────────────────────────────────────────
|
|
61
59
|
|
|
62
60
|
async start(): Promise<void> {
|
|
63
|
-
this.registerTelegramPlatformTools();
|
|
64
|
-
const groupTools = createGroupManagementTools(this as unknown as IGroupManagement, this.name);
|
|
65
|
-
groupTools.forEach((t) => this.addTool(t));
|
|
66
61
|
await super.start();
|
|
67
62
|
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 注册 Telegram 平台特有工具(置顶消息等)
|
|
71
|
-
*/
|
|
72
|
-
private registerTelegramPlatformTools(): void {
|
|
73
|
-
// 置顶消息工具
|
|
74
|
-
this.addTool({
|
|
75
|
-
name: 'telegram_pin_message',
|
|
76
|
-
description: '置顶 Telegram 群组消息',
|
|
77
|
-
parameters: {
|
|
78
|
-
type: 'object',
|
|
79
|
-
properties: {
|
|
80
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
81
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
82
|
-
message_id: { type: 'number', description: '消息 ID' },
|
|
83
|
-
},
|
|
84
|
-
required: ['bot', 'chat_id', 'message_id'],
|
|
85
|
-
},
|
|
86
|
-
platforms: ['telegram'],
|
|
87
|
-
scopes: ['group'],
|
|
88
|
-
permissionLevel: 'group_admin',
|
|
89
|
-
execute: async (args) => {
|
|
90
|
-
const { bot: botId, chat_id, message_id } = args;
|
|
91
|
-
const bot = this.bots.get(botId);
|
|
92
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
93
|
-
const success = await bot.pinMessage(chat_id, message_id);
|
|
94
|
-
return { success, message: success ? '消息已置顶' : '操作失败' };
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// 取消置顶工具
|
|
99
|
-
this.addTool({
|
|
100
|
-
name: 'telegram_unpin_message',
|
|
101
|
-
description: '取消置顶 Telegram 群组消息',
|
|
102
|
-
parameters: {
|
|
103
|
-
type: 'object',
|
|
104
|
-
properties: {
|
|
105
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
106
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
107
|
-
message_id: { type: 'number', description: '消息 ID(可选,不提供则取消所有置顶)' },
|
|
108
|
-
},
|
|
109
|
-
required: ['bot', 'chat_id'],
|
|
110
|
-
},
|
|
111
|
-
platforms: ['telegram'],
|
|
112
|
-
scopes: ['group'],
|
|
113
|
-
permissionLevel: 'group_admin',
|
|
114
|
-
execute: async (args) => {
|
|
115
|
-
const { bot: botId, chat_id, message_id } = args;
|
|
116
|
-
const bot = this.bots.get(botId);
|
|
117
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
118
|
-
const success = await bot.unpinMessage(chat_id, message_id);
|
|
119
|
-
return { success, message: success ? '已取消置顶' : '操作失败' };
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// 获取管理员列表工具
|
|
124
|
-
this.addTool({
|
|
125
|
-
name: 'telegram_list_admins',
|
|
126
|
-
description: '获取 Telegram 群组管理员列表',
|
|
127
|
-
parameters: {
|
|
128
|
-
type: 'object',
|
|
129
|
-
properties: {
|
|
130
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
131
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
132
|
-
},
|
|
133
|
-
required: ['bot', 'chat_id'],
|
|
134
|
-
},
|
|
135
|
-
platforms: ['telegram'],
|
|
136
|
-
scopes: ['group'],
|
|
137
|
-
permissionLevel: 'user',
|
|
138
|
-
execute: async (args) => {
|
|
139
|
-
const { bot: botId, chat_id } = args;
|
|
140
|
-
const bot = this.bots.get(botId);
|
|
141
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
142
|
-
const admins = await bot.getChatAdmins(chat_id);
|
|
143
|
-
return {
|
|
144
|
-
admins: admins.map(a => ({
|
|
145
|
-
user_id: a.user.id,
|
|
146
|
-
username: a.user.username,
|
|
147
|
-
first_name: a.user.first_name,
|
|
148
|
-
status: a.status,
|
|
149
|
-
})),
|
|
150
|
-
count: admins.length,
|
|
151
|
-
};
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// 获取成员数量工具
|
|
156
|
-
this.addTool({
|
|
157
|
-
name: 'telegram_member_count',
|
|
158
|
-
description: '获取 Telegram 群组成员数量',
|
|
159
|
-
parameters: {
|
|
160
|
-
type: 'object',
|
|
161
|
-
properties: {
|
|
162
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
163
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
164
|
-
},
|
|
165
|
-
required: ['bot', 'chat_id'],
|
|
166
|
-
},
|
|
167
|
-
platforms: ['telegram'],
|
|
168
|
-
scopes: ['group'],
|
|
169
|
-
permissionLevel: 'user',
|
|
170
|
-
execute: async (args) => {
|
|
171
|
-
const { bot: botId, chat_id } = args;
|
|
172
|
-
const bot = this.bots.get(botId);
|
|
173
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
174
|
-
const count = await bot.getChatMemberCount(chat_id);
|
|
175
|
-
return { count, message: `群组共有 ${count} 名成员` };
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// 创建邀请链接工具
|
|
180
|
-
this.addTool({
|
|
181
|
-
name: 'telegram_create_invite',
|
|
182
|
-
description: '创建 Telegram 群组邀请链接',
|
|
183
|
-
parameters: {
|
|
184
|
-
type: 'object',
|
|
185
|
-
properties: {
|
|
186
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
187
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
188
|
-
},
|
|
189
|
-
required: ['bot', 'chat_id'],
|
|
190
|
-
},
|
|
191
|
-
platforms: ['telegram'],
|
|
192
|
-
scopes: ['group'],
|
|
193
|
-
permissionLevel: 'group_admin',
|
|
194
|
-
execute: async (args) => {
|
|
195
|
-
const { bot: botId, chat_id } = args;
|
|
196
|
-
const bot = this.bots.get(botId);
|
|
197
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
198
|
-
const link = await bot.createInviteLink(chat_id);
|
|
199
|
-
return { invite_link: link, message: `邀请链接: ${link}` };
|
|
200
|
-
},
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
// 发起投票
|
|
204
|
-
this.addTool({
|
|
205
|
-
name: 'telegram_send_poll',
|
|
206
|
-
description: '在 Telegram 群组中发起投票',
|
|
207
|
-
parameters: {
|
|
208
|
-
type: 'object',
|
|
209
|
-
properties: {
|
|
210
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
211
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
212
|
-
question: { type: 'string', description: '投票问题' },
|
|
213
|
-
options: { type: 'string', description: '选项,用逗号分隔' },
|
|
214
|
-
is_anonymous: { type: 'boolean', description: '是否匿名投票,默认 true' },
|
|
215
|
-
allows_multiple: { type: 'boolean', description: '是否允许多选,默认 false' },
|
|
216
|
-
},
|
|
217
|
-
required: ['bot', 'chat_id', 'question', 'options'],
|
|
218
|
-
},
|
|
219
|
-
platforms: ['telegram'],
|
|
220
|
-
scopes: ['group'],
|
|
221
|
-
permissionLevel: 'user',
|
|
222
|
-
execute: async (args) => {
|
|
223
|
-
const { bot: botId, chat_id, question, options, is_anonymous = true, allows_multiple = false } = args;
|
|
224
|
-
const bot = this.bots.get(botId);
|
|
225
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
226
|
-
const optList = options.split(',').map((o: string) => o.trim()).filter(Boolean);
|
|
227
|
-
if (optList.length < 2) return { success: false, message: '至少需要 2 个选项' };
|
|
228
|
-
const result = await bot.sendPoll(chat_id, question, optList, is_anonymous, allows_multiple);
|
|
229
|
-
return { success: true, message_id: result.message_id, message: '投票已发送' };
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
// 消息表情反应
|
|
234
|
-
this.addTool({
|
|
235
|
-
name: 'telegram_react',
|
|
236
|
-
description: '对 Telegram 消息添加表情反应',
|
|
237
|
-
parameters: {
|
|
238
|
-
type: 'object',
|
|
239
|
-
properties: {
|
|
240
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
241
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
242
|
-
message_id: { type: 'number', description: '消息 ID' },
|
|
243
|
-
reaction: { type: 'string', description: '反应表情(如 👍、❤️、🔥)' },
|
|
244
|
-
},
|
|
245
|
-
required: ['bot', 'chat_id', 'message_id', 'reaction'],
|
|
246
|
-
},
|
|
247
|
-
platforms: ['telegram'],
|
|
248
|
-
scopes: ['group', 'private'],
|
|
249
|
-
permissionLevel: 'user',
|
|
250
|
-
execute: async (args) => {
|
|
251
|
-
const { bot: botId, chat_id, message_id, reaction } = args;
|
|
252
|
-
const bot = this.bots.get(botId);
|
|
253
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
254
|
-
const success = await bot.setMessageReaction(chat_id, message_id, reaction);
|
|
255
|
-
return { success, message: success ? `已添加反应 ${reaction}` : '操作失败' };
|
|
256
|
-
},
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
// 发送贴纸
|
|
260
|
-
this.addTool({
|
|
261
|
-
name: 'telegram_send_sticker',
|
|
262
|
-
description: '发送 Telegram 贴纸',
|
|
263
|
-
parameters: {
|
|
264
|
-
type: 'object',
|
|
265
|
-
properties: {
|
|
266
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
267
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
268
|
-
sticker: { type: 'string', description: '贴纸 file_id 或 URL' },
|
|
269
|
-
},
|
|
270
|
-
required: ['bot', 'chat_id', 'sticker'],
|
|
271
|
-
},
|
|
272
|
-
platforms: ['telegram'],
|
|
273
|
-
scopes: ['group', 'private'],
|
|
274
|
-
permissionLevel: 'user',
|
|
275
|
-
execute: async (args) => {
|
|
276
|
-
const { bot: botId, chat_id, sticker } = args;
|
|
277
|
-
const bot = this.bots.get(botId);
|
|
278
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
279
|
-
const result = await bot.sendStickerMessage(chat_id, sticker);
|
|
280
|
-
return { success: true, message_id: result.message_id, message: '贴纸已发送' };
|
|
281
|
-
},
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
// 设置群权限
|
|
285
|
-
this.addTool({
|
|
286
|
-
name: 'telegram_set_permissions',
|
|
287
|
-
description: '设置 Telegram 群组的默认成员权限',
|
|
288
|
-
parameters: {
|
|
289
|
-
type: 'object',
|
|
290
|
-
properties: {
|
|
291
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
292
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
293
|
-
can_send_messages: { type: 'boolean', description: '是否可以发消息' },
|
|
294
|
-
can_send_photos: { type: 'boolean', description: '是否可以发图片' },
|
|
295
|
-
can_send_videos: { type: 'boolean', description: '是否可以发视频' },
|
|
296
|
-
can_send_polls: { type: 'boolean', description: '是否可以发投票' },
|
|
297
|
-
can_send_other_messages: { type: 'boolean', description: '是否可以发贴纸/GIF等' },
|
|
298
|
-
can_add_web_page_previews: { type: 'boolean', description: '是否可以添加网页预览' },
|
|
299
|
-
can_change_info: { type: 'boolean', description: '是否可以改群信息' },
|
|
300
|
-
can_invite_users: { type: 'boolean', description: '是否可以邀请用户' },
|
|
301
|
-
can_pin_messages: { type: 'boolean', description: '是否可以置顶消息' },
|
|
302
|
-
},
|
|
303
|
-
required: ['bot', 'chat_id'],
|
|
304
|
-
},
|
|
305
|
-
platforms: ['telegram'],
|
|
306
|
-
scopes: ['group'],
|
|
307
|
-
permissionLevel: 'group_admin',
|
|
308
|
-
execute: async (args) => {
|
|
309
|
-
const { bot: botId, chat_id, ...perms } = args;
|
|
310
|
-
const bot = this.bots.get(botId);
|
|
311
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
312
|
-
const permissions: any = {};
|
|
313
|
-
for (const [k, v] of Object.entries(perms)) {
|
|
314
|
-
if (typeof v === 'boolean') permissions[k] = v;
|
|
315
|
-
}
|
|
316
|
-
const success = await bot.setChatPermissionsAll(chat_id, permissions);
|
|
317
|
-
return { success, message: success ? '群权限已更新' : '操作失败' };
|
|
318
|
-
},
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
// 设置群描述
|
|
322
|
-
this.addTool({
|
|
323
|
-
name: 'telegram_set_description',
|
|
324
|
-
description: '设置 Telegram 群组描述',
|
|
325
|
-
parameters: {
|
|
326
|
-
type: 'object',
|
|
327
|
-
properties: {
|
|
328
|
-
bot: { type: 'string', description: 'Bot 名称' },
|
|
329
|
-
chat_id: { type: 'number', description: '聊天 ID' },
|
|
330
|
-
description: { type: 'string', description: '群描述文字' },
|
|
331
|
-
},
|
|
332
|
-
required: ['bot', 'chat_id', 'description'],
|
|
333
|
-
},
|
|
334
|
-
platforms: ['telegram'],
|
|
335
|
-
scopes: ['group'],
|
|
336
|
-
permissionLevel: 'group_admin',
|
|
337
|
-
execute: async (args) => {
|
|
338
|
-
const { bot: botId, chat_id, description } = args;
|
|
339
|
-
const bot = this.bots.get(botId);
|
|
340
|
-
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
341
|
-
const success = await bot.setChatDescription(chat_id, description);
|
|
342
|
-
return { success, message: success ? '群描述已更新' : '操作失败' };
|
|
343
|
-
},
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
this.plugin.logger.debug('已注册 Telegram 平台群组管理工具');
|
|
347
|
-
}
|
|
348
63
|
}
|
|
349
64
|
|
package/src/bot.ts
CHANGED
|
@@ -83,6 +83,7 @@ export class TelegramBot extends Telegraf implements Bot<TelegramBotConfig, Tele
|
|
|
83
83
|
|
|
84
84
|
async $disconnect(): Promise<void> {
|
|
85
85
|
try {
|
|
86
|
+
(this as unknown as import('node:events').EventEmitter).removeAllListeners();
|
|
86
87
|
await this.stop();
|
|
87
88
|
this.$connected = false;
|
|
88
89
|
this.pluginLogger.info(`Telegram bot ${this.$config.name} disconnected`);
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Telegram 适配器入口:类型扩展、导出、注册
|
|
3
3
|
*/
|
|
4
|
-
import { usePlugin, type Plugin, type Context } from "zhin.js";
|
|
4
|
+
import { usePlugin, type Plugin, type Context, type IGroupManagement, createGroupManagementTools, type ToolFeature } from "zhin.js";
|
|
5
5
|
import { TelegramAdapter } from "./adapter.js";
|
|
6
6
|
|
|
7
7
|
declare module "zhin.js" {
|
|
@@ -15,7 +15,7 @@ export { TelegramBot } from "./bot.js";
|
|
|
15
15
|
export { TelegramAdapter } from "./adapter.js";
|
|
16
16
|
|
|
17
17
|
const plugin = usePlugin();
|
|
18
|
-
const { provide } = plugin;
|
|
18
|
+
const { provide, useContext } = plugin;
|
|
19
19
|
|
|
20
20
|
provide({
|
|
21
21
|
name: "telegram",
|
|
@@ -29,3 +29,267 @@ provide({
|
|
|
29
29
|
await adapter.stop();
|
|
30
30
|
},
|
|
31
31
|
} as Context<"telegram">);
|
|
32
|
+
|
|
33
|
+
useContext('tool', 'telegram', (toolService: ToolFeature, telegram: TelegramAdapter) => {
|
|
34
|
+
const groupTools = createGroupManagementTools(
|
|
35
|
+
telegram as unknown as IGroupManagement,
|
|
36
|
+
'telegram',
|
|
37
|
+
);
|
|
38
|
+
const disposers: (() => void)[] = groupTools.map(t => toolService.addTool(t, 'telegram'));
|
|
39
|
+
|
|
40
|
+
disposers.push(toolService.addTool({
|
|
41
|
+
name: 'telegram_pin_message',
|
|
42
|
+
description: '置顶 Telegram 群组消息',
|
|
43
|
+
parameters: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: {
|
|
46
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
47
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
48
|
+
message_id: { type: 'string', description: '消息 ID' },
|
|
49
|
+
},
|
|
50
|
+
required: ['bot', 'chat_id', 'message_id'],
|
|
51
|
+
},
|
|
52
|
+
platforms: ['telegram'],
|
|
53
|
+
tags: ['telegram'],
|
|
54
|
+
execute: async (args: Record<string, any>) => {
|
|
55
|
+
const bot = telegram.bots.get(args.bot);
|
|
56
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
57
|
+
const success = await bot.pinMessage(Number(args.chat_id), Number(args.message_id));
|
|
58
|
+
return { success, message: success ? '消息已置顶' : '操作失败' };
|
|
59
|
+
},
|
|
60
|
+
}, 'telegram'));
|
|
61
|
+
|
|
62
|
+
disposers.push(toolService.addTool({
|
|
63
|
+
name: 'telegram_unpin_message',
|
|
64
|
+
description: '取消置顶 Telegram 群组消息',
|
|
65
|
+
parameters: {
|
|
66
|
+
type: 'object',
|
|
67
|
+
properties: {
|
|
68
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
69
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
70
|
+
message_id: { type: 'string', description: '消息 ID(可选,不提供则取消所有置顶)' },
|
|
71
|
+
},
|
|
72
|
+
required: ['bot', 'chat_id'],
|
|
73
|
+
},
|
|
74
|
+
platforms: ['telegram'],
|
|
75
|
+
tags: ['telegram'],
|
|
76
|
+
execute: async (args: Record<string, any>) => {
|
|
77
|
+
const bot = telegram.bots.get(args.bot);
|
|
78
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
79
|
+
const success = await bot.unpinMessage(Number(args.chat_id), args.message_id ? Number(args.message_id) : undefined);
|
|
80
|
+
return { success, message: success ? '已取消置顶' : '操作失败' };
|
|
81
|
+
},
|
|
82
|
+
}, 'telegram'));
|
|
83
|
+
|
|
84
|
+
disposers.push(toolService.addTool({
|
|
85
|
+
name: 'telegram_list_admins',
|
|
86
|
+
description: '获取 Telegram 群组管理员列表',
|
|
87
|
+
parameters: {
|
|
88
|
+
type: 'object',
|
|
89
|
+
properties: {
|
|
90
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
91
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
92
|
+
},
|
|
93
|
+
required: ['bot', 'chat_id'],
|
|
94
|
+
},
|
|
95
|
+
platforms: ['telegram'],
|
|
96
|
+
tags: ['telegram'],
|
|
97
|
+
execute: async (args: Record<string, any>) => {
|
|
98
|
+
const bot = telegram.bots.get(args.bot);
|
|
99
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
100
|
+
const admins = await bot.getChatAdmins(Number(args.chat_id));
|
|
101
|
+
return {
|
|
102
|
+
admins: admins.map((a: any) => ({
|
|
103
|
+
user_id: a.user.id,
|
|
104
|
+
username: a.user.username,
|
|
105
|
+
first_name: a.user.first_name,
|
|
106
|
+
status: a.status,
|
|
107
|
+
})),
|
|
108
|
+
count: admins.length,
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
}, 'telegram'));
|
|
112
|
+
|
|
113
|
+
disposers.push(toolService.addTool({
|
|
114
|
+
name: 'telegram_member_count',
|
|
115
|
+
description: '获取 Telegram 群组成员数量',
|
|
116
|
+
parameters: {
|
|
117
|
+
type: 'object',
|
|
118
|
+
properties: {
|
|
119
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
120
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
121
|
+
},
|
|
122
|
+
required: ['bot', 'chat_id'],
|
|
123
|
+
},
|
|
124
|
+
platforms: ['telegram'],
|
|
125
|
+
tags: ['telegram'],
|
|
126
|
+
execute: async (args: Record<string, any>) => {
|
|
127
|
+
const bot = telegram.bots.get(args.bot);
|
|
128
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
129
|
+
const count = await bot.getChatMemberCount(Number(args.chat_id));
|
|
130
|
+
return { count, message: `群组共有 ${count} 名成员` };
|
|
131
|
+
},
|
|
132
|
+
}, 'telegram'));
|
|
133
|
+
|
|
134
|
+
disposers.push(toolService.addTool({
|
|
135
|
+
name: 'telegram_create_invite',
|
|
136
|
+
description: '创建 Telegram 群组邀请链接',
|
|
137
|
+
parameters: {
|
|
138
|
+
type: 'object',
|
|
139
|
+
properties: {
|
|
140
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
141
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
142
|
+
},
|
|
143
|
+
required: ['bot', 'chat_id'],
|
|
144
|
+
},
|
|
145
|
+
platforms: ['telegram'],
|
|
146
|
+
tags: ['telegram'],
|
|
147
|
+
execute: async (args: Record<string, any>) => {
|
|
148
|
+
const bot = telegram.bots.get(args.bot);
|
|
149
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
150
|
+
const link = await bot.createInviteLink(Number(args.chat_id));
|
|
151
|
+
return { invite_link: link, message: `邀请链接: ${link}` };
|
|
152
|
+
},
|
|
153
|
+
}, 'telegram'));
|
|
154
|
+
|
|
155
|
+
disposers.push(toolService.addTool({
|
|
156
|
+
name: 'telegram_send_poll',
|
|
157
|
+
description: '在 Telegram 群组中发起投票',
|
|
158
|
+
parameters: {
|
|
159
|
+
type: 'object',
|
|
160
|
+
properties: {
|
|
161
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
162
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
163
|
+
question: { type: 'string', description: '投票问题' },
|
|
164
|
+
options: { type: 'string', description: '选项 JSON 数组,如 ["A","B","C"]' },
|
|
165
|
+
is_anonymous: { type: 'boolean', description: '是否匿名投票,默认 true' },
|
|
166
|
+
allows_multiple: { type: 'boolean', description: '是否允许多选,默认 false' },
|
|
167
|
+
},
|
|
168
|
+
required: ['bot', 'chat_id', 'question', 'options'],
|
|
169
|
+
},
|
|
170
|
+
platforms: ['telegram'],
|
|
171
|
+
tags: ['telegram'],
|
|
172
|
+
execute: async (args: Record<string, any>) => {
|
|
173
|
+
const bot = telegram.bots.get(args.bot);
|
|
174
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
175
|
+
let optList: string[];
|
|
176
|
+
try {
|
|
177
|
+
optList = JSON.parse(args.options);
|
|
178
|
+
} catch {
|
|
179
|
+
return { success: false, message: 'options 格式错误,应为 JSON 数组' };
|
|
180
|
+
}
|
|
181
|
+
if (!Array.isArray(optList) || optList.length < 2) {
|
|
182
|
+
return { success: false, message: '至少需要 2 个选项' };
|
|
183
|
+
}
|
|
184
|
+
const result = await bot.sendPoll(
|
|
185
|
+
Number(args.chat_id), args.question, optList,
|
|
186
|
+
args.is_anonymous ?? true, args.allows_multiple ?? false,
|
|
187
|
+
);
|
|
188
|
+
return { success: true, message_id: result.message_id, message: '投票已发送' };
|
|
189
|
+
},
|
|
190
|
+
}, 'telegram'));
|
|
191
|
+
|
|
192
|
+
disposers.push(toolService.addTool({
|
|
193
|
+
name: 'telegram_react',
|
|
194
|
+
description: '对 Telegram 消息添加表情反应',
|
|
195
|
+
parameters: {
|
|
196
|
+
type: 'object',
|
|
197
|
+
properties: {
|
|
198
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
199
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
200
|
+
message_id: { type: 'string', description: '消息 ID' },
|
|
201
|
+
reaction: { type: 'string', description: '反应表情(如 👍、❤️、🔥)' },
|
|
202
|
+
},
|
|
203
|
+
required: ['bot', 'chat_id', 'message_id', 'reaction'],
|
|
204
|
+
},
|
|
205
|
+
platforms: ['telegram'],
|
|
206
|
+
tags: ['telegram'],
|
|
207
|
+
execute: async (args: Record<string, any>) => {
|
|
208
|
+
const bot = telegram.bots.get(args.bot);
|
|
209
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
210
|
+
const success = await bot.setMessageReaction(Number(args.chat_id), Number(args.message_id), args.reaction);
|
|
211
|
+
return { success, message: success ? `已添加反应 ${args.reaction}` : '操作失败' };
|
|
212
|
+
},
|
|
213
|
+
}, 'telegram'));
|
|
214
|
+
|
|
215
|
+
disposers.push(toolService.addTool({
|
|
216
|
+
name: 'telegram_send_sticker',
|
|
217
|
+
description: '发送 Telegram 贴纸',
|
|
218
|
+
parameters: {
|
|
219
|
+
type: 'object',
|
|
220
|
+
properties: {
|
|
221
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
222
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
223
|
+
sticker: { type: 'string', description: '贴纸 file_id 或 URL' },
|
|
224
|
+
},
|
|
225
|
+
required: ['bot', 'chat_id', 'sticker'],
|
|
226
|
+
},
|
|
227
|
+
platforms: ['telegram'],
|
|
228
|
+
tags: ['telegram'],
|
|
229
|
+
execute: async (args: Record<string, any>) => {
|
|
230
|
+
const bot = telegram.bots.get(args.bot);
|
|
231
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
232
|
+
const result = await bot.sendStickerMessage(Number(args.chat_id), args.sticker);
|
|
233
|
+
return { success: true, message_id: result.message_id, message: '贴纸已发送' };
|
|
234
|
+
},
|
|
235
|
+
}, 'telegram'));
|
|
236
|
+
|
|
237
|
+
disposers.push(toolService.addTool({
|
|
238
|
+
name: 'telegram_set_permissions',
|
|
239
|
+
description: '设置 Telegram 群组的默认成员权限',
|
|
240
|
+
parameters: {
|
|
241
|
+
type: 'object',
|
|
242
|
+
properties: {
|
|
243
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
244
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
245
|
+
can_send_messages: { type: 'boolean', description: '是否可以发消息' },
|
|
246
|
+
can_send_photos: { type: 'boolean', description: '是否可以发图片' },
|
|
247
|
+
can_send_videos: { type: 'boolean', description: '是否可以发视频' },
|
|
248
|
+
can_send_polls: { type: 'boolean', description: '是否可以发投票' },
|
|
249
|
+
can_send_other_messages: { type: 'boolean', description: '是否可以发贴纸/GIF等' },
|
|
250
|
+
can_add_web_page_previews: { type: 'boolean', description: '是否可以添加网页预览' },
|
|
251
|
+
can_change_info: { type: 'boolean', description: '是否可以改群信息' },
|
|
252
|
+
can_invite_users: { type: 'boolean', description: '是否可以邀请用户' },
|
|
253
|
+
can_pin_messages: { type: 'boolean', description: '是否可以置顶消息' },
|
|
254
|
+
},
|
|
255
|
+
required: ['bot', 'chat_id'],
|
|
256
|
+
},
|
|
257
|
+
platforms: ['telegram'],
|
|
258
|
+
tags: ['telegram'],
|
|
259
|
+
execute: async (args: Record<string, any>) => {
|
|
260
|
+
const { bot: botId, chat_id, ...perms } = args;
|
|
261
|
+
const bot = telegram.bots.get(botId);
|
|
262
|
+
if (!bot) throw new Error(`Bot ${botId} 不存在`);
|
|
263
|
+
const permissions: any = {};
|
|
264
|
+
for (const [k, v] of Object.entries(perms)) {
|
|
265
|
+
if (typeof v === 'boolean') permissions[k] = v;
|
|
266
|
+
}
|
|
267
|
+
const success = await bot.setChatPermissionsAll(Number(chat_id), permissions);
|
|
268
|
+
return { success, message: success ? '群权限已更新' : '操作失败' };
|
|
269
|
+
},
|
|
270
|
+
}, 'telegram'));
|
|
271
|
+
|
|
272
|
+
disposers.push(toolService.addTool({
|
|
273
|
+
name: 'telegram_set_description',
|
|
274
|
+
description: '设置 Telegram 群组描述',
|
|
275
|
+
parameters: {
|
|
276
|
+
type: 'object',
|
|
277
|
+
properties: {
|
|
278
|
+
bot: { type: 'string', description: 'Bot 名称' },
|
|
279
|
+
chat_id: { type: 'string', description: '聊天 ID' },
|
|
280
|
+
description: { type: 'string', description: '群描述文字' },
|
|
281
|
+
},
|
|
282
|
+
required: ['bot', 'chat_id', 'description'],
|
|
283
|
+
},
|
|
284
|
+
platforms: ['telegram'],
|
|
285
|
+
tags: ['telegram'],
|
|
286
|
+
execute: async (args: Record<string, any>) => {
|
|
287
|
+
const bot = telegram.bots.get(args.bot);
|
|
288
|
+
if (!bot) throw new Error(`Bot ${args.bot} 不存在`);
|
|
289
|
+
const success = await bot.setChatDescription(Number(args.chat_id), args.description);
|
|
290
|
+
return { success, message: success ? '群描述已更新' : '操作失败' };
|
|
291
|
+
},
|
|
292
|
+
}, 'telegram'));
|
|
293
|
+
|
|
294
|
+
return () => disposers.forEach(d => d());
|
|
295
|
+
});
|