-
-
Notifications
You must be signed in to change notification settings - Fork 392
Bots: Add metadata scheme for bots. #66
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
Open
neiljp
wants to merge
6
commits into
zulip:main
Choose a base branch
from
neiljp:default_commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ab2312
bots: Refactor bot_details handling into function.
neiljp 18d7df9
bots: Add (optional) minimal default commands (empty, about, usage).
neiljp 16544d5
bots: Refactor updating default commands into updated_default_commands.
neiljp 97579be
bot testing: Support testing of default commands.
neiljp 5d4e5b4
bots: Enable zulip_bot_output.py to understand default commands.
neiljp 34b5f3a
bots: Add command-based default commands.
neiljp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
|
||
from zulip import Client | ||
|
||
from collections import OrderedDict | ||
|
||
def exit_gracefully(signum, frame): | ||
# type: (int, Optional[Any]) -> None | ||
sys.exit(0) | ||
|
@@ -168,6 +170,66 @@ def is_private_message_from_another_user(message_dict, current_user_id): | |
return current_user_id != message_dict['sender_id'] | ||
return False | ||
|
||
def setup_default_commands(bot_details, message_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. I suppose this is the nucleus of the PR. |
||
def default_empty_response(): | ||
return "You sent the bot an empty message; perhaps try 'about', 'help' or 'usage'." | ||
|
||
def default_about_response(): | ||
if bot_details['description'] == "": | ||
return "**{name}**".format(**bot_details) | ||
return "**{name}**: {description}".format(**bot_details) | ||
|
||
def default_commands_response(): | ||
return "**Commands**: {} {}".format( | ||
" ".join(name for name in command_defaults if name), | ||
" ".join(name for name in bot_details['commands'] if name)) | ||
|
||
def commands_list(): | ||
return ("\n".join("**{}** - {}".format(name, options['help']) | ||
for name, options in command_defaults.items() if name) + | ||
"\n" + | ||
"\n".join("**{}** - {}".format(name, description) | ||
for name, description in bot_details['commands'].items() if name)) | ||
|
||
def default_help_response(): | ||
return "{}\n{}\n{}".format(default_about_response(), | ||
message_handler.usage(), commands_list()) | ||
|
||
command_defaults = OrderedDict([ # Variable definition required for callbacks above | ||
('', {'action': default_empty_response, | ||
'help': "[BLANK MESSAGE NOT SHOWN]"}), | ||
('about', {'action': default_about_response, | ||
'help': "The type and use of this bot"}), | ||
('usage', {'action': lambda: message_handler.usage(), | ||
'help': "Bot-provided usage text"}), | ||
('commands', {'action': default_commands_response, | ||
'help': "A short list of supported commands"}), | ||
('help', {'action': default_help_response, | ||
'help': "This help text"}), | ||
]) | ||
return command_defaults | ||
|
||
def updated_default_commands(default_commands, bot_details): | ||
if not bot_details['default_commands_enabled']: | ||
return OrderedDict() | ||
exclude_list = bot_details['commands'] or ('commands', 'help') | ||
updated = OrderedDict((name, option) for name, option in default_commands.items() | ||
if name not in exclude_list) | ||
# Update bot_details if updated is empty | ||
if len(updated) == 0: | ||
bot_details['default_commands_enabled'] = False | ||
return updated | ||
|
||
def get_bot_details(bot_class, bot_name): | ||
Contributor
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. LGTm for the commit ffc977a |
||
bot_details = { | ||
'name': bot_name.capitalize(), | ||
'description': "", | ||
'commands': {}, | ||
'default_commands_enabled': True, | ||
} | ||
bot_details.update(getattr(bot_class, 'META', {})) | ||
return bot_details | ||
|
||
def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): | ||
# type: (Any, bool, str, str) -> Any | ||
# | ||
|
@@ -186,11 +248,11 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): | |
message_handler.initialize(bot_handler=restricted_client) | ||
|
||
# Set default bot_details, then override from class, if provided | ||
bot_details = { | ||
'name': bot_name.capitalize(), | ||
'description': "", | ||
} | ||
bot_details.update(getattr(lib_module.handler_class, 'META', {})) | ||
bot_details = get_bot_details(message_handler, bot_name) | ||
|
||
# Initialise default commands, then override & sync with bot_details | ||
default_commands = setup_default_commands(bot_details, message_handler) | ||
updated_defaults = updated_default_commands(default_commands, bot_details) | ||
|
||
if not quiet: | ||
print("Running {} Bot:".format(bot_details['name'])) | ||
|
@@ -216,6 +278,13 @@ def handle_message(message, flags): | |
return | ||
|
||
if is_private_message or is_mentioned: | ||
# Handle any default commands first | ||
for command in updated_defaults: | ||
if command == message['content']: | ||
restricted_client.send_reply(message, | ||
updated_defaults[command]['action']()) | ||
return | ||
# ...then pass anything else to bot to deal with | ||
message_handler.handle_message( | ||
message=message, | ||
bot_handler=restricted_client | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't understand this comment.
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.
In this bot, with this PR, help is now handled internally by the lib, so the default command is used instead as it stands. If 'help' was listed in
commands
then the bot wouldn't handle it and the help text would be as specified in the tuple.