Skip to content

Commit ccb9841

Browse files
committed
Response to comments.
1 parent 952b565 commit ccb9841

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class WikipediaHandler(object):
2121
META = {
2222
'name': 'Wikipedia',
2323
'description': 'Searches Wikipedia for a term and returns the top article.',
24-
'defaults': False, # Let bot handle all messages
24+
'default_commands_enabled': False,
2525
}
2626

2727
def usage(self):

zulip_bots/zulip_bots/bots/xkcd/xkcd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class XkcdHandler(object):
1919
META = {
2020
'name': 'XKCD',
2121
'description': 'Fetches comic strips from https://xkcd.com.',
22-
'defaults': True,
22+
'default_commands_enabled': True,
2323
'commands': OrderedDict([
2424
('latest', "Show the latest comic strip"),
2525
('random', "Show a random comic strip"),

zulip_bots/zulip_bots/lib.py

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -191,34 +191,50 @@ def is_private_message_from_another_user(message_dict, current_user_id):
191191
return False
192192

193193
def setup_default_commands(bot_details, message_handler):
194-
def def_about():
195-
if bot_details['description'] == "":
196-
return "**{}**".format(bot_details['name'])
197-
return "**{}**: {}".format(bot_details['name'], bot_details['description'])
194+
def default_empty_response():
195+
return "Oops. Your message was empty."
198196

199-
def def_help():
200-
return ("\n".join("**{}** - {}".format(k, v[1])
201-
for k, v in defaults.items() if k) + "\n" +
202-
"\n".join("**{}** - {}".format(k, v)
203-
for k, v in bot_details['commands'].items() if k))
197+
def default_about_response():
198+
if bot_details['description'] == "":
199+
return "**{name}**".format(**bot_details)
200+
return "**{name}**: {description}".format(**bot_details)
204201

205-
def def_commands():
202+
def default_commands_response():
206203
return "**Commands**: {} {}".format(
207-
" ".join(k for k in defaults if k),
208-
" ".join(k for k in bot_details['commands'] if k))
209-
defaults = OrderedDict([ # Variable definition required for callbacks above
210-
('', (lambda: "Oops. Your message was empty.", "[BLANK MESSAGE NOT SHOWN]")),
211-
('about', (def_about, "The type and use of this bot")),
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())
217+
# FIXME Add boilerplate text above, to include eg. how to mention bot?
218+
# Should usage be included?
219+
220+
command_defaults = OrderedDict([ # Variable definition required for callbacks above
221+
('', (default_empty_response, "[BLANK MESSAGE NOT SHOWN]")),
222+
('about', (default_about_response, "The type and use of this bot")),
212223
('usage', ((lambda: message_handler.usage(), "Bot-provided usage text"))),
213-
('help', (lambda: "{}\n{}\n{}".format(def_about(), message_handler.usage(), def_help()),
214-
"This help text")),
215-
('commands', (def_commands, "A short list of supported commands"))
224+
('help', (default_help_response, "This help text")),
225+
('commands', (default_commands_response, "A short list of supported commands"))
216226
])
217-
return defaults
227+
return command_defaults
228+
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)
218235

219-
def sync_botdetails_defaultcommands(bot_details, default_commands):
220236
# Update default_commands from any changes in bot_details
221-
if not bot_details['defaults']: # Bot class will handle all commands
237+
if not bot_details['default_commands_enabled']: # Bot class will handle all commands
222238
default_commands = {}
223239
else:
224240
if len(bot_details['commands']) == 0: # No commands specified, so don't use this feature
@@ -228,9 +244,6 @@ def sync_botdetails_defaultcommands(bot_details, default_commands):
228244
for command in bot_details['commands']: # Bot commands override defaults
229245
if command in default_commands:
230246
del default_commands[command]
231-
# Sync default_commands changes with bot_details
232-
if len(default_commands) == 0:
233-
bot_details['defaults'] = False
234247

235248
def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
236249
# type: (Any, bool, str) -> Any
@@ -256,13 +269,16 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
256269
'name': bot_name.capitalize(),
257270
'description': "",
258271
'commands': {},
259-
'defaults': True,
272+
'default_commands_enabled': True,
260273
}
261274
bot_details.update(getattr(lib_module.handler_class, 'META', {}))
262275

263276
# Initialise default commands, then override & sync with bot_details
264277
default_commands = setup_default_commands(bot_details, message_handler)
265-
sync_botdetails_defaultcommands(bot_details, default_commands)
278+
updated_defaults = updated_default_commands(default_commands, bot_details)
279+
# Update bot_details if updated_defaults is empty
280+
if len(updated_defaults) == 0:
281+
bot_details['default_commands_enabled'] = False
266282

267283
if not quiet:
268284
print("Running {} Bot:".format(bot_details['name']))
@@ -288,16 +304,14 @@ def handle_message(message):
288304
return
289305

290306
if is_private_message or is_mentioned:
291-
# Handle any default_commands first
292-
if len(default_commands) > 0:
293-
if '' in default_commands and len(message['content']) == 0:
294-
restricted_client.send_reply(message, default_commands[''][0]())
295-
return
296-
for command in default_commands:
297-
if command == '':
298-
continue
299-
if message['content'].startswith(command):
300-
restricted_client.send_reply(message, default_commands[command][0]())
307+
# Handle any default commands first
308+
if len(updated_defaults) > 0:
309+
for command in updated_defaults:
310+
if command == '' and len(message['content']) == 0:
311+
restricted_client.send_reply(message, updated_defaults[''][0]())
312+
return
313+
if command == message['content']:
314+
restricted_client.send_reply(message, updated_defaults[command][0]())
301315
return
302316
# ...then pass anything else to bot to deal with
303317
message_handler.handle_message(

0 commit comments

Comments
 (0)