-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Bots: Add support for running packaged bots via entrypoints #708
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a87303b
zulip_botserver: Fix path finding for external bots.
PIG208 4fd29ba
bot_server: Reuse import_module_from_source to load bot modules from …
PIG208 4bc0c60
bots: Find external packaged bots via 'zulip_bots.registry' entry_point.
PIG208 66434d0
bots: Indicate source of bot (from source/module/registry) upon startup.
PIG208 745f2cd
zulip_bots: Add a boilerplate bot for external bots.
PIG208 285a946
bot_server: Add support for running botserver from bots registry.
PIG208 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 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,2 @@ | ||
This is a boilerplate package for a Zulip bot that can be installed from pip | ||
and launched using the `zulip-run-bots` command. |
This file contains 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 @@ | ||
__version__ = "1.0.0" |
This file contains 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,5 @@ | ||
Simple Zulip bot that will respond to any query with a "beep boop". | ||
|
||
The packaged_helloworld bot is a boilerplate bot that can be used as a | ||
template for more sophisticated/evolved Zulip bots that can be | ||
installed separately. |
30 changes: 30 additions & 0 deletions
30
packaged_helloworld/packaged_helloworld/packaged_helloworld.py
This file contains 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,30 @@ | ||
# See readme.md for instructions on running this code. | ||
from typing import Any, Dict | ||
|
||
import packaged_helloworld | ||
|
||
from zulip_bots.lib import BotHandler | ||
|
||
__version__ = packaged_helloworld.__version__ | ||
|
||
|
||
class HelloWorldHandler: | ||
def usage(self) -> str: | ||
return """ | ||
This is a boilerplate bot that responds to a user query with | ||
"beep boop", which is robot for "Hello World". | ||
|
||
This bot can be used as a template for other, more | ||
sophisticated, bots that can be installed separately. | ||
""" | ||
|
||
def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: | ||
content = "beep boop" # type: str | ||
bot_handler.send_reply(message, content) | ||
|
||
emoji_name = "wave" # type: str | ||
bot_handler.react(message, emoji_name) | ||
return | ||
|
||
|
||
handler_class = HelloWorldHandler |
This file contains 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,13 @@ | ||
import packaged_helloworld | ||
from setuptools import find_packages, setup | ||
|
||
package_info = { | ||
"name": "packaged_helloworld", | ||
"version": packaged_helloworld.__version__, | ||
"entry_points": { | ||
"zulip_bots.registry": ["packaged_helloworld=packaged_helloworld.packaged_helloworld"], | ||
}, | ||
"packages": find_packages(), | ||
} | ||
|
||
setup(**package_info) | ||
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from typing import Any, Dict | ||
from unittest import mock | ||
|
||
from zulip_bots.finder import metadata | ||
from zulip_bots.lib import BotHandler | ||
from zulip_botserver import server | ||
from zulip_botserver.input_parameters import parse_args | ||
|
@@ -273,6 +274,37 @@ def test_load_lib_modules(self) -> None: | |
).as_posix() | ||
module = server.load_lib_modules([path])[path] | ||
|
||
@mock.patch("zulip_botserver.server.app") | ||
@mock.patch("sys.argv", ["zulip-botserver", "--config-file", "/foo/bar/baz.conf"]) | ||
def test_load_from_registry(self, mock_app: mock.Mock) -> None: | ||
packaged_bot_module = mock.MagicMock(__version__="1.0.0", __file__="asd") | ||
packaged_bot_entrypoint = metadata.EntryPoint( | ||
"packaged_bot", "module_name", "zulip_bots.registry" | ||
) | ||
bots_config = { | ||
"packaged_bot": { | ||
"email": "[email protected]", | ||
"key": "value", | ||
"site": "http://localhost", | ||
"token": "abcd1234", | ||
} | ||
} | ||
|
||
with mock.patch( | ||
"zulip_botserver.server.read_config_file", return_value=bots_config | ||
), mock.patch("zulip_botserver.server.lib.ExternalBotHandler", new=mock.Mock()), mock.patch( | ||
"zulip_bots.finder.metadata.EntryPoint.load", | ||
return_value=packaged_bot_module, | ||
), mock.patch( | ||
"zulip_bots.finder.metadata.entry_points", | ||
return_value=(packaged_bot_entrypoint,), | ||
): | ||
server.main() | ||
|
||
mock_app.config.__setitem__.assert_any_call( | ||
"BOTS_LIB_MODULES", {"packaged_bot": packaged_bot_module} | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
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.
Hmm.. have we thought about if these will be installed alongside the
zulip_bots
PyPI package or not? @timabbott My guess is we should keep these as separate packages? Although, that adds more release work to our process.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.
Regardless, we need to improve our automated release process if we are going to do this. :)
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 think it won't be necessary. Since this package is only for demonstration and
zulip_bots
hardly rely on it.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.
Yeah, I think it's probably fine to have this be in the same repository for now. When we write documentation for this, I think what we'll actually want is a
zulip-new-bot bot_name
command that creates a directory of this form for you, so that we can have really simple steps for making a new bot.