-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.py
51 lines (44 loc) · 1.7 KB
/
session.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
from telethon import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import GetMessagesViewsRequest
from telethon.tl.types import Channel
class TelegramSession:
def __init__(self, api_id, api_hash):
self.api_id = api_id
self.api_hash = api_hash
self.session_file = f'accounts/{self.api_id}_{self.api_hash}'
self.channel_count = None
self.session = TelegramClient(
self.session_file,
self.api_id,
self.api_hash
)
self._setup()
def _setup(self):
self._refresh_channel_count()
def _refresh_channel_count(self):
self.channel_count = 0
with self.session:
for dialog in self.session.iter_dialogs():
if isinstance(dialog.entity, Channel):
self.channel_count += 1
def follow_channel(self, channel_id):
with self.session:
self.session.loop.run_until_complete(
self.session(JoinChannelRequest(channel_id))
)
def view_posts(self):
with self.session:
for dialog in self.session.iter_dialogs():
if isinstance(dialog.entity, Channel):
message_ids = [
i.id
for i in self.session.iter_messages(entity=dialog.entity, limit=100)
]
self.session.loop.run_until_complete(
self.session(GetMessagesViewsRequest(
peer=dialog,
id=message_ids,
increment=True
))
)