mirror of
https://github.com/avitoras/telegram-tui.git
synced 2025-07-27 19:26:10 +00:00
дело делается, интерфейсы мутятся
This commit is contained in:
parent
58a6cd6875
commit
9c6ca34135
106
main.py
106
main.py
@ -1,15 +1,17 @@
|
|||||||
from telethon import TelegramClient, events, sync, utils
|
from telethon import TelegramClient, events, utils
|
||||||
from textual.app import App, ComposeResult
|
from textual.app import App, ComposeResult
|
||||||
from textual.widgets import Placeholder, Label, Static, Rule
|
from textual.widgets import Placeholder, Label, Static, Rule, Input, Button
|
||||||
from textual.containers import Horizontal, VerticalScroll, Vertical
|
from textual.containers import Horizontal, VerticalScroll, Vertical, Container
|
||||||
from textual.reactive import var
|
from textual.reactive import var
|
||||||
from textual.widget import Widget
|
from textual.widget import Widget
|
||||||
from tokens import api_id, api_hash
|
from tokens import api_id, api_hash
|
||||||
|
|
||||||
class Chat(Widget):
|
class Chat(Widget):
|
||||||
"""Кастомный виджет чата слева"""
|
"""Кастомный виджет чата слева"""
|
||||||
def __init__(self, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False):
|
|
||||||
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
def __init__(self, name: str | None = None, msg: 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):
|
def _on_click(self):
|
||||||
pass
|
pass
|
||||||
@ -19,9 +21,58 @@ class Chat(Widget):
|
|||||||
yield Label(f"┌───┐\n│ {self.name[:1]} │\n└───┘")
|
yield Label(f"┌───┐\n│ {self.name[:1]} │\n└───┘")
|
||||||
with Vertical():
|
with Vertical():
|
||||||
yield Label(self.name, id="name")
|
yield Label(self.name, id="name")
|
||||||
#yield Label(self.user.dialog[-1].text)
|
yield Label(self.msg, id="last_msg")
|
||||||
|
|
||||||
|
class Dialog(Widget):
|
||||||
|
"""Кастомный виджет диалога справа"""
|
||||||
|
|
||||||
|
def __init__(self, name = None, id = None, classes = None, disabled = False):
|
||||||
|
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
||||||
|
|
||||||
|
def compose(self):
|
||||||
|
with Vertical():
|
||||||
|
with VerticalScroll(id="dialog"):
|
||||||
|
yield Message(message="привет, я ыплыжлп", is_me=True)
|
||||||
|
yield Message(message="о, дщытрапшщцрущ", is_me=False)
|
||||||
|
yield Message(message="ДАТОУШЩАРШЩУРЩША!!!!", is_me=False)
|
||||||
|
#должно быть примерно is_me = message.from_id == client.get_peer_id("me")
|
||||||
|
#но я могу ошибаться, я это фиш если что
|
||||||
|
|
||||||
|
with Horizontal(id="input_place"):
|
||||||
|
yield Input(placeholder="Сообщение", id="msg_input")
|
||||||
|
yield Button(label="➤", id="send", variant="primary")
|
||||||
|
|
||||||
|
def on_button_pressed(event):
|
||||||
|
app.notify("Нажато отправить")
|
||||||
|
|
||||||
|
|
||||||
|
class Message(Widget):
|
||||||
|
"""Кастомный виджет сообщения"""
|
||||||
|
|
||||||
|
def __init__(self, name = None, message = None, is_me = None, id = None, classes = None, disabled = False):
|
||||||
|
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
||||||
|
self.message = message
|
||||||
|
self.is_me = is_me
|
||||||
|
|
||||||
|
def _on_mount(self):
|
||||||
|
if self.is_me:
|
||||||
|
self.styles.padding = (0, 0, 0, 15)
|
||||||
|
self.query_one(Container).query_one(Label).styles.text_align = "right"
|
||||||
|
self.query_one(Container).styles.align_horizontal = "right"
|
||||||
|
self.query_one(Container).query_one(Label).styles.border = ("solid", "#4287f5")
|
||||||
|
else:
|
||||||
|
self.styles.padding = (0, 15, 0, 0)
|
||||||
|
self.query_one(Container).query_one(Label).styles.text_align = "left"
|
||||||
|
self.query_one(Container).styles.align_horizontal = "left"
|
||||||
|
self.query_one(Container).query_one(Label).styles.border = ("solid", "#ffffff")
|
||||||
|
|
||||||
|
def compose(self):
|
||||||
|
with Container():
|
||||||
|
#yield Label(self.message.message) #это нормальный вариант
|
||||||
|
yield Label(str(self.message)) #это тестовый вариант
|
||||||
|
|
||||||
class TelegramTUI(App):
|
class TelegramTUI(App):
|
||||||
|
"""Главный класс приложения"""
|
||||||
|
|
||||||
CSS_PATH = "styles.tcss"
|
CSS_PATH = "styles.tcss"
|
||||||
|
|
||||||
@ -29,37 +80,29 @@ class TelegramTUI(App):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.api_id = api_id
|
self.api_id = api_id
|
||||||
self.api_hash = api_hash
|
self.api_hash = api_hash
|
||||||
|
self.chats = []
|
||||||
self.client = TelegramClient('user', api_id, api_hash)
|
self.client = TelegramClient('user', api_id, api_hash)
|
||||||
self.chats = var([])
|
self.client.on(events.NewMessage())(self.handler)
|
||||||
|
|
||||||
async def on_mount(self) -> None:
|
async def on_mount(self) -> None:
|
||||||
await self.client.start()
|
await self.client.start()
|
||||||
dialogs = []
|
await self.update_chat_list()
|
||||||
async for dialog in self.client.iter_dialogs():
|
|
||||||
dialogs.append(dialog)
|
async def handler(self, event):
|
||||||
self.chats = dialogs
|
|
||||||
await self.update_chat_list()
|
await self.update_chat_list()
|
||||||
|
|
||||||
async def update_chat_list(self):
|
async def update_chat_list(self):
|
||||||
#if self.chats:
|
dialogs = []
|
||||||
#for dialog in self.chats:
|
async for dialog in self.client.iter_dialogs(limit=10):
|
||||||
# name = utils.get_display_name(dialog.entity)
|
dialogs.append(dialog)
|
||||||
# last_msg = "" # Значение по умолчанию
|
|
||||||
|
|
||||||
# try:
|
|
||||||
# last_messages = await self.client.get_messages(dialog.entity, limit=1)
|
|
||||||
# if last_messages:
|
|
||||||
# last_msg = last_messages[0].message # Получаем текст последнего сообщения
|
|
||||||
# except Exception as e: # Добавлена обработка ошибок
|
|
||||||
# print(f"Ошибка получения последнего сообщения: {e}")
|
|
||||||
|
|
||||||
chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
|
chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container")
|
||||||
chat_container.query(Chat).remove() # Clear existing labels
|
chat_container.query(Chat).remove()
|
||||||
|
|
||||||
for dialog in self.chats:
|
for dialog in dialogs:
|
||||||
name = utils.get_display_name(dialog.entity)
|
name = utils.get_display_name(dialog.entity)
|
||||||
#msg = utils.get_input_peer
|
msg = dialog.message.message
|
||||||
chat = Chat(name, id=f"chat-{dialog.id}")
|
chat = Chat(name, msg, id=f"chat-{dialog.id}")
|
||||||
chat_container.mount(chat)
|
chat_container.mount(chat)
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
@ -69,12 +112,11 @@ class TelegramTUI(App):
|
|||||||
|
|
||||||
yield Rule("vertical")
|
yield Rule("vertical")
|
||||||
|
|
||||||
with VerticalScroll(id="dialog"):
|
yield Dialog()
|
||||||
yield Placeholder(label="message", classes=("message"))
|
|
||||||
yield Placeholder(label="message", classes=("message"))
|
async def _on_exit_app(self):
|
||||||
yield Placeholder(label="message", classes=("message"))
|
await self.client.disconnect()
|
||||||
yield Placeholder(label="message", classes=("message"))
|
return super()._on_exit_app()
|
||||||
yield Placeholder(label="message", classes=("message"))
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = TelegramTUI()
|
app = TelegramTUI()
|
||||||
|
17
styles.tcss
17
styles.tcss
@ -18,3 +18,20 @@ Rule {
|
|||||||
height: 3;
|
height: 3;
|
||||||
padding: 1;
|
padding: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Message {
|
||||||
|
height: auto;
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
Message Container {
|
||||||
|
height: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
#input_place {
|
||||||
|
height: 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
#msg_input {
|
||||||
|
width: 70%;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user