Skip to content

Commit 3c13a95

Browse files
committed
bots: Add command-based default commands.
1 parent 3a7cac9 commit 3c13a95

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

zulip_bots/zulip_bots/bots/xkcd/xkcd.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import logging
44
import requests
55

6+
from collections import OrderedDict
7+
68
XKCD_TEMPLATE_URL = 'https://xkcd.com/%s/info.0.json'
79
LATEST_XKCD_URL = 'https://xkcd.com/info.0.json'
810

@@ -18,6 +20,11 @@ class XkcdHandler(object):
1820
'name': 'XKCD',
1921
'description': 'Fetches comic strips from https://xkcd.com.',
2022
'default_commands_enabled': True,
23+
'commands': OrderedDict([
24+
('latest', "Show the latest comic strip"),
25+
('random', "Show a random comic strip"),
26+
('<comic id>', "Show a comic strip with a specific 'comic id'"),
27+
]) # NOTE: help not listed here, so default command used
2128
}
2229

2330
def usage(self):

zulip_bots/zulip_bots/lib.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,41 @@ def default_about_response():
198198
if bot_details['description'] == "":
199199
return "**{name}**".format(**bot_details)
200200
return "**{name}**: {description}".format(**bot_details)
201+
202+
def default_commands_response():
203+
return "**Commands**: {} {}".format(
204+
" ".join(name for name in command_defaults if name),
205+
" ".join(name for name in bot_details['commands'] if name))
206+
207+
def commands_list():
208+
return ("\n".join("**{}** - {}".format(name, options[1])
209+
for name, options in command_defaults.items() if name) +
210+
"\n" +
211+
"\n".join("**{}** - {}".format(name, description)
212+
for name, description in bot_details['commands'].items() if name))
213+
214+
def default_help_response():
215+
return "{}\n{}\n{}".format(default_about_response(),
216+
message_handler.usage(), commands_list())
201217
# FIXME Add boilerplate text above, to include eg. how to mention bot?
202218
# FIXME Should usage be included?
203219

204220
command_defaults = OrderedDict([ # Variable definition required for callbacks above
205221
('', (default_empty_response, "[BLANK MESSAGE NOT SHOWN]")),
206222
('about', (default_about_response, "The type and use of this bot")),
207223
('usage', ((lambda: message_handler.usage(), "Bot-provided usage text"))),
224+
('commands', (default_commands_response, "A short list of supported commands")),
225+
('help', (default_help_response, "This help text")),
208226
])
209227
return command_defaults
210228

229+
def updated_default_commands(default_commands, bot_details):
230+
if not bot_details['default_commands_enabled']:
231+
return OrderedDict()
232+
exclude_list = bot_details['commands'] or ('commands', 'help')
233+
return OrderedDict((name, option) for name, option in default_commands.items()
234+
if name not in exclude_list)
235+
211236
def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
212237
# type: (Any, bool, str) -> Any
213238
#
@@ -231,16 +256,17 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
231256
bot_details = {
232257
'name': bot_name.capitalize(),
233258
'description': "",
259+
# 'commands': {},
234260
'default_commands_enabled': True,
235261
}
236262
bot_details.update(getattr(lib_module.handler_class, 'META', {}))
237263

238264
# Initialise default commands, then override & sync with bot_details
239265
default_commands = setup_default_commands(bot_details, message_handler)
240-
if bot_details['default_commands_enabled']:
241-
updated_defaults = default_commands
242-
else:
243-
updated_defaults = OrderedDict()
266+
updated_defaults = updated_default_commands(default_commands, bot_details)
267+
# Update bot_details if updated_defaults is empty
268+
if len(updated_defaults) == 0:
269+
bot_details['default_commands_enabled'] = False
244270

245271
if not quiet:
246272
print("Running {} Bot:".format(bot_details['name']))

0 commit comments

Comments
 (0)