Skip to content

Commit 1913dac

Browse files
committed
Bots: Add dispatch decorator for bot commands.
1 parent d648cce commit 1913dac

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

zulip_bots/zulip_bots/bots/helloworld_defaults/helloworld_defaults.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# See readme.md for instructions on running this code.
22

3+
from zulip_bots.lib import dispatch
4+
35
class HelloWorld_DefaultBot(object):
46
META = {
57
'name': 'HelloWorld-defaults',
@@ -38,5 +40,9 @@ def handle_message(self, message, bot_handler):
3840
content = 'beep boop'
3941
bot_handler.send_reply(message, content)
4042

43+
@dispatch("bye", "Says bye to the user.")
44+
def do_byebye():
45+
return "I'm not going anywhere!"
46+
4147

4248
handler_class = HelloWorld_DefaultBot

zulip_bots/zulip_bots/bots/helloworld_defaults/test_helloworld_defaults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ def test_bot(self):
2525
# "commands"
2626
# "help"
2727

28-
# "hi" is handled in the library too, though is an external function
28+
# "hi" and "bye" are handled in the library too, though is an external function
2929
# so we can't test it?

zulip_bots/zulip_bots/lib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ def __init__(self, command, supported):
9595
def __str__(self):
9696
return self.msg
9797

98+
to_dispatch = {}
99+
def dispatch(command, help_text):
100+
def wrap(func):
101+
to_dispatch[command] = (help_text, func)
102+
def wrapped_f(*args, **kwargs):
103+
return func(*args, **kwargs)
104+
return wrapped_f
105+
return wrap
106+
98107
class ExternalBotHandler(object):
99108
def __init__(self, client, root_dir, bot_details, bot_config_file):
100109
# type: (Client, str, Dict[str, Any], str) -> None
@@ -249,18 +258,22 @@ def dispatch_default_commands(self, message, command_list, meta, other_commands=
249258
cmd_list = [cmd for cmd in command_list if cmd != ""]
250259
if other_commands is not None:
251260
cmd_list.extend(other_commands)
261+
cmd_list.extend(to_dispatch)
252262
return "**Commands**: " + ", ".join(cmd_list)
253263
elif command == "help":
254264
cmd_list = OrderedDict([(cmd, supported_commands[cmd]) for cmd in command_list if cmd != ""])
255265
if other_commands is not None:
256266
cmd_list.update(OrderedDict([(c, h[0]) for c, h in other_commands.items()]))
267+
cmd_list.update(OrderedDict([(c, h[0]) for c, h in to_dispatch.items()]))
257268
help_text = ("**{name}**: {description}".format(**meta)+
258269
"\nThis bot supports the following commands:\n"+
259270
"\n".join(["**{}** - {}".format(c, h) for c, h in cmd_list.items()]))
260271
return help_text
261272
if other_commands is not None and command in other_commands:
262273
if other_commands[command][1] is not None:
263274
return other_commands[command][1]()
275+
if command in to_dispatch:
276+
return to_dispatch[command][1]()
264277
return None
265278

266279
def extract_query_without_mention(message, client):

0 commit comments

Comments
 (0)