1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00
royalnet/royalnet_telethon/bullet/contents/__init__.py

98 lines
3.1 KiB
Python
Raw Normal View History

2021-04-03 15:48:07 +00:00
from __future__ import annotations
import royalnet.royaltyping as t
import royalnet.engineer.bullet.casing as c
import royalnet.engineer.bullet.contents as co
import telethon as tt
import telethon.tl.types as tlt
import telethon.tl.custom as tlc
import async_property as ap
import datetime
2021-04-20 00:25:25 +00:00
from royalnet_telethon.formatting import tg_html_format
2021-04-03 15:48:07 +00:00
class TelegramMessage(co.Message):
def __init__(self, msg: tlc.Message):
super().__init__()
2021-04-03 17:10:38 +00:00
self._msg: tlc.Message = msg
2021-04-03 15:48:07 +00:00
def __hash__(self) -> int:
2021-04-03 17:10:38 +00:00
return self._msg.id
2021-04-03 15:48:07 +00:00
@ap.async_property
async def text(self) -> t.Optional[str]:
2021-04-03 17:10:38 +00:00
return self._msg.text
2021-04-03 15:48:07 +00:00
@ap.async_property
async def timestamp(self) -> t.Optional[datetime.datetime]:
2021-04-03 17:10:38 +00:00
return max(self._msg.date, self._msg.edit_date)
2021-04-03 15:48:07 +00:00
@ap.async_property
async def channel(self) -> t.Optional[TelegramChannel]:
2021-04-03 17:10:38 +00:00
channel: t.Union[tlt.Chat, tlt.User, tlt.Channel] = await self._msg.get_chat()
return TelegramChannel(channel=channel, client=self._msg.client)
2021-04-03 15:48:07 +00:00
@ap.async_property
async def sender(self) -> t.Optional[TelegramUser]:
2021-04-03 17:10:38 +00:00
sender: tlt.User = await self._msg.get_sender()
return TelegramUser(user=sender, client=self._msg.client)
2021-04-03 15:48:07 +00:00
async def reply(self, *,
text: str = None,
files: t.List[t.BinaryIO] = None) -> t.Optional[TelegramMessage]:
2021-04-20 00:25:25 +00:00
sent = await self._msg.reply(message=tg_html_format(text), file=files, parse_mode="HTML")
2021-04-03 15:48:07 +00:00
return TelegramMessage(msg=sent)
class TelegramChannel(co.Channel):
def __init__(self, channel: t.Union[tlt.Chat, tlt.User, tlt.Channel], client: tt.TelegramClient):
super().__init__()
2021-04-03 17:10:38 +00:00
self._channel: t.Union[tlt.Chat, tlt.User, tlt.Channel] = channel
self._client: tt.TelegramClient = client
2021-04-03 15:48:07 +00:00
def __hash__(self):
2021-04-03 17:10:38 +00:00
return self._channel.id
2021-04-03 15:48:07 +00:00
@ap.async_property
async def name(self) -> t.Optional[str]:
2021-04-03 17:10:38 +00:00
return self._channel.title
2021-04-03 15:48:07 +00:00
async def send_message(self, *,
text: str = None,
files: t.List[t.BinaryIO] = None) -> t.Optional[TelegramMessage]:
2021-04-20 00:25:25 +00:00
sent = await self._client.send_message(
self._channel,
message=tg_html_format(text),
file=files,
parse_mode="HTML"
)
2021-04-03 15:48:07 +00:00
return TelegramMessage(msg=sent)
class TelegramUser(co.User):
def __init__(self, user: tlt.User, client: tt.TelegramClient):
super().__init__()
2021-04-03 17:10:38 +00:00
self._user: tlt.User = user
self._client: tt.TelegramClient = client
2021-04-03 15:48:07 +00:00
def __hash__(self):
2021-04-03 17:10:38 +00:00
return self._user.id
2021-04-03 15:48:07 +00:00
@ap.async_property
async def name(self) -> t.Optional[str]:
2021-04-03 17:10:38 +00:00
if self._user.username:
return f"{self._user.username}"
elif self._user.last_name:
return f"{self._user.first_name} {self._user.last_name}"
return f"{self._user.first_name}"
2021-04-03 15:48:07 +00:00
async def slide(self) -> TelegramChannel:
2021-04-03 17:10:38 +00:00
return TelegramChannel(channel=self._user, client=self._client)
2021-04-03 15:48:07 +00:00
__all__ = (
"TelegramMessage",
"TelegramChannel",
"TelegramUser"
)