-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (173 loc) · 7.66 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import asyncio
from telethon import TelegramClient, events, types
import os
from dotenv import load_dotenv
from PIL import Image
class ConsoleTelegramClient(TelegramClient):
def __init__(self, session_user_id, api_id, api_hash, page_size=15):
print('Initialization...')
super().__init__(session_user_id, api_id, api_hash, device_model="Linux 5.15.0", system_version="Ubuntu 20.04.6 LTS")
self.page_size = page_size
self.input_msg = 'Input command or dialog id: '
self.add_event_handler(self.message_handler, events.NewMessage(incoming=True))
self.add_event_handler(self.update_dialogs_list, events.ChatAction)
if not os.path.exists('download'):
os.mkdir('download')
print('Initialization complited')
async def login(self):
print('Connecting to Telegram servers...')
try:
await self.connect()
except IOError:
print('Initial connection failed. Retrying...')
await self.connect()
print('Connect successfully')
if not await self.is_user_authorized():
await self.start()
self.id = (await self.get_me()).id
async def print_main_menu(self):
print('---------------------------------')
print('Command list:')
print('/np - next page of dialogs')
print('/pp - previous page of dialogs')
print('"chat id" - open chat by id')
print('/e - disconnect and close program')
print('/log_out - log_out your account')
print('/h - command list')
print('---------------------------------')
async def print_chat_menu(self):
print('---------------------------------')
print('Command list:')
print('/p "photo id" - open photo by id')
print('/sf "file name" - send file from /download by name. Example: "/sf hello.jpg"')
print('/b - go to dialogs list')
print('/e - disconnect and close program')
print('/h - command list')
print('---------------------------------')
async def run(self):
await self.update_dialogs_list()
dialog_num = None
dialogs_page = 0
while True:
await self.print_main_menu()
while dialog_num == None:
await self.print_dialogs(dialogs_page)
command = (await asyncio.to_thread(input, self.input_msg)).strip()
if command.startswith('/e'):
await self.disconnect()
return
elif command.startswith('/h'):
await self.print_main_menu()
elif command.isdigit():
dialog_num = int(command)
elif command.startswith('/log_out'):
await self.log_out()
return
elif command.startswith('/np'):
dialogs_num = (len(self.dialogs_list)+self.page_size-1)//self.page_size - 1
if dialogs_page < dialogs_num:
dialogs_page += 1
elif command.startswith('/pp'):
if dialogs_page > 0:
dialogs_page -= 1
else:
print('Use commands please! Input "help" for more information')
await asyncio.sleep(0.1)
dialog = self.dialogs_list[dialog_num]
entity = dialog.entity
await self.print_chat_menu()
print(f'Dialog with {dialog.title}')
await self.print_messages(entity, dialog)
while dialog_num != None:
command = (await asyncio.to_thread(input, 'Input message or command: ')).strip()
if command.startswith('/e'):
await self.disconnect()
return
elif command.startswith('/b'):
dialog_num = None
elif command.startswith('/h'):
await self.print_chat_menu()
elif command.startswith('/p'):
img_path = f'download/{command.split()[1]}.jpg'
if os.path.exists(img_path):
Image.open(img_path).show()
else:
print('incorrect image id')
elif command.startswith('/sf'):
img_path = f'download/{command.split()[1]}'
if os.path.exists(img_path):
await self.send_file(entity, img_path)
else:
print('incorrect image name')
else:
await self.send_message(entity, command)
await asyncio.sleep(0.1)
async def print_messages(self, entity, dialog):
for msg in [i async for i in self.iter_messages(entity, limit=self.page_size)][::-1]:
if msg.from_id and msg.from_id.user_id == self.id:
sender = 'me'
else:
sender = dialog.title
print(f'{sender}[{msg.date.strftime("%Y-%m-%d %H:%M")}]: ', await self.get_message(msg))
async def print_dialogs(self, dialogs_page):
dialogs = self.dialogs_list
for i, dialog in enumerate(dialogs[self.page_size*dialogs_page:self.page_size*(dialogs_page+1)]):
msg = await self.get_message(dialog.message)
title = dialog.title
print(f'{self.page_size*dialogs_page+i}. {title}: {msg}')
async def get_message(self, message):
msg = ''
if message.message:
msg += message.message.split('\n')[0]
if message.media:
if type(message.media) == types.MessageMediaPhoto:
if not os.path.exists(f'download/{message.media.photo.id}.jpg'):
await self.download_media(message, f'download/{message.media.photo.id}.jpg')
if msg:
msg += f' *photo({message.media.photo.id})*'
else:
msg += f'*photo({message.media.photo.id})*'
else:
if msg:
msg += ' *anower media*'
else:
msg += '*anower media*'
if type(message) == types.MessageService:
msg = '*service meassage*'
if msg == '':
print(type(message), message)
return msg
async def get_name_from_event(self, event):
chat = await event.get_chat()
name = ''
if type(chat) == types.User:
if chat.first_name:
name += chat.first_name
if chat.last_name:
name += chat.last_name
if name == '':
name = chat.username
else:
name = chat.title
return name
async def message_handler(self, event):
name = await self.get_name_from_event(event)
msg = await self.get_message(event.message)
print(f'\n[New messange] {name}: {msg}\nKeep input: ', end='')
async def update_dialogs_list(self, event=None):
self.dialogs_list = await self.get_dialogs()
async def main():
load_dotenv()
session_name = os.getenv('session_name')
api_id = os.getenv('api_id')
api_hash = os.getenv('api_hash')
client = ConsoleTelegramClient(session_name, api_id, api_hash)
await client.login()
try:
await client.run()
except Exception as e:
print(e)
await client.disconnect()
print('Goodbye!')
if __name__ == '__main__':
asyncio.run(main())