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

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 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, 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.chat import Chat
from widgets.dialog import Dialog from widgets.dialog import Dialog
from telegram.client import TelegramClientWrapper from telegram.client import TelegramClientWrapper
@ -12,31 +12,37 @@ class TelegramTUI(App):
def __init__(self): def __init__(self):
super().__init__() 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: async def on_mount(self) -> None:
await self.telegram_client.connect() self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
await self.update_chat_list()
async def handler(self, event): self.limit = 25
await self.update_chat_list() 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): async def update_chat_list(self):
dialogs = await self.telegram_client.get_dialogs(limit=10) dialogs = await self.telegram_client.get_dialogs(limit=self.limit)
chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
chat_container.query(Chat).remove()
for dialog in dialogs: for i in range(len(dialogs)):
name = dialog.name chat = self.chat_container.query_one(f"#chat-{i + 1}")
msg = dialog.message.message chat.username = str(dialogs[i].name)
chat = Chat(name, msg, id=f"chat-{dialog.id}") chat.msg = str(dialogs[i].message.message)
chat_container.mount(chat) chat.peer_id = dialogs[i].id
#self.notify("Новое сообщение") #колхоз дебаг
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
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"))
yield Rule("vertical")
yield Dialog() yield Dialog()
async def on_exit_app(self): async def on_exit_app(self):

View File

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

View File

@ -3,7 +3,10 @@ from telethon import TelegramClient, events, utils
class TelegramClientWrapper: class TelegramClientWrapper:
def __init__(self, api_id, api_hash, message_handler): def __init__(self, api_id, api_hash, message_handler):
self.client = TelegramClient('user', api_id, api_hash) 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): async def connect(self):
await self.client.start() await self.client.start()

View File

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