xkcd bot: Update bot & tests to use bot identity functionality.

This commit is contained in:
neiljp (Neil Pilgrim) 2018-04-08 15:51:25 -07:00 committed by showell
parent f15356c4c8
commit 2b86b9f488
2 changed files with 13 additions and 11 deletions

View file

@ -44,10 +44,11 @@ class TestXkcdBot(BotTestCase):
help_txt = "xkcd bot supports these commands:" help_txt = "xkcd bot supports these commands:"
err_txt = "xkcd bot only supports these commands, not `{}`:" err_txt = "xkcd bot only supports these commands, not `{}`:"
commands = ''' commands = '''
* `@xkcd help` to show this help message. * `{0} help` to show this help message.
* `@xkcd latest` to fetch the latest comic strip from xkcd. * `{0} latest` to fetch the latest comic strip from xkcd.
* `@xkcd random` to fetch a random comic strip from xkcd. * `{0} random` to fetch a random comic strip from xkcd.
* `@xkcd <comic id>` to fetch a comic strip based on `<comic id>` e.g `@xkcd 1234`.''' * `{0} <comic id>` to fetch a comic strip based on `<comic id>` e.g `{0} 1234`.'''.format(
"@**test-bot**")
self.verify_reply('', err_txt.format('') + commands) self.verify_reply('', err_txt.format('') + commands)
self.verify_reply('help', help_txt + commands) self.verify_reply('help', help_txt + commands)
# Example invalid command # Example invalid command

View file

@ -35,7 +35,8 @@ class XkcdHandler(object):
''' '''
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None: def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
xkcd_bot_response = get_xkcd_bot_response(message) quoted_name = bot_handler.identity().mention
xkcd_bot_response = get_xkcd_bot_response(message, quoted_name)
bot_handler.send_reply(message, xkcd_bot_response) bot_handler.send_reply(message, xkcd_bot_response)
class XkcdBotCommand(object): class XkcdBotCommand(object):
@ -49,16 +50,16 @@ class XkcdNotFoundError(Exception):
class XkcdServerError(Exception): class XkcdServerError(Exception):
pass pass
def get_xkcd_bot_response(message: Dict[str, str]) -> str: def get_xkcd_bot_response(message: Dict[str, str], quoted_name: str) -> str:
original_content = message['content'].strip() original_content = message['content'].strip()
command = original_content.strip() command = original_content.strip()
commands_help = ("%s" commands_help = ("%s"
"\n* `@xkcd help` to show this help message." "\n* `{0} help` to show this help message."
"\n* `@xkcd latest` to fetch the latest comic strip from xkcd." "\n* `{0} latest` to fetch the latest comic strip from xkcd."
"\n* `@xkcd random` to fetch a random comic strip from xkcd." "\n* `{0} random` to fetch a random comic strip from xkcd."
"\n* `@xkcd <comic id>` to fetch a comic strip based on `<comic id>` " "\n* `{0} <comic id>` to fetch a comic strip based on `<comic id>` "
"e.g `@xkcd 1234`.") "e.g `{0} 1234`.".format(quoted_name))
try: try:
if command == 'help': if command == 'help':