-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (70 loc) · 2.69 KB
/
Copy pathmain.py
File metadata and controls
89 lines (70 loc) · 2.69 KB
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
82
83
84
85
86
87
88
89
import os
import asyncio
import random
from datetime import datetime, timezone, timedelta
from telethon import TelegramClient
from telethon.sessions import StringSession
# Load credentials and bot configuration from environment
API_ID = int(os.environ["API_ID"])
API_HASH = os.environ["API_HASH"]
SESSION_STRING = os.environ["SESSION_STRING"]
# Combined mapping of bot usernames and commands, e.g. "@bot1:/qd,@bot2:sign,@bot3"
BOT_CONFIG_RAW = os.environ.get("BOT_CONFIG", "").strip()
def _resolve_command(cmd: str) -> str:
"""Use the configured command as-is, defaulting to '/qd' when omitted."""
cmd = (cmd or "").strip()
if not cmd:
return "/qd"
return cmd
def get_bot_command_list() -> list[tuple[str, str]]:
"""
Build (bot_username, command) pairs from BOT_CONFIG.
BOT_CONFIG example: "@bot1:/qd,@bot2:sign,@bot3"
- Each entry: "bot_username:command"
- Commands are sent exactly as configured; no leading slash is added automatically.
- If command is omitted, default "/qd" is used.
"""
result: list[tuple[str, str]] = []
if not BOT_CONFIG_RAW:
return result
entries = [e.strip() for e in BOT_CONFIG_RAW.split(",") if e.strip()]
for entry in entries:
if ":" in entry:
bot, cmd_raw = entry.split(":", 1)
else:
bot, cmd_raw = entry, ""
bot = bot.strip()
if not bot:
continue
cmd = _resolve_command(cmd_raw)
result.append((bot, cmd))
return result
async def main():
# Random startup delay to mimic human behavior and reduce spam risk
delay_seconds = random.randint(60, 300)
await asyncio.sleep(delay_seconds)
print("Connecting to Telegram...")
client = TelegramClient(StringSession(SESSION_STRING), API_ID, API_HASH)
await client.start()
print("Logged in.\n")
bot_command_list = get_bot_command_list()
success_bots = []
for bot_username, sign_cmd in bot_command_list:
print(f"Sending sign command '{sign_cmd}' to {bot_username}...")
await client.send_message(bot_username, sign_cmd)
success_bots.append(f"{bot_username}({sign_cmd})")
sleep_time = random.randint(2, 5)
await asyncio.sleep(sleep_time)
print("\nAll sign commands sent.")
await client.disconnect()
tz = timezone(timedelta(hours=8))
current_time = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
log_msg = (
f"[{current_time}] Sent sign commands to {', '.join(success_bots)} "
f"(start delay: {delay_seconds}s).\n"
)
with open("checkin.log", "a", encoding="utf-8") as f:
f.write(log_msg)
print("Log written to checkin.log")
if __name__ == "__main__":
asyncio.run(main())