mirror of
https://github.com/avitoras/telegram-tui.git
synced 2025-07-27 19:26:10 +00:00
короче
короче
This commit is contained in:
commit
f1663fe880
30
app/app.py
30
app/app.py
@ -1,7 +1,7 @@
|
|||||||
from telethon import TelegramClient, events
|
#from telethon import TelegramClient, events
|
||||||
from textual.app import App, ComposeResult
|
from textual.app import App, ComposeResult
|
||||||
from textual.containers import Horizontal, VerticalScroll, Vertical, Container
|
from textual.containers import Horizontal, VerticalScroll
|
||||||
from textual.widgets import Placeholder, Label, Static, Input, Button
|
from textual.widgets import Static, Footer
|
||||||
from widgets.chat import Chat
|
from widgets.chat import Chat
|
||||||
from widgets.dialog import Dialog
|
from widgets.dialog import Dialog
|
||||||
from telegram.client import TelegramClientWrapper
|
from telegram.client import TelegramClientWrapper
|
||||||
@ -12,21 +12,33 @@ class TelegramTUI(App):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list)
|
|
||||||
|
|
||||||
async def on_mount(self) -> None:
|
async def on_mount(self) -> None:
|
||||||
|
self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list)
|
||||||
self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
|
self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
|
||||||
|
self.limit = 100
|
||||||
self.limit = 25
|
|
||||||
for i in range(self.limit):
|
for i in range(self.limit):
|
||||||
chat = Chat(id=f"chat-{i + 1}")
|
chat = Chat(id=f"chat-{i + 1}")
|
||||||
self.chat_container.mount(chat)
|
self.chat_container.mount(chat)
|
||||||
|
#self.mount_chats(limit=25)
|
||||||
|
|
||||||
await self.telegram_client.connect()
|
await self.telegram_client.connect()
|
||||||
|
|
||||||
|
await self.update_chat_list()
|
||||||
|
|
||||||
# TODO: скоро сюда переедет маунт чатов из функции on_mount
|
# TODO: скоро сюда переедет маунт чатов из функции on_mount
|
||||||
def mount_chats(self):
|
# P.S. сделано, но неудачно
|
||||||
pass
|
def mount_chats(self, limit: int):
|
||||||
|
self.limit = limit
|
||||||
|
chats_amount = len(self.chat_container.query(Chat))
|
||||||
|
if limit > chats_amount:
|
||||||
|
for i in range(limit - chats_amount):
|
||||||
|
chat = Chat(id=f"chat-{i + 1 + (limit - chats_amount)}")
|
||||||
|
self.chat_container.mount(chat)
|
||||||
|
elif not (limit == chats_amount):
|
||||||
|
for i in range(chats_amount - limit):
|
||||||
|
self.chat_container.query(Chat).last().remove()
|
||||||
|
|
||||||
async def update_chat_list(self):
|
async def update_chat_list(self):
|
||||||
dialogs = await self.telegram_client.get_dialogs(limit=self.limit)
|
dialogs = await self.telegram_client.get_dialogs(limit=self.limit)
|
||||||
@ -39,9 +51,11 @@ class TelegramTUI(App):
|
|||||||
#self.notify("Новое сообщение") #колхоз дебаг
|
#self.notify("Новое сообщение") #колхоз дебаг
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
|
yield Footer()
|
||||||
with Horizontal(id="main_container"):
|
with Horizontal(id="main_container"):
|
||||||
with Horizontal(id="chats"):
|
with Horizontal(id="chats"):
|
||||||
yield VerticalScroll(Static(id="chat_container"))
|
yield VerticalScroll(Static(id="chat_container"))
|
||||||
|
#TODO: сделать кнопку чтобы прогрузить больше чатов, это оптимизация
|
||||||
|
|
||||||
yield Dialog()
|
yield Dialog()
|
||||||
|
|
||||||
|
@ -6,8 +6,6 @@ class TelegramClientWrapper:
|
|||||||
self.client = TelegramClient('user', api_id, api_hash)
|
self.client = TelegramClient('user', api_id, api_hash)
|
||||||
self.client.on(events.NewMessage())(self.local_message_handler)
|
self.client.on(events.NewMessage())(self.local_message_handler)
|
||||||
|
|
||||||
#ни то ни то не работает, костя спаси
|
|
||||||
|
|
||||||
async def local_message_handler(self, event):
|
async def local_message_handler(self, event):
|
||||||
await self.message_handler()
|
await self.message_handler()
|
||||||
|
|
||||||
@ -17,21 +15,24 @@ class TelegramClientWrapper:
|
|||||||
async def disconnect(self):
|
async def disconnect(self):
|
||||||
await self.client.disconnect()
|
await self.client.disconnect()
|
||||||
|
|
||||||
async def get_dialogs(self, limit=10):
|
async def get_dialogs(self, limit=None):
|
||||||
|
await self.client.get_dialogs(limit=limit)
|
||||||
dialogs_list = []
|
dialogs_list = []
|
||||||
async for dialog in self.client.iter_dialogs(limit=limit):
|
async for dialog in self.client.iter_dialogs(limit=limit):
|
||||||
dialogs_list.append(dialog)
|
dialogs_list.append(dialog)
|
||||||
return [self._map_dialog(d) for d in dialogs_list]
|
#return [self._map_dialog(d) for d in dialogs_list]
|
||||||
|
return dialogs_list
|
||||||
|
|
||||||
def _map_dialog(self, dialog):
|
#ого:
|
||||||
|
"""def _map_dialog(self, dialog):
|
||||||
return DialogInfo(
|
return DialogInfo(
|
||||||
id=dialog.id,
|
id=dialog.id,
|
||||||
name=utils.get_display_name(dialog.entity),
|
name=utils.get_display_name(dialog.entity),
|
||||||
message=dialog.message
|
message=dialog.message
|
||||||
)
|
)"""
|
||||||
|
|
||||||
class DialogInfo:
|
"""class DialogInfo:
|
||||||
def __init__(self, id, name, message):
|
def __init__(self, id, name, message):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.message = message
|
self.message = message"""
|
||||||
|
@ -15,6 +15,7 @@ class Dialog(Widget):
|
|||||||
yield Message(message="ДАТОУШЩАРШЩУРЩША!!!!", is_me=False)
|
yield Message(message="ДАТОУШЩАРШЩУРЩША!!!!", is_me=False)
|
||||||
# должно быть примерно is_me = message.from_id == client.get_peer_id("me")
|
# должно быть примерно is_me = message.from_id == client.get_peer_id("me")
|
||||||
# но я могу ошибаться, я это фиш если что
|
# но я могу ошибаться, я это фиш если что
|
||||||
|
#TODO: сделать кнопку чтобы прогрузить больше сообщений, но при этом чтобы при перезаходе в чат оставались прогруженными только 10 сообщений, а остальные декомпоузились
|
||||||
|
|
||||||
with Horizontal(id="input_place"):
|
with Horizontal(id="input_place"):
|
||||||
yield Input(placeholder="Сообщение", id="msg_input")
|
yield Input(placeholder="Сообщение", id="msg_input")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user