diff --git a/telegram/commands.py b/telegram/commands.py index 7707930..6d723fb 100644 --- a/telegram/commands.py +++ b/telegram/commands.py @@ -11,6 +11,7 @@ logger = logging.getLogger(__name__) class StartCommand(TelegramCommand): command_text = '/start' + description = "Start a session with the bot" async def exec_inner(self, text, update, *args, **kwargs): username = update['message']['chat']['username'] @@ -21,7 +22,9 @@ class StartCommand(TelegramCommand): yield "Welcome to Gear Orders bot.\n\nEdit your orders at {FLASK_URL}\n\nAdd a dom with `/dom_add ` or ask a sub to add you." class DomAddCommand(TelegramCommand): + command_text = "/dom_add" command_regex = re.compile(r"^\/dom_add( (?P@?\w+))$") + description = "Add a dom to your profile" async def exec_inner(self, text, update, session, *args, **kwargs): dom_username = text.split(' ')[1] @@ -48,7 +51,9 @@ class DomAddCommand(TelegramCommand): await t.message_send(dom.telegram_chat_id, f"@{sub_username} has added you as a dom. You may edit their orders at {FLASK_URL}") class DomRemoveCommand(TelegramCommand): + command_text = "/dom_remove" command_regex = re.compile(r"^\/dom_remove( (?P@?\w+))$") + description = "Remove a dom from your profile" async def exec_inner(self, text, update, session, *args, **kwargs): dom_username = text.split(' ')[1] @@ -77,6 +82,7 @@ class DomRemoveCommand(TelegramCommand): class SkipDayAddCommand(TelegramCommand): command_text = "/skip_day" + description = "Add a skip day" async def exec_inner(self, text, update, session, forResponse=None, reply=None): try: @@ -97,6 +103,7 @@ class SkipDayAddCommand(TelegramCommand): class SkipDayDeleteCommand(TelegramCommand): command_text = "/skip_day_delete" + description = "Delete a skip day" async def exec_inner(self, text, update, session, forResponse=None, reply=None): try: @@ -118,6 +125,7 @@ class SkipDayDeleteCommand(TelegramCommand): class SkipDaysCommand(TelegramCommand): command_text = "/skip_days" + description = "List upcoming skip days" async def exec_inner(self, text, update, session, forResponse=None, reply=None): user = user_get(update['message']['chat']['username']) diff --git a/telegram/telegram.py b/telegram/telegram.py index 53814ae..9175fcf 100644 --- a/telegram/telegram.py +++ b/telegram/telegram.py @@ -34,7 +34,7 @@ class Telegram: request ), json=data) as response: if not response.ok: - logger.error('{} {}'.format(request, response.status)) + logger.error('{} {} {}'.format(request, response.status, await response.json())) raise Exception('{} {}'.format(request, response.status)) return await response.json() @@ -47,6 +47,13 @@ class Telegram: return await self.post('sendMessage', data=data) + async def set_commands(self, commands): + data = { + 'commands': commands + } + + return await self.post('setMyCommands', data=data) + async def updates_get(self, offset=0, timeout=240): return await self.get('getUpdates', params={ 'offset': offset, @@ -59,10 +66,13 @@ class TelegramCommand(): authorized = True def matches(self, text): - return ( - (self.command_text is not None and text == self.command_text) or - (self.command_regex is not None and self.command_regex.match(text) is not None) - ) + if self.command_regex is not None: + return self.command_regex.match(text) is not None + + if self.command_text is not None: + return text == self.command_text + + return False async def exec(self, *args, **kwargs): try: @@ -79,6 +89,14 @@ async def handle_commands(commands=[], loop=None): async with make_session() as session: t = Telegram(session) + await t.set_commands([ + { + "command": c.command_text, + "description": c.description + } for c in commands + if c.command_text is not None and c.description is not None + ]) + command_tasks = set() command_futures = {} def forResponse():