ljx77qaq commited on
Commit
f6bb964
·
verified ·
1 Parent(s): fc7f22e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +37 -4
main.py CHANGED
@@ -1,8 +1,9 @@
1
  import asyncio
2
  import os
3
  import logging
 
4
  from fastapi import FastAPI, Request
5
- from aiogram import Bot, Dispatcher, types
6
  from aiogram.types import Update
7
  from aiogram.enums import ParseMode
8
  from aiogram.client.default import DefaultBotProperties
@@ -12,14 +13,14 @@ logging.basicConfig(level=logging.INFO)
12
 
13
  # 从环境变量获取配置 (在 HF Space 的 Settings -> Variables and secrets 里设置)
14
  BOT_TOKEN = os.getenv("BOT_TOKEN")
15
- WEBHOOK_URL = os.getenv("WEBHOOK_URL") # 你的 HF Space 网址,例如 https://your-username-space-name.hf.space
16
 
17
  bot = Bot(token=BOT_TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
18
  dp = Dispatcher()
19
  app = FastAPI()
20
 
21
  # ----------------- 模拟数据库 -----------------
22
- # 警告:HF 重启会清空本地数据!后续请换成 Cloudflare D1 或外部数据库接口
23
  CHANNEL_MAP = {
24
  -1001234567890: -1009876543210 # 来源频道 ID : 目标频道 ID
25
  }
@@ -27,6 +28,38 @@ CHANNEL_MAP = {
27
  # ----------------- 媒体组缓冲池 -----------------
28
  album_buffer = {}
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  @dp.channel_post()
31
  async def handle_channel_post(message: types.Message):
32
  source_id = message.chat.id
@@ -78,8 +111,8 @@ async def send_album_delayed(mg_id: str, source_id: int, target_id: int):
78
  except Exception as e:
79
  logging.error(f"同步媒体组失败: {e}")
80
 
81
- # ----------------- FastAPI 路由与 Webhook -----------------
82
 
 
83
  @app.on_event("startup")
84
  async def on_startup():
85
  # 启动时告诉 Telegram 把消息推送到我们的接口
 
1
  import asyncio
2
  import os
3
  import logging
4
+ import json # 【新增部分】引入 json 库解析小程序数据
5
  from fastapi import FastAPI, Request
6
+ from aiogram import Bot, Dispatcher, types, F # 【新增部分】引入 F 用于魔法过滤
7
  from aiogram.types import Update
8
  from aiogram.enums import ParseMode
9
  from aiogram.client.default import DefaultBotProperties
 
13
 
14
  # 从环境变量获取配置 (在 HF Space 的 Settings -> Variables and secrets 里设置)
15
  BOT_TOKEN = os.getenv("BOT_TOKEN")
16
+ WEBHOOK_URL = os.getenv("WEBHOOK_URL") # 你的 HF Space 网址
17
 
18
  bot = Bot(token=BOT_TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
19
  dp = Dispatcher()
20
  app = FastAPI()
21
 
22
  # ----------------- 模拟数据库 -----------------
23
+ # 警告:HF 重启会清空本地数据!后续请换成真实的数据库
24
  CHANNEL_MAP = {
25
  -1001234567890: -1009876543210 # 来源频道 ID : 目标频道 ID
26
  }
 
28
  # ----------------- 媒体组缓冲池 -----------------
29
  album_buffer = {}
30
 
31
+
32
+ # ==========================================
33
+ # 【新增部分】处理从小程序 (TWA) 传回来的数据
34
+ # ==========================================
35
+ @dp.message(F.web_app_data)
36
+ async def handle_web_app_data(message: types.Message):
37
+ # 获取小程序发来的 JSON 字符串
38
+ data_str = message.web_app_data.data
39
+
40
+ try:
41
+ data = json.loads(data_str)
42
+ if data.get("action") == "set_sync":
43
+ source = data.get("source")
44
+ target = data.get("target")
45
+
46
+ logging.info(f"收到新配置: {source} -> {target}")
47
+
48
+ # 动态更新内存中的字典
49
+ CHANNEL_MAP[source] = target
50
+
51
+ await message.answer(
52
+ f"✅ <b>配置保存成功!</b>\n\n将来从 <code>{source}</code> 发布的消息会自动同步到 <code>{target}</code>。",
53
+ parse_mode=ParseMode.HTML
54
+ )
55
+
56
+ except Exception as e:
57
+ logging.error(f"解析小程序数据失败: {e}")
58
+ await message.answer("❌ 配置解析失败,请重试。")
59
+ # ==========================================
60
+
61
+
62
+ # ----------------- 处理频道消息同步 -----------------
63
  @dp.channel_post()
64
  async def handle_channel_post(message: types.Message):
65
  source_id = message.chat.id
 
111
  except Exception as e:
112
  logging.error(f"同步媒体组失败: {e}")
113
 
 
114
 
115
+ # ----------------- FastAPI 路由与 Webhook -----------------
116
  @app.on_event("startup")
117
  async def on_startup():
118
  # 启动时告诉 Telegram 把消息推送到我们的接口