-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·68 lines (45 loc) · 1.6 KB
/
main.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
62
63
64
65
66
67
68
#!/usr/bin/env python3
import re
from telethon import TelegramClient
import telethon.tl.functions as functions
import telethon.tl.types as types
from feedgen.feed import FeedGenerator
import config
feeds = {}
def add_channel(client, username):
result = client.invoke(functions.contacts.ResolveUsernameRequest(username))
if not isinstance(result.peer, types.PeerChannel):
print("Wrong username " + username)
return
channel = result.chats[0]
if channel.id in feeds:
print("Already joined channel " + username)
return
full_channel = client.invoke(functions.channels.GetFullChannelRequest(channel)).full_chat
fg = FeedGenerator()
fg.title(channel.title)
fg.link(href='https://t.me/' + username, rel='via')
fg.description(full_channel.about)
feeds[channel.id] = fg
client.invoke(functions.channels.JoinChannelRequest(channel))
def handle_update(update):
pass
def main():
client = TelegramClient(config.session_name, config.api_id, config.api_hash, update_workers=1)
client.connect()
if not client.is_user_authorized():
client.send_code_request(config.phone)
client.sign_in(phone=config.phone, code=input("Enter code: "))
client.add_update_handler(handle_update)
while True:
command = input(">> ")
if command == "exit":
break
else:
result = re.match(r"^join\s+@?(\w+)", command)
if result:
username = result.group(1)
add_channel(client, username)
client.disconnect()
if __name__ == "__main__":
main()