Telegram setMyCommands
This commit is contained in:
parent
e90df3d61e
commit
8f56e7f882
2 changed files with 31 additions and 5 deletions
|
|
@ -11,6 +11,7 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class StartCommand(TelegramCommand):
|
class StartCommand(TelegramCommand):
|
||||||
command_text = '/start'
|
command_text = '/start'
|
||||||
|
description = "Start a session with the bot"
|
||||||
|
|
||||||
async def exec_inner(self, text, update, *args, **kwargs):
|
async def exec_inner(self, text, update, *args, **kwargs):
|
||||||
username = update['message']['chat']['username']
|
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."
|
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):
|
class DomAddCommand(TelegramCommand):
|
||||||
|
command_text = "/dom_add"
|
||||||
command_regex = re.compile(r"^\/dom_add( (?P<username>@?\w+))$")
|
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):
|
async def exec_inner(self, text, update, session, *args, **kwargs):
|
||||||
dom_username = text.split(' ')[1]
|
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}")
|
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):
|
class DomRemoveCommand(TelegramCommand):
|
||||||
|
command_text = "/dom_remove"
|
||||||
command_regex = re.compile(r"^\/dom_remove( (?P<username>@?\w+))$")
|
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):
|
async def exec_inner(self, text, update, session, *args, **kwargs):
|
||||||
dom_username = text.split(' ')[1]
|
dom_username = text.split(' ')[1]
|
||||||
|
|
@ -77,6 +82,7 @@ class DomRemoveCommand(TelegramCommand):
|
||||||
|
|
||||||
class SkipDayAddCommand(TelegramCommand):
|
class SkipDayAddCommand(TelegramCommand):
|
||||||
command_text = "/skip_day"
|
command_text = "/skip_day"
|
||||||
|
description = "Add a skip day"
|
||||||
|
|
||||||
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
||||||
try:
|
try:
|
||||||
|
|
@ -97,6 +103,7 @@ class SkipDayAddCommand(TelegramCommand):
|
||||||
|
|
||||||
class SkipDayDeleteCommand(TelegramCommand):
|
class SkipDayDeleteCommand(TelegramCommand):
|
||||||
command_text = "/skip_day_delete"
|
command_text = "/skip_day_delete"
|
||||||
|
description = "Delete a skip day"
|
||||||
|
|
||||||
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
||||||
try:
|
try:
|
||||||
|
|
@ -118,6 +125,7 @@ class SkipDayDeleteCommand(TelegramCommand):
|
||||||
|
|
||||||
class SkipDaysCommand(TelegramCommand):
|
class SkipDaysCommand(TelegramCommand):
|
||||||
command_text = "/skip_days"
|
command_text = "/skip_days"
|
||||||
|
description = "List upcoming skip days"
|
||||||
|
|
||||||
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
||||||
user = user_get(update['message']['chat']['username'])
|
user = user_get(update['message']['chat']['username'])
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ class Telegram:
|
||||||
request
|
request
|
||||||
), json=data) as response:
|
), json=data) as response:
|
||||||
if not response.ok:
|
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))
|
raise Exception('{} {}'.format(request, response.status))
|
||||||
|
|
||||||
return await response.json()
|
return await response.json()
|
||||||
|
|
@ -47,6 +47,13 @@ class Telegram:
|
||||||
|
|
||||||
return await self.post('sendMessage', data=data)
|
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):
|
async def updates_get(self, offset=0, timeout=240):
|
||||||
return await self.get('getUpdates', params={
|
return await self.get('getUpdates', params={
|
||||||
'offset': offset,
|
'offset': offset,
|
||||||
|
|
@ -59,10 +66,13 @@ class TelegramCommand():
|
||||||
authorized = True
|
authorized = True
|
||||||
|
|
||||||
def matches(self, text):
|
def matches(self, text):
|
||||||
return (
|
if self.command_regex is not None:
|
||||||
(self.command_text is not None and text == self.command_text) or
|
return self.command_regex.match(text) is not None
|
||||||
(self.command_regex is not None and 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):
|
async def exec(self, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
|
|
@ -79,6 +89,14 @@ async def handle_commands(commands=[], loop=None):
|
||||||
async with make_session() as session:
|
async with make_session() as session:
|
||||||
t = Telegram(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_tasks = set()
|
||||||
command_futures = {}
|
command_futures = {}
|
||||||
def forResponse():
|
def forResponse():
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue