Telegram setMyCommands

This commit is contained in:
Johnny Gear 2026-03-06 19:12:28 -06:00
parent e90df3d61e
commit 8f56e7f882
2 changed files with 31 additions and 5 deletions

View file

@ -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 <username>` or ask a sub to add you."
class DomAddCommand(TelegramCommand):
command_text = "/dom_add"
command_regex = re.compile(r"^\/dom_add( (?P<username>@?\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<username>@?\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'])

View file

@ -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():