Skip to content

Commit d0cc55c

Browse files
committed
bots: Add command-based default commands.
1 parent d2466f3 commit d0cc55c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

zulip_bots/zulip_bots/bots/xkcd/xkcd.py

Lines changed: 8 additions & 1 deletion
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

@@ -17,7 +19,12 @@ class XkcdHandler(object):
1719
META = {
1820
'name': 'XKCD',
1921
'description': 'Fetches comic strips from https://xkcd.com.',
20-
'default_commands_enabled': False,
22+
'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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,45 @@ def default_about_response():
179179
return "**{name}**".format(**bot_details)
180180
return "**{name}**: {description}".format(**bot_details)
181181

182+
def default_commands_response():
183+
return "**Commands**: {} {}".format(
184+
" ".join(name for name in command_defaults if name),
185+
" ".join(name for name in bot_details['commands'] if name))
186+
187+
def commands_list():
188+
return ("\n".join("**{}** - {}".format(name, options['help'])
189+
for name, options in command_defaults.items() if name) +
190+
"\n" +
191+
"\n".join("**{}** - {}".format(name, description)
192+
for name, description in bot_details['commands'].items() if name))
193+
194+
def default_help_response():
195+
return "{}\n{}\n{}".format(default_about_response(),
196+
message_handler.usage(), commands_list())
197+
182198
command_defaults = OrderedDict([ # Variable definition required for callbacks above
183199
('', {'action': default_empty_response,
184200
'help': "[BLANK MESSAGE NOT SHOWN]"}),
185201
('about', {'action': default_about_response,
186202
'help': "The type and use of this bot"}),
187203
('usage', {'action': lambda: message_handler.usage(),
188204
'help': "Bot-provided usage text"}),
205+
('commands', {'action': default_commands_response,
206+
'help': "A short list of supported commands"}),
207+
('help', {'action': default_help_response,
208+
'help': "This help text"}),
189209
])
190210
return command_defaults
191211

192212
def updated_default_commands(default_commands, bot_details):
193213
if not bot_details['default_commands_enabled']:
194214
return OrderedDict()
195-
updated = OrderedDict(default_commands)
215+
exclude_list = bot_details['commands'] or ('commands', 'help')
216+
updated = OrderedDict((name, option) for name, option in default_commands.items()
217+
if name not in exclude_list)
218+
# Update bot_details if updated is empty
219+
if len(updated) == 0:
220+
bot_details['default_commands_enabled'] = False
196221
return updated
197222

198223
def get_bot_details(bot_class, bot_name):

0 commit comments

Comments
 (0)