-
-
Notifications
You must be signed in to change notification settings - Fork 392
Bots: Add default (and other) command dispatch #169
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
7
commits into
zulip:main
Choose a base branch
from
neiljp:command_registry
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
7 commits
Select commit
Hold shift + click to select a range
54e8826
Bots: Add default-command skeleton in lib and test_lib.
neiljp 99a3d80
Bots: Add helloworld_defaults bot: helloworld with default commands.
neiljp cfba702
Bots: Add 'commands' default command & use in helloworld_defaults bot.
neiljp 6692603
Bots: Add 'help' default command & use in helloworld_defaults bot.
neiljp dfea028
Helloworld_defaults: Add sample 'other_commands' to illustrate feature.
neiljp d648cce
Bots: Amend default-commands parameter to dispatch regular commands.
neiljp 1913dac
Bots: Add dispatch decorator for bot 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
Empty file.
48 changes: 48 additions & 0 deletions
48
zulip_bots/zulip_bots/bots/helloworld_defaults/helloworld_defaults.py
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# See readme.md for instructions on running this code. | ||
|
||
from zulip_bots.lib import dispatch | ||
|
||
class HelloWorld_DefaultBot(object): | ||
META = { | ||
'name': 'HelloWorld-defaults', | ||
'description': 'Minimal bot using default commands.', | ||
} | ||
def usage(self): | ||
return ''' | ||
This is a simple bot that responds to *most* user queries with | ||
"beep boop", which is robot for "Hello World"; others are | ||
dealt with by the default-command system. | ||
|
||
This bot can be used as a template for other, more | ||
sophisticated, bots using default commands. | ||
''' | ||
|
||
def do_hi(self): | ||
return "Hi!" | ||
|
||
def handle_message(self, message, bot_handler): | ||
default_commands_to_handle = ["", "about", "commands", "help"] | ||
other_commands = {"hello": ("Says hello to the user.", None), | ||
"hi": ("Says hi to the user.", self.do_hi), | ||
} | ||
default_response = bot_handler.dispatch_default_commands(message, | ||
default_commands_to_handle, | ||
self.META, | ||
other_commands) | ||
if default_response is not None: | ||
bot_handler.send_reply(message, default_response) | ||
return | ||
|
||
if message['content'].startswith('hello'): | ||
bot_handler.send_reply(message, "Hello!") | ||
return | ||
|
||
content = 'beep boop' | ||
bot_handler.send_reply(message, content) | ||
|
||
@dispatch("bye", "Says bye to the user.") | ||
def do_byebye(): | ||
return "I'm not going anywhere!" | ||
|
||
|
||
handler_class = HelloWorld_DefaultBot |
29 changes: 29 additions & 0 deletions
29
zulip_bots/zulip_bots/bots/helloworld_defaults/test_helloworld_defaults.py
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import absolute_import | ||
from __future__ import print_function | ||
|
||
from six.moves import zip | ||
|
||
from zulip_bots.test_lib import BotTestCase | ||
|
||
class TestHelloWorldDefaultsBot(BotTestCase): | ||
bot_name = "helloworld_defaults" | ||
|
||
def test_bot(self): | ||
|
||
# Check for some possible inputs, which should all be responded to with txt | ||
txt = "beep boop" | ||
beep_messages = ["foo", "Hi, my name is abc"] | ||
self.check_expected_responses(list(zip(beep_messages, len(beep_messages)*[txt]))) | ||
|
||
self.check_expected_responses([("hello", "Hello!")]) | ||
|
||
# Don't check for these, as they are handled by default in the library | ||
# "" | ||
# "about" | ||
# "commands" | ||
# "help" | ||
|
||
# "hi" and "bye" are handled in the library too, though is an external function | ||
# so we can't test it? |
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.
This doesn't feel right, having
to_dispatch
being a module-level variable.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.
Would you prefer this in eg.
ExternalBotHandler
? Or in a separate class?Is this approach preferable to #66?