File size: 2,936 Bytes
d97a679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
FROM python:3.9-slim

WORKDIR /app

RUN echo "telethon" > requirements.txt && pip install --no-cache-dir -r requirements.txt

RUN echo 'import asyncio\n\
import os\n\
import sys\n\
import re\n\
from telethon import TelegramClient, events\n\
from telethon.sessions import StringSession\n\
\n\
sys.stdout.reconfigure(line_buffering=True)\n\
\n\
API_ID = int(os.environ.get("API_ID", "0"))\n\
API_HASH = os.environ.get("API_HASH")\n\
SESSION_STRING = os.environ.get("TG_SESSION_STRING")\n\
\n\
if not all([API_ID, API_HASH, SESSION_STRING]):\n\
    print("缺少必要的环境变量")\n\
    sys.exit(1)\n\
\n\
async def get_recent_verification_messages(client, count=5):\n\
    """获取来自官方通知用户(777000)的最近 count 条消息,并打印完整内容和验证码"""\n\
    print(f"正在获取来自官方通知用户的最近 {count} 条消息...")\n\
    messages = []\n\
    async for msg in client.iter_messages(777000, limit=count, reverse=False):\n\
        messages.append(msg)\n\
    # 按时间顺序从旧到新打印\n\
    for i, msg in enumerate(reversed(messages), 1):\n\
        text = msg.text\n\
        print(f"--- 第 {i} 条({msg.date})---")\n\
        print(f"消息内容: {text}")\n\
        match = re.search(r"\\b(\\d{5,6})\\b", text)\n\
        if match:\n\
            print(f"验证码: {match.group(1)}")\n\
        else:\n\
            print("未找到验证码")\n\
    if not messages:\n\
        print("未找到来自官方通知用户的消息")\n\
\n\
async def main():\n\
    # 延迟 60 秒启动,让旧容器完全退出,避免 session 冲突\n\
    print("等待 60 秒,确保旧容器已退出...")\n\
    await asyncio.sleep(60)\n\
\n\
    client = TelegramClient(StringSession(SESSION_STRING), API_ID, API_HASH)\n\
    await client.start()\n\
    print("连接成功")\n\
\n\
    # 启动时获取最近 5 条验证码消息\n\
    await get_recent_verification_messages(client, count=5)\n\
\n\
    # 添加实时监听验证码(官方通知用户 777000)\n\
    @client.on(events.NewMessage(from_users=777000))\n\
    async def verification_handler(event):\n\
        text = event.message.text\n\
        print(f"收到新官方通知(实时)")\n\
        print(f"消息内容: {text}")\n\
        match = re.search(r"\\b(\\d{5,6})\\b", text)\n\
        if match:\n\
            print(f"验证码: {match.group(1)}")\n\
        else:\n\
            print("未找到验证码")\n\
\n\
    print("验证码监听已启动,等待新消息...")\n\
    await client.run_until_disconnected()\n\
\n\
if __name__ == "__main__":\n\
    try:\n\
        asyncio.run(main())\n\
    except KeyboardInterrupt:\n\
        print("停止")\n\
    except Exception as e:\n\
        print(f"主程序异常: {e}")\n\
        import traceback\n\
        traceback.print_exc()\n' > verif_monitor.py

EXPOSE 7860
CMD python -u verif_monitor.py 2>&1 & python -m http.server 7860