mirror of
https://github.com/Nick80835/microbot
synced 2025-08-31 06:26:04 +00:00
add maybe broken support for callback queries
This commit is contained in:
@@ -16,10 +16,12 @@ class CommandHandler():
|
||||
self.incoming_commands = {}
|
||||
self.inline_photo_commands = {}
|
||||
self.inline_article_commands = {}
|
||||
self.callback_queries = {}
|
||||
self.logger = logger
|
||||
self.settings = settings
|
||||
client.add_event_handler(self.handle_incoming, events.NewMessage(incoming=True))
|
||||
client.add_event_handler(self.handle_inline, events.InlineQuery())
|
||||
client.add_event_handler(self.handle_callback_query, events.CallbackQuery())
|
||||
|
||||
async def handle_incoming(self, event):
|
||||
prefix = escape(self.settings.get_config("cmd_prefix") or '.')
|
||||
@@ -136,6 +138,23 @@ class CommandHandler():
|
||||
except:
|
||||
pass
|
||||
|
||||
async def handle_callback_query(self, event):
|
||||
data_str = event.data.decode("utf-8")
|
||||
data_id = data_str.split("*")[0]
|
||||
data_data = data_str.lstrip(data_id + "*")
|
||||
|
||||
for key, value in self.callback_queries.items():
|
||||
if key == data_id:
|
||||
event.args = data_data
|
||||
event.extras = value["extras"]
|
||||
|
||||
try:
|
||||
await value["function"](event)
|
||||
except Exception as exception:
|
||||
self.logger.warn(f"{value['function'].__name__} - {exception}")
|
||||
await event.reply(f"`An error occurred in {value['function'].__name__}: {exception}`")
|
||||
raise exception
|
||||
|
||||
async def fallback_inline(self, event):
|
||||
defaults_dict = {**self.inline_photo_commands, **self.inline_article_commands}
|
||||
|
||||
|
@@ -35,6 +35,7 @@ class Loader():
|
||||
self.command_handler.incoming_commands = {}
|
||||
self.command_handler.inline_photo_commands = {}
|
||||
self.command_handler.inline_article_commands = {}
|
||||
self.command_handler.callback_queries = {}
|
||||
self.help_dict = {}
|
||||
|
||||
errors = ""
|
||||
@@ -99,6 +100,19 @@ class Loader():
|
||||
|
||||
return decorator
|
||||
|
||||
def add_callback_query(self, data_id=None, **args):
|
||||
data_id = args.get("data_id", data_id)
|
||||
|
||||
def decorator(func):
|
||||
self.command_handler.callback_queries[data_id] = {
|
||||
"function": func,
|
||||
"extras": args.get('extras', None)
|
||||
}
|
||||
|
||||
return func
|
||||
|
||||
return decorator
|
||||
|
||||
async def get_text(self, event, with_reply=True, return_msg=False, default=None):
|
||||
if event.args:
|
||||
if return_msg:
|
||||
|
Reference in New Issue
Block a user