-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Dispatcher.set_commands #1911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dispatcher.set_commands #1911
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
|
||
from future.builtins import range | ||
|
||
from telegram import TelegramError, Update | ||
from telegram import TelegramError, Update, BotCommand | ||
from telegram.ext.handler import Handler | ||
from telegram.ext.callbackcontext import CallbackContext | ||
from telegram.utils.deprecate import TelegramDeprecationWarning | ||
|
@@ -529,3 +529,72 @@ def dispatch_error(self, update, error): | |
else: | ||
self.logger.exception( | ||
'No error handlers are registered, logging exception.', exc_info=error) | ||
|
||
def set_commands(self, add=True, update=True, delete=True, skip_empty=False, | ||
alphabetical=False): | ||
""" | ||
Convenience method or registering bot commands with Botfather. Uses | ||
:meth:`telegram.Bot.set_my_commands` for all :class:`telegram.ext.CommandHandler` s | ||
registered with this dispatcher. | ||
|
||
Note: | ||
While this method already allows for some customization, it is to be understood as a | ||
simple helper for the most common use cases. If you need to set your bots commands in | ||
a specific manner not covered by this method, use :meth:`telegram.Bot.set_my_commands` | ||
directly. | ||
|
||
Args: | ||
add (:obj:`bool`, optional): If there are commands set for the bot and :attr:`add` is | ||
:obj:`False`, no new commands are added. Defaults to :obj:`True`. | ||
update (:obj:`bool`, optional): Whether to override descriptions of commands already | ||
set. Defaults to :obj:`True`. | ||
delete (:obj:`bool`, optional): Whether to delete commands, which are currently set but | ||
don't have a corresponding :class:`telegram.ext.CommandHandler` listening for that | ||
command. Defaults to :obj:`True`. | ||
skip_empty (:obj:`bool`, optional): Whether to skip | ||
:class:`telegram.ext.CommandHandler` s with an empty description. If :obj:`False`, | ||
empty descriptions will be replaced by ``Command "<command>"``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defaults to :obj: |
||
alphabetical (:obj:`bool`, optional): Whether to sort commands by alphabetical order. | ||
If :obj:`False`, commands are sorted in the same order, in with :meth:`add_handler` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with -> which |
||
is invoked. If :attr:`delete` is :obj:`False`, commands without corresponding | ||
handler will be last. Defaults to :obj:`False`. | ||
""" | ||
set_bot_commands = self.bot.get_my_commands() | ||
|
||
dp_bot_commands = [] | ||
for group in self.handlers: | ||
for handler in self.handlers[group]: | ||
if hasattr(handler, 'command') and hasattr(handler, 'description'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather just do |
||
for cmd in handler.command: | ||
if handler.description or not skip_empty: | ||
desc = handler.description or 'Command "{}"'.format(cmd) | ||
dp_bot_commands.extend([BotCommand(cmd, desc)]) | ||
|
||
if not set_bot_commands: | ||
new_bot_commands = [bc for bc in dp_bot_commands] | ||
else: | ||
if add: | ||
new_bot_commands = [bc for bc in dp_bot_commands] | ||
new_commands = [nbc.command for nbc in new_bot_commands] | ||
new_bot_commands.extend([bc for bc in set_bot_commands | ||
if bc.command not in new_commands]) | ||
else: | ||
new_bot_commands = [bc for bc in set_bot_commands] | ||
|
||
if delete: | ||
dp_commands = [bc.command for bc in dp_bot_commands] | ||
new_bot_commands = [bc for bc in new_bot_commands if bc.command in dp_commands] | ||
|
||
if update: | ||
for bot_command in new_bot_commands: | ||
old_bc = next((bc for bc in set_bot_commands | ||
if bc.command == bot_command.command), None) | ||
new_bc = next((bc for bc in dp_bot_commands | ||
if bc.command == bot_command.command), None) | ||
if new_bc and old_bc and new_bc.description != old_bc.description: | ||
bot_command.description = new_bc.description | ||
|
||
if alphabetical: | ||
new_bot_commands = sorted(new_bot_commands, key=lambda c: c.command) | ||
|
||
self.bot.set_my_commands(new_bot_commands) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should remove this. We don't usually perform such checks as TG will just return BadRequest if invalid and more importantly, the limitations might change