Skip to content

Commit 5bb8f26

Browse files
committed
Bots: Add dispatch decorator for bot commands.
1 parent 102d340 commit 5bb8f26

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
@@ -92,6 +92,15 @@ def __init__(self, command, supported):
9292
def __str__(self):
9393
return self.msg
9494

95+
to_dispatch = {}
96+
def dispatch(command, help_text):
97+
def wrap(func):
98+
to_dispatch[command] = (help_text, func)
99+
def wrapped_f(*args, **kwargs):
100+
return func(*args, **kwargs)
101+
return wrapped_f
102+
return wrap
103+
95104
class ExternalBotHandler(object):
96105
def __init__(self, client, root_dir, bot_details={}):
97106
# type: (Client, str, Dict[str, Any]) -> None
@@ -215,18 +224,22 @@ def dispatch_default_commands(self, message, command_list, meta, other_commands=
215224
cmd_list = [cmd for cmd in command_list if cmd != ""]
216225
if other_commands is not None:
217226
cmd_list.extend(other_commands)
227+
cmd_list.extend(to_dispatch)
218228
return "**Commands**: " + ", ".join(cmd_list)
219229
elif command == "help":
220230
cmd_list = OrderedDict([(cmd, supported_commands[cmd]) for cmd in command_list if cmd != ""])
221231
if other_commands is not None:
222232
cmd_list.update(OrderedDict([(c, h[0]) for c, h in other_commands.items()]))
233+
cmd_list.update(OrderedDict([(c, h[0]) for c, h in to_dispatch.items()]))
223234
help_text = ("**{name}**: {description}".format(**meta)+
224235
"\nThis bot supports the following commands:\n"+
225236
"\n".join(["**{}** - {}".format(c, h) for c, h in cmd_list.items()]))
226237
return help_text
227238
if other_commands is not None and command in other_commands:
228239
if other_commands[command][1] is not None:
229240
return other_commands[command][1]()
241+
if command in to_dispatch:
242+
return to_dispatch[command][1]()
230243
return None
231244

232245
def extract_query_without_mention(message, client):

0 commit comments

Comments
 (0)