-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (63 loc) · 2.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# main.py
import hmac
import hashlib
from fastapi import FastAPI, Header, HTTPException, Request
from config import GROUP_CONFIGS
from chat_sender import send_group_forward_msg
import json
from formatter import format_to_json
app = FastAPI()
def verify_signature(secret: str, signature: str, body: bytes) -> bool:
"""
验证GitHub Webhook的签名。
"""
sha_name, signature = signature.split("=")
if sha_name != "sha256":
return False
mac = hmac.new(secret.encode(), msg=body, digestmod=hashlib.sha256)
return hmac.compare_digest(mac.hexdigest(), signature)
@app.post("/webhook")
async def github_webhook(
request: Request,
x_github_event: str = Header(None),
x_hub_signature_256: str = Header(None),
):
if not x_github_event or not x_hub_signature_256:
raise HTTPException(status_code=400, detail="缺少必要的头信息")
body = await request.body()
# 解析payload
try:
payload = json.loads(body)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="无效的JSON格式")
# 根据payload确定来源
organization = (
payload.get("organization", {}).get("login")
if "organization" in payload
else None
)
repository = (
payload.get("repository", {}).get("full_name")
if "repository" in payload
else None
)
# 找到匹配的群聊配置
matched_groups = []
for config in GROUP_CONFIGS:
if config.organization and config.organization == organization:
matched_groups.append(config)
if config.repository and config.repository == repository:
matched_groups.append(config)
if not matched_groups:
return {"msg": "未找到匹配的群聊"}
# 构建消息内容
message = format_to_json(payload)
# 为每个匹配的群聊验证签名并发送消息
for group in matched_groups:
if not verify_signature(group.secret, x_hub_signature_256, body):
raise HTTPException(
status_code=403, detail=f"群聊 {group.group_id} 的签名验证失败"
)
# 发送消息到群聊
send_group_forward_msg(group.group_id, message)
return {"msg": "消息已成功发送"}