用于Dlercloud_Bot 签到

🧱 一、前提条件

请提前准备好:

  • 你的 Telegram 帐号 + 手机验证码(首次登录时用)

  • 你的 Telegram API ID 与 API Hash

  • 目标 bot 的用户名(不含 @,如 CheckinBot


🛠️ 二、安装依赖与创建虚拟环境

1. 安装 Python venv 工具

sudo apt update 
sudo apt install python3-venv -y

2. 创建虚拟环境

python3 -m venv /opt/checkin-env

3. 激活虚拟环境

source /opt/checkin-env/bin/activate

4. 安装 Telethon

pip install telethon

5. 退出虚拟环境(可选)

deactivate

🧾 三、创建签到脚本 /root/send_checkin.py

nano /root/send_checkin.py

将以下内容粘贴进去(记得改成你的 api_id、api_hash、bot 用户名):

from telethon import TelegramClient, events
from datetime import datetime
import asyncio
import requests
import os

# === 你要改的部分(Telegram账户和Bot配置) ===
api_id = 12345678              # 你的 Telegram API ID
api_hash = 'your_api_hash'     # 你的 API HASH
session_name = 'checkin_session'
target_bot = 'your_target_bot_username'  # 比如 'myvpn_bot'

# 你的通知 Bot 的 token 和 chat_id
notify_bot_token = ''
notify_chat_id = ''

# 日志文件路径
log_file = '/root/checkin.log'

# === 发送 TG 通知函数 ===
def send_notify(content):
    url = f"https://api.telegram.org/bot{notify_bot_token}/sendMessage"
    payload = {
        "chat_id": notify_chat_id,
        "text": f"🔔 检测到流量消息:\n\n{content}"
    }
    try:
        res = requests.post(url, data=payload, timeout=10)
        print(f"[通知发送结果] 状态码: {res.status_code}")
    except Exception as e:
        with open(log_file, 'a') as f:
            f.write(f"[通知失败] {e}\n")

# === 主逻辑 ===
client = TelegramClient(session_name, api_id, api_hash)

@client.on(events.NewMessage(from_users=target_bot))
async def handler(event):
    now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    msg = event.message.message.replace('\n', ' | ')
    print(f"[DEBUG] 收到 Bot 消息: {msg}")

    with open(log_file, 'a') as f:
        f.write(f"[{now}] Bot回复: {msg}\n")

    # 判断关键词
    keywords = ['流量', 'MiB', 'MB', 'GB']
    if any(kw in msg for kw in keywords):
        print("[DEBUG] 关键词命中,准备发送通知")
        send_notify(msg)
    else:
        print("[DEBUG] 无关键词命中,不发送通知")

    await client.disconnect()

async def main():
    await client.start()
    await client.send_message(target_bot, '/checkin 100')
    print("[INFO] 已发送 /checkin 100 指令")
    await client.run_until_disconnected()

asyncio.run(main())

保存退出:Ctrl + O → 回车 → Ctrl + X


🔐 四、首次运行脚本以登录 Telegram

/opt/checkin-env/bin/python /root/send_checkin.py

按提示输入:

  • 手机号(如 +8613xxxxxxxxx

  • 验证码

  • 二步验证密码(如设置过)

成功后会在 /root/ 生成 .checkin_session.session 文件。