mirror of
https://github.com/avitoras/telegram-tui.git
synced 2025-07-27 19:26:10 +00:00
почти получилось сделать многоэкранное
This commit is contained in:
parent
5be6da7eeb
commit
c9fb658a6b
55
app/app.py
55
app/app.py
@ -1,34 +1,30 @@
|
|||||||
#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
|
from textual.containers import Horizontal, VerticalScroll
|
||||||
from textual.widgets import Static, Footer
|
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 textual.screen import Screen
|
||||||
|
#from telegram.client import TelegramClientWrapper
|
||||||
from tokens import api_id, api_hash
|
from tokens import api_id, api_hash
|
||||||
|
|
||||||
class TelegramTUI(App):
|
class ChatScreen(Screen):
|
||||||
CSS_PATH = "../tcss/style.tcss"
|
"""Класс экрана чатов, он же основной экран приложения"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, name = None, id = None, classes = None, telegram_client = None):
|
||||||
super().__init__()
|
super().__init__(name, id, classes)
|
||||||
|
self.telegram_client = telegram_client
|
||||||
|
self.telegram_client.on(events.NewMessage())(self.update_chat_list)
|
||||||
|
|
||||||
async def on_mount(self) -> None:
|
def on_mount(self):
|
||||||
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 = 100
|
||||||
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}", notify_func=self.notify)
|
||||||
self.chat_container.mount(chat)
|
self.chat_container.mount(chat)
|
||||||
#self.mount_chats(limit=25)
|
#self.mount_chats(limit=25)
|
||||||
|
|
||||||
await self.telegram_client.connect()
|
|
||||||
|
|
||||||
await self.update_chat_list()
|
|
||||||
|
|
||||||
# TODO: скоро сюда переедет маунт чатов из функции on_mount
|
|
||||||
# P.S. сделано, но неудачно
|
|
||||||
def mount_chats(self, limit: int):
|
def mount_chats(self, limit: int):
|
||||||
self.limit = limit
|
self.limit = limit
|
||||||
chats_amount = len(self.chat_container.query(Chat))
|
chats_amount = len(self.chat_container.query(Chat))
|
||||||
@ -48,7 +44,7 @@ class TelegramTUI(App):
|
|||||||
chat.username = str(dialogs[i].name)
|
chat.username = str(dialogs[i].name)
|
||||||
chat.msg = str(dialogs[i].message.message)
|
chat.msg = str(dialogs[i].message.message)
|
||||||
chat.peer_id = dialogs[i].id
|
chat.peer_id = dialogs[i].id
|
||||||
#self.notify("Новое сообщение") #колхоз дебаг
|
self.notify("Новое сообщение") #колхоз дебаг
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
yield Footer()
|
yield Footer()
|
||||||
@ -59,6 +55,31 @@ class TelegramTUI(App):
|
|||||||
|
|
||||||
yield Dialog()
|
yield Dialog()
|
||||||
|
|
||||||
|
class AuthScreen(Screen):
|
||||||
|
"""Это будет классом экрана логина"""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
class TelegramTUI(App):
|
||||||
|
"""Класс приложения"""
|
||||||
|
|
||||||
|
CSS_PATH = "../tcss/style.tcss"
|
||||||
|
#SCREENS = {"chats": ChatScreen}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
async def on_mount(self) -> None:
|
||||||
|
self.telegram_client = TelegramClient("../user", api_id, api_hash)
|
||||||
|
#self.push_screen("chats")
|
||||||
|
chat_screen = ChatScreen(telegram_client=self.telegram_client)
|
||||||
|
self.install_screen(chat_screen, name="chats")
|
||||||
|
self.push_screen("chats")
|
||||||
|
self.telegram_client.on(events.NewMessage())(chat_screen.update_chat_list())
|
||||||
|
|
||||||
|
await self.telegram_client.start()
|
||||||
|
await self.update_chat_list()
|
||||||
|
|
||||||
async def on_exit_app(self):
|
async def on_exit_app(self):
|
||||||
await self.telegram_client.disconnect()
|
await self.telegram_client.disconnect()
|
||||||
return super()._on_exit_app()
|
return super()._on_exit_app()
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
from telethon import TelegramClient, events, utils
|
from telethon import TelegramClient, events, utils
|
||||||
|
|
||||||
class TelegramClientWrapper:
|
class TelegramClientWrapper:
|
||||||
|
"""Обёртка для метода TelegramClient из Telethon"""
|
||||||
|
|
||||||
def __init__(self, api_id, api_hash, message_handler):
|
def __init__(self, api_id, api_hash, message_handler):
|
||||||
self.message_handler = message_handler
|
self.message_handler = message_handler
|
||||||
self.client = TelegramClient('user', api_id, api_hash)
|
self.client = TelegramClient('user', api_id, api_hash)
|
||||||
@ -10,6 +12,9 @@ class TelegramClientWrapper:
|
|||||||
await self.message_handler()
|
await self.message_handler()
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self):
|
||||||
|
await self.client.connect()
|
||||||
|
|
||||||
|
async def start(self):
|
||||||
await self.client.start()
|
await self.client.start()
|
||||||
|
|
||||||
async def disconnect(self):
|
async def disconnect(self):
|
||||||
|
@ -4,15 +4,19 @@ from textual.widget import Widget
|
|||||||
from textual.reactive import Reactive
|
from textual.reactive import Reactive
|
||||||
|
|
||||||
class Chat(Widget):
|
class Chat(Widget):
|
||||||
|
"""Класс виджета чата для панели чатов"""
|
||||||
|
|
||||||
username = Reactive(" ", recompose=True)
|
username = Reactive(" ", recompose=True)
|
||||||
msg = Reactive(" ", recompose=True)
|
msg = Reactive(" ", recompose=True)
|
||||||
peer_id = Reactive(0)
|
peer_id = Reactive(0)
|
||||||
|
|
||||||
def __init__(self, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False):
|
def __init__(self, name: str | None = None, notify_func = 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.notify = notify_func
|
||||||
|
|
||||||
def _on_click(self):
|
def _on_click(self):
|
||||||
self.msg = str(self.peer_id)
|
self.msg = str(self.peer_id)
|
||||||
|
self.notify("нажат чат")
|
||||||
|
|
||||||
def compose(self):
|
def compose(self):
|
||||||
with Horizontal():
|
with Horizontal():
|
||||||
|
@ -4,6 +4,8 @@ from textual.widget import Widget
|
|||||||
from widgets.message import Message
|
from widgets.message import Message
|
||||||
|
|
||||||
class Dialog(Widget):
|
class Dialog(Widget):
|
||||||
|
"""Класс окна диалога"""
|
||||||
|
|
||||||
def __init__(self, id=None, classes=None, disabled=False):
|
def __init__(self, id=None, classes=None, disabled=False):
|
||||||
super().__init__(id=id, classes=classes, disabled=disabled)
|
super().__init__(id=id, classes=classes, disabled=disabled)
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ from textual.containers import Container
|
|||||||
from textual.widget import Widget
|
from textual.widget import Widget
|
||||||
|
|
||||||
class Message(Widget):
|
class Message(Widget):
|
||||||
|
"""Класс виджета сообщений для окна диалога"""
|
||||||
|
|
||||||
def __init__(self, name=None, message=None, is_me=None, id=None, classes=None, disabled=False):
|
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)
|
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
||||||
self.message = message
|
self.message = message
|
||||||
|
Loading…
x
Reference in New Issue
Block a user