Skip to content

Commit 0d38514

Browse files
BukeLyCopilot
andcommitted
Apply suggestion from @Copilot
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 1fa02af commit 0d38514

2 files changed

Lines changed: 92 additions & 6 deletions

File tree

agent-sdk-client/handler.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,18 @@ def lambda_handler(event: dict, context: Any) -> dict:
280280
'administrator',
281281
):
282282
chat_id = member_update.chat.id
283-
is_ok, error_msg = asyncio.run(
284-
_check_forum_requirements(bot, chat_id)
285-
)
286-
if not is_ok:
287-
asyncio.run(bot.send_message(chat_id=chat_id, text=error_msg))
288-
_send_metric('TopicPrecheck.Failed')
283+
284+
async def _run_topic_precheck():
285+
is_ok, error_msg = await _check_forum_requirements(bot, chat_id)
286+
if not is_ok:
287+
await bot.send_message(chat_id=chat_id, text=error_msg)
288+
logger.warning(
289+
"Forum requirements check failed",
290+
extra={'chat_id': chat_id, 'error_msg': error_msg},
291+
)
292+
_send_metric('TopicPrecheck.Failed')
293+
294+
asyncio.run(_run_topic_precheck())
289295
return {'statusCode': 200}
290296

291297
message = update.message or update.edited_message
@@ -304,6 +310,10 @@ def lambda_handler(event: dict, context: Any) -> dict:
304310
_send_metric('SecurityBlock.UnauthorizedPrivate')
305311
return {'statusCode': 200}
306312

313+
# 群组消息:非 Forum 直接忽略(用户入群时已收到预检提示)
314+
if message.chat.type in ('group', 'supergroup') and not message.chat.is_forum:
315+
return {'statusCode': 200}
316+
307317
cmd = config.get_command(message.text)
308318

309319
# /newchat 特殊处理 - 创建 Topic 后发 SQS

docs/forum-group-security.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Forum 群组安全设计
2+
3+
## 背景
4+
5+
Bot 设计上依赖 Telegram Forum (Topics) 功能,需要确保:
6+
1. Bot 只在满足条件的群组中工作
7+
2. 用户得到清晰的配置指引
8+
9+
## 安全策略
10+
11+
### 1. 用户白名单 (最高优先级)
12+
13+
非白名单用户拉 Bot 进群 → 直接退群,不做任何处理。
14+
15+
```python
16+
if should_leave_group(update, config.user_whitelist):
17+
bot.leave_chat(chat_id)
18+
return
19+
```
20+
21+
### 2. 入群预检
22+
23+
白名单用户拉 Bot 进群时,检查:
24+
- `chat.is_forum` - 群组是否开启 Topics
25+
- `can_manage_topics` - Bot 是否有 Topic 管理权限
26+
27+
预检失败时发送详细配置指引,但不退群(给用户配置时间)。
28+
29+
### 3. 消息过滤 (统一入口)
30+
31+
非 Forum 群组的消息在 producer 入口处直接忽略:
32+
33+
```python
34+
# 群组消息:非 Forum 直接忽略
35+
if message.chat.type in ('group', 'supergroup') and not message.chat.is_forum:
36+
return {'statusCode': 200}
37+
```
38+
39+
**优点**
40+
- 一行代码,所有命令无需单独检查
41+
- 私聊不受影响
42+
- 用户已在入群时收到预检提示
43+
44+
## 处理流程
45+
46+
```
47+
Bot 被添加到群组
48+
49+
┌─────────────────────┐
50+
│ 白名单检查 │
51+
└─────────────────────┘
52+
53+
通过? ──No──→ 退群
54+
↓ Yes
55+
┌─────────────────────┐
56+
│ Topic 预检 │
57+
│ - is_forum? │
58+
│ - can_manage_topics?│
59+
└─────────────────────┘
60+
61+
通过? ──No──→ 发送配置指引
62+
↓ Yes
63+
正常工作
64+
65+
---
66+
67+
收到群组消息
68+
69+
┌─────────────────────┐
70+
│ is_forum 检查 │
71+
└─────────────────────┘
72+
73+
是 Forum? ──No──→ 静默忽略
74+
↓ Yes
75+
正常处理命令
76+
```

0 commit comments

Comments
 (0)