Это невыносимо - Костя, спаси

This commit is contained in:
kirill 2025-01-21 21:19:28 +03:00
parent cdb68b6f6c
commit ab8908bb83
4 changed files with 41 additions and 22 deletions

View File

@ -1,7 +1,7 @@
from telethon import TelegramClient, events
from textual.app import App, ComposeResult
from textual.containers import Horizontal, VerticalScroll, Vertical, Container
from textual.widgets import Placeholder, Label, Static, Rule, Input, Button
from textual.widgets import Placeholder, Label, Static, Input, Button
from widgets.chat import Chat
from widgets.dialog import Dialog
from telegram.client import TelegramClientWrapper
@ -12,31 +12,37 @@ class TelegramTUI(App):
def __init__(self):
super().__init__()
self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.handler)
self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list)
async def on_mount(self) -> None:
await self.telegram_client.connect()
await self.update_chat_list()
self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
async def handler(self, event):
await self.update_chat_list()
self.limit = 25
for i in range(self.limit):
chat = Chat(id=f"chat-{i + 1}")
self.chat_container.mount(chat)
await self.telegram_client.connect()
# TODO: скоро сюда переедет маунт чатов из функции on_mount
def mount_chats(self):
pass
async def update_chat_list(self):
dialogs = await self.telegram_client.get_dialogs(limit=10)
chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
chat_container.query(Chat).remove()
dialogs = await self.telegram_client.get_dialogs(limit=self.limit)
for dialog in dialogs:
name = dialog.name
msg = dialog.message.message
chat = Chat(name, msg, id=f"chat-{dialog.id}")
chat_container.mount(chat)
for i in range(len(dialogs)):
chat = self.chat_container.query_one(f"#chat-{i + 1}")
chat.username = str(dialogs[i].name)
chat.msg = str(dialogs[i].message.message)
chat.peer_id = dialogs[i].id
#self.notify("Новое сообщение") #колхоз дебаг
def compose(self) -> ComposeResult:
with Horizontal(id="main_container"):
with Horizontal(id="chats"):
yield VerticalScroll(Static(id="chat_container"))
yield Rule("vertical")
yield Dialog()
async def on_exit_app(self):

View File

@ -30,8 +30,14 @@ Message Container {
#input_place {
height: 3;
width: 70%;
align-horizontal: center;
}
#msg_input {
width: 70%;
width: 65%;
}
#send {
}

View File

@ -3,7 +3,10 @@ from telethon import TelegramClient, events, utils
class TelegramClientWrapper:
def __init__(self, api_id, api_hash, message_handler):
self.client = TelegramClient('user', api_id, api_hash)
self.client.add_event_handler(message_handler, events.NewMessage())
#self.client.add_event_handler(message_handler, events.NewMessage())
self.client.on(events.NewMessage())(message_handler)
#ни то ни то не работает, костя спаси
async def connect(self):
await self.client.start()

View File

@ -1,18 +1,22 @@
from textual.widgets import Label
from textual.containers import Horizontal, Vertical
from textual.widget import Widget
from textual.reactive import Reactive
class Chat(Widget):
def __init__(self, name: str | None = None, msg: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False):
username = Reactive(" ", recompose=True)
msg = Reactive(" ", recompose=True)
peer_id = Reactive(0)
def __init__(self, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False):
super().__init__(name=str(name), id=id, classes=classes, disabled=disabled)
self.msg = str(msg)
def _on_click(self):
pass
self.msg = str(self.peer_id)
def compose(self):
with Horizontal():
yield Label(f"┌───┐\n{self.name[:1]}\n└───┘")
yield Label(f"┌───┐\n{self.username[:1]}\n└───┘")
with Vertical():
yield Label(self.name, id="name")
yield Label(self.username, id="name")
yield Label(self.msg, id="last_msg")