1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/royalnet_telethon/pda.py

158 lines
4.6 KiB
Python
Raw Normal View History

2021-04-03 15:48:07 +00:00
"""
The PDA ("main" class) for the :mod:`royalnet_telethon` frontend.
"""
from __future__ import annotations
import royalnet.royaltyping as t
import logging
import royalnet.engineer as engi
import telethon as tt
import telethon.tl.custom as tlc
import enum
from .bullet.projectiles.message import TelegramMessageReceived, TelegramMessageEdited, TelegramMessageDeleted
log = logging.getLogger(__name__)
class TelethonPDAMode(enum.Enum):
2021-04-14 02:12:54 +00:00
"""
.. todo:: Document this.
"""
2021-04-03 15:48:07 +00:00
GLOBAL = enum.auto()
CHAT = enum.auto()
USER = enum.auto()
CHAT_USER = enum.auto()
2021-04-14 02:12:54 +00:00
class TelethonPDAImplementation(engi.ConversationListImplementation):
2021-04-03 15:48:07 +00:00
"""
2021-04-14 02:12:54 +00:00
.. todo:: Document this.
2021-04-03 15:48:07 +00:00
"""
2021-04-14 02:12:54 +00:00
@property
def namespace(self):
return "telethon"
2021-04-03 15:48:07 +00:00
2021-04-14 02:12:54 +00:00
def __init__(self, name: str, tg_api_id: int, tg_api_hash: str, bot_username: str, bot_token: str,
2021-04-19 01:24:17 +00:00
mode: TelethonPDAMode = TelethonPDAMode.CHAT_USER):
2021-04-03 15:48:07 +00:00
2021-04-19 01:24:17 +00:00
super().__init__(name=name)
2021-04-03 15:48:07 +00:00
2021-04-14 02:12:54 +00:00
self.mode: TelethonPDAMode = mode
2021-04-03 15:48:07 +00:00
"""
2021-04-14 02:12:54 +00:00
The mode to use for mapping dispensers.
2021-04-03 15:48:07 +00:00
"""
2021-04-14 02:12:54 +00:00
self.tg_api_id: int = tg_api_id
"""
.. todo:: Document this.
"""
2021-04-03 15:48:07 +00:00
2021-04-14 02:12:54 +00:00
self.tg_api_hash: str = tg_api_hash
2021-04-03 15:48:07 +00:00
"""
2021-04-14 02:12:54 +00:00
.. todo:: Document this.
2021-04-03 15:48:07 +00:00
"""
self.bot_username: str = bot_username
2021-04-14 02:12:54 +00:00
"""
.. todo:: Document this.
"""
2021-04-03 15:48:07 +00:00
2021-04-14 02:12:54 +00:00
self.bot_token: str = bot_token
"""
.. todo:: Document this.
"""
def _register_events(self, client):
"""
.. todo:: Document this.
"""
self.log.info("Registering Telethon events...")
self.log.debug("Registering NewMessage event...")
client.add_event_handler(callback=self._message_new, event=tt.events.NewMessage())
self.log.debug("Registering MessageEdited event...")
client.add_event_handler(callback=self._message_edit, event=tt.events.MessageEdited())
self.log.debug("Registering MessageDeleted event...")
client.add_event_handler(callback=self._message_delete, event=tt.events.MessageDeleted())
2021-04-03 17:10:38 +00:00
# self._client.add_event_handler(callback=self._message_read, _event=tt.events.MessageRead())
# self._client.add_event_handler(callback=self._chat_action, _event=tt.events.ChatAction())
# self._client.add_event_handler(callback=self._user_update, _event=tt.events.UserUpdate())
# self._client.add_event_handler(callback=self._callback_query, _event=tt.events.CallbackQuery())
# self._client.add_event_handler(callback=self._inline_query, _event=tt.events.InlineQuery())
# self._client.add_event_handler(callback=self._album, _event=tt.events.Album())
2021-04-03 15:48:07 +00:00
def _determine_key(self, event: tlc.message.Message):
2021-04-14 02:12:54 +00:00
"""
.. todo:: Document this.
"""
2021-04-03 15:48:07 +00:00
if self.mode == TelethonPDAMode.GLOBAL:
return None
elif self.mode == TelethonPDAMode.USER:
2021-04-03 16:38:22 +00:00
if event.from_id:
return event.from_id.user_id
else:
return event.peer_id.user_id
2021-04-03 15:48:07 +00:00
elif self.mode == TelethonPDAMode.CHAT:
return event.chat_id
elif self.mode == TelethonPDAMode.CHAT_USER:
2021-04-03 16:38:22 +00:00
if event.from_id:
return event.chat_id, event.from_id.user_id
else:
return event.chat_id, event.peer_id.user_id
2021-04-03 15:48:07 +00:00
else:
raise TypeError("Invalid mode")
async def _message_new(self, event: tlc.message.Message):
2021-04-14 02:12:54 +00:00
"""
.. todo:: Document this.
"""
2021-04-17 01:25:27 +00:00
await self.put(
2021-04-03 15:48:07 +00:00
key=self._determine_key(event),
2021-04-14 02:12:54 +00:00
projectile=TelegramMessageReceived(event=event),
2021-04-03 15:48:07 +00:00
)
async def _message_edit(self, event: tlc.message.Message):
2021-04-14 02:12:54 +00:00
"""
.. todo:: Document this.
"""
2021-04-03 15:48:07 +00:00
2021-04-17 01:25:27 +00:00
await self.put(
2021-04-03 15:48:07 +00:00
key=self._determine_key(event),
2021-04-14 02:12:54 +00:00
projectile=TelegramMessageEdited(event=event),
2021-04-03 15:48:07 +00:00
)
2021-04-14 02:12:54 +00:00
async def _message_delete(self, event: tlc.message.Message):
2021-04-03 15:48:07 +00:00
"""
2021-04-14 02:12:54 +00:00
.. todo:: Document this.
2021-04-03 15:48:07 +00:00
"""
2021-04-17 01:25:27 +00:00
await self.put(
2021-04-14 02:12:54 +00:00
key=self._determine_key(event),
projectile=TelegramMessageDeleted(event=event),
)
2021-04-03 15:48:07 +00:00
2021-04-14 02:12:54 +00:00
async def run(self) -> t.NoReturn:
client = tt.TelegramClient(
"bot",
api_id=self.tg_api_id,
api_hash=self.tg_api_hash
)
2021-04-03 15:48:07 +00:00
2021-04-14 02:12:54 +00:00
await client.start(bot_token=self.bot_token)
try:
self._register_events(client)
await client.run_until_disconnected()
finally:
await client.disconnect()
2021-04-03 15:48:07 +00:00
__all__ = (
2021-04-14 02:12:54 +00:00
"TelethonPDAImplementation",
2021-04-03 15:48:07 +00:00
)