Skip to content

Commit d648cce

Browse files
committed
Bots: Amend default-commands parameter to dispatch regular commands.
1 parent dfea028 commit d648cce

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

zulip_bots/zulip_bots/bots/helloworld_defaults/helloworld_defaults.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ def usage(self):
1515
sophisticated, bots using default commands.
1616
'''
1717

18+
def do_hi(self):
19+
return "Hi!"
20+
1821
def handle_message(self, message, bot_handler):
1922
default_commands_to_handle = ["", "about", "commands", "help"]
20-
other_commands = {"hello": "Says hello to the user."}
23+
other_commands = {"hello": ("Says hello to the user.", None),
24+
"hi": ("Says hi to the user.", self.do_hi),
25+
}
2126
default_response = bot_handler.dispatch_default_commands(message,
2227
default_commands_to_handle,
2328
self.META,
@@ -33,4 +38,5 @@ def handle_message(self, message, bot_handler):
3338
content = 'beep boop'
3439
bot_handler.send_reply(message, content)
3540

41+
3642
handler_class = HelloWorld_DefaultBot

zulip_bots/zulip_bots/bots/helloworld_defaults/test_helloworld_defaults.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ def test_bot(self):
2424
# "about"
2525
# "commands"
2626
# "help"
27+
28+
# "hi" is handled in the library too, though is an external function
29+
# so we can't test it?

zulip_bots/zulip_bots/lib.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
if False:
1818
from mypy_extensions import NoReturn
19-
from typing import Any, Optional, List, Dict, IO, Text, Set, Sequence
19+
from typing import Any, Optional, List, Dict, IO, Text, Set, Sequence, Tuple, Callable
2020
from types import ModuleType
2121

2222
from zulip import Client, ZulipError
@@ -221,14 +221,14 @@ def open(self, filepath):
221221
"files in their local directory.".format(abs_filepath))
222222

223223
def dispatch_default_commands(self, message, command_list, meta, other_commands=None):
224-
# type: (Dict[str, Any], Sequence[Text], Dict[Text, Text], Optional[Mapping[Text, Text]]) -> Optional[Text]
224+
# type: (Dict[str, Any], Sequence[Text], Dict[Text, Text], Optional[Mapping[Text, Tuple[Text, Optional[Callable[[], Text]]]]]) -> Optional[Text]
225225
supported_commands = OrderedDict([
226226
("", ""), # No help text, as this shouldn't appear in commands/help
227227
("about", "The brief type and use of this bot."),
228228
("commands", "A short list of the supported commands."),
229229
("help", "This help text."),
230230
])
231-
["", "about", "commands", "help"] # TODO: 'custom'
231+
["", "about", "commands", "help"]
232232

233233
# Check command_list has supported commands
234234
for requested_command in command_list:
@@ -253,11 +253,14 @@ def dispatch_default_commands(self, message, command_list, meta, other_commands=
253253
elif command == "help":
254254
cmd_list = OrderedDict([(cmd, supported_commands[cmd]) for cmd in command_list if cmd != ""])
255255
if other_commands is not None:
256-
cmd_list.update(other_commands)
256+
cmd_list.update(OrderedDict([(c, h[0]) for c, h in other_commands.items()]))
257257
help_text = ("**{name}**: {description}".format(**meta)+
258258
"\nThis bot supports the following commands:\n"+
259259
"\n".join(["**{}** - {}".format(c, h) for c, h in cmd_list.items()]))
260260
return help_text
261+
if other_commands is not None and command in other_commands:
262+
if other_commands[command][1] is not None:
263+
return other_commands[command][1]()
261264
return None
262265

263266
def extract_query_without_mention(message, client):

0 commit comments

Comments
 (0)