Skip to content

Commit 102d340

Browse files
committed
Bots: Amend default-commands parameter to dispatch regular commands.
1 parent 8b7adde commit 102d340

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
@@ -187,14 +187,14 @@ def open(self, filepath):
187187
"files in their local directory.".format(abs_filepath))
188188

189189
def dispatch_default_commands(self, message, command_list, meta, other_commands=None):
190-
# type: (Dict[str, Any], Sequence[Text], Dict[Text, Text], Optional[Mapping[Text, Text]]) -> Optional[Text]
190+
# type: (Dict[str, Any], Sequence[Text], Dict[Text, Text], Optional[Mapping[Text, Tuple[Text, Optional[Callable[[], Text]]]]]) -> Optional[Text]
191191
supported_commands = OrderedDict([
192192
("", ""), # No help text, as this shouldn't appear in commands/help
193193
("about", "The brief type and use of this bot."),
194194
("commands", "A short list of the supported commands."),
195195
("help", "This help text."),
196196
])
197-
["", "about", "commands", "help"] # TODO: 'custom'
197+
["", "about", "commands", "help"]
198198

199199
# Check command_list has supported commands
200200
for requested_command in command_list:
@@ -219,11 +219,14 @@ def dispatch_default_commands(self, message, command_list, meta, other_commands=
219219
elif command == "help":
220220
cmd_list = OrderedDict([(cmd, supported_commands[cmd]) for cmd in command_list if cmd != ""])
221221
if other_commands is not None:
222-
cmd_list.update(other_commands)
222+
cmd_list.update(OrderedDict([(c, h[0]) for c, h in other_commands.items()]))
223223
help_text = ("**{name}**: {description}".format(**meta)+
224224
"\nThis bot supports the following commands:\n"+
225225
"\n".join(["**{}** - {}".format(c, h) for c, h in cmd_list.items()]))
226226
return help_text
227+
if other_commands is not None and command in other_commands:
228+
if other_commands[command][1] is not None:
229+
return other_commands[command][1]()
227230
return None
228231

229232
def extract_query_without_mention(message, client):

0 commit comments

Comments
 (0)