forked from farneser/tg-bio-time-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
61 lines (45 loc) · 1.66 KB
/
bot.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
from telethon.tl.functions.account import UpdateProfileRequest
from telethon import TelegramClient
from datetime import datetime
from telethon import errors
import asyncio
import pytz
import os
import sys
api_id = int(os.environ["API_ID"])
api_hash = os.environ["API_HASH"]
timezone = pytz.timezone(os.environ.get("TIME_ZONE", "Europe/Minsk"))
pattern = os.environ.get("BIO_PATTERN", "my local time is {TIME}")
session_path = 'session/bio.session'
os.makedirs(os.path.dirname(session_path), exist_ok=True)
client = TelegramClient(session_path, api_id, api_hash)
def get_text(time: str) -> str:
return pattern.replace("{TIME}", time)
async def main():
prev_minute = None
print("Program started...")
while True:
current_time = (datetime.now(timezone)).strftime("%H:%M")
if current_time != prev_minute:
prev_minute = current_time
try:
await client(UpdateProfileRequest(
about=get_text(current_time)
))
print(f"Bio updated at {current_time}")
except errors.FloodWaitError as e:
print(f"FloodWaitError, waiting for {e.seconds} seconds")
await asyncio.sleep(e.seconds)
else:
print(f"bio already up-to-date at {current_time}, waiting...")
await asyncio.sleep(1)
if __name__ == "__main__":
if len(sys.argv) > 1:
if sys.argv[1] == "--session":
client.start()
else:
print("Connecting to client...")
with client:
print("Connected successfully!")
print("Starting main loop...")
client.loop.run_until_complete(main())