From c6d294385f2bbed7aada0471c33846a2df34acb0 Mon Sep 17 00:00:00 2001 From: derAnfaenger Date: Wed, 24 May 2017 21:49:44 +0200 Subject: [PATCH] bots: Refactor bots to use send_reply. The send_reply function makes it easier for bots to send messages. This commit updates all bots to make use of this function, when possible. --- contrib_bots/bots/commute/commute.py | 14 +------------- contrib_bots/bots/converter/converter.py | 14 +------------- contrib_bots/bots/foursquare/foursquare.py | 14 +------------- contrib_bots/bots/giphy/giphy.py | 15 +-------------- contrib_bots/bots/googlesearch/googlesearch.py | 16 +--------------- contrib_bots/bots/helloworld/helloworld.py | 8 +------- contrib_bots/bots/john/john.py | 15 +-------------- contrib_bots/bots/thesaurus/thesaurus.py | 16 +--------------- contrib_bots/bots/yoda/yoda.py | 13 +++++-------- 9 files changed, 13 insertions(+), 112 deletions(-) diff --git a/contrib_bots/bots/commute/commute.py b/contrib_bots/bots/commute/commute.py index 08ef85b..8a55afa 100644 --- a/contrib_bots/bots/commute/commute.py +++ b/contrib_bots/bots/commute/commute.py @@ -90,19 +90,7 @@ class CommuteHandler(object): # determines if bot will respond as a private message/ stream message def send_info(self, message, letter, client): - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=message['sender_email'], - content=letter, - )) - else: - client.send_message(dict( - type='stream', - subject=message['subject'], - to=message['display_recipient'], - content=letter, - )) + client.send_reply(message, letter) def calculate_seconds(self, time_str): times = time_str.split(',') diff --git a/contrib_bots/bots/converter/converter.py b/contrib_bots/bots/converter/converter.py index 27e4ad9..9658d75 100644 --- a/contrib_bots/bots/converter/converter.py +++ b/contrib_bots/bots/converter/converter.py @@ -49,19 +49,7 @@ class ConverterHandler(object): def handle_message(self, message, client, state_handler): bot_response = get_bot_converter_response(message, client) - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=message['sender_email'], - content=bot_response, - )) - else: - client.send_message(dict( - type='stream', - to=message['display_recipient'], - subject=message['subject'], - content=bot_response, - )) + client.send_reply(message, bot_response) def get_bot_converter_response(message, client): content = message['content'] diff --git a/contrib_bots/bots/foursquare/foursquare.py b/contrib_bots/bots/foursquare/foursquare.py index 29e1f60..e71a18a 100644 --- a/contrib_bots/bots/foursquare/foursquare.py +++ b/contrib_bots/bots/foursquare/foursquare.py @@ -60,19 +60,7 @@ Example Inputs: return '\n'.join(format_venue(venue) for venue in venues) def send_info(self, message, letter, client): - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=message['sender_email'], - content=letter, - )) - else: - client.send_message(dict( - type='stream', - subject=message['subject'], - to=message['display_recipient'], - content=letter, - )) + client.send_reply(message, letter) def handle_message(self, message, client, state_handler): words = message['content'].split() diff --git a/contrib_bots/bots/giphy/giphy.py b/contrib_bots/bots/giphy/giphy.py index 9d352fc..db93dc1 100644 --- a/contrib_bots/bots/giphy/giphy.py +++ b/contrib_bots/bots/giphy/giphy.py @@ -35,20 +35,7 @@ class GiphyHandler(object): def handle_message(self, message, client, state_handler): bot_response = get_bot_giphy_response(message, client) - - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=message['sender_email'], - content=bot_response, - )) - else: - client.send_message(dict( - type='stream', - to=message['display_recipient'], - subject=message['subject'], - content=bot_response, - )) + client.send_reply(message, bot_response) class GiphyNoResultException(Exception): diff --git a/contrib_bots/bots/googlesearch/googlesearch.py b/contrib_bots/bots/googlesearch/googlesearch.py index 8863345..73aec56 100644 --- a/contrib_bots/bots/googlesearch/googlesearch.py +++ b/contrib_bots/bots/googlesearch/googlesearch.py @@ -74,22 +74,8 @@ class GoogleSearchHandler(object): def handle_message(self, message, client, state_handler): original_content = message['content'] - original_sender = message['sender_email'] result = get_google_result(original_content) - - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=original_sender, - content=result, - )) - else: - client.send_message(dict( - type=message['type'], - to=message['display_recipient'], - subject=message['subject'], - content=result, - )) + client.send_reply(message, result) handler_class = GoogleSearchHandler diff --git a/contrib_bots/bots/helloworld/helloworld.py b/contrib_bots/bots/helloworld/helloworld.py index c6904a9..95ca03f 100644 --- a/contrib_bots/bots/helloworld/helloworld.py +++ b/contrib_bots/bots/helloworld/helloworld.py @@ -13,12 +13,6 @@ class HelloWorldHandler(object): def handle_message(self, message, client, state_handler): content = 'beep boop' - - client.send_message(dict( - type='stream', - to=message['display_recipient'], - subject=message['subject'], - content=content, - )) + client.send_reply(message, content) handler_class = HelloWorldHandler diff --git a/contrib_bots/bots/john/john.py b/contrib_bots/bots/john/john.py index 21a9b86..29e0319 100644 --- a/contrib_bots/bots/john/john.py +++ b/contrib_bots/bots/john/john.py @@ -118,19 +118,6 @@ class JohnHandler(object): def handle_message(self, message, client, state_handler): original_content = message['content'] bot_response = str(bota.get_response(original_content)) - - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=message['sender_email'], - content=bot_response, - )) - else: - client.send_message(dict( - type='stream', - to=message['display_recipient'], - subject=message['subject'], - content=bot_response, - )) + client.send_reply(message, bot_response) handler_class = JohnHandler diff --git a/contrib_bots/bots/thesaurus/thesaurus.py b/contrib_bots/bots/thesaurus/thesaurus.py index 44345e8..56fa404 100644 --- a/contrib_bots/bots/thesaurus/thesaurus.py +++ b/contrib_bots/bots/thesaurus/thesaurus.py @@ -62,21 +62,7 @@ class ThesaurusHandler(object): def handle_message(self, message, client, state_handler): original_content = message['content'].strip() - original_sender = message['sender_email'] new_content = get_thesaurus_result(original_content) - - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=original_sender, - content=new_content, - )) - else: - client.send_message(dict( - type=message['type'], - to=message['display_recipient'], - subject=message['subject'], - content=new_content, - )) + client.send_reply(message, new_content) handler_class = ThesaurusHandler diff --git a/contrib_bots/bots/yoda/yoda.py b/contrib_bots/bots/yoda/yoda.py index cc5b132..a87049f 100644 --- a/contrib_bots/bots/yoda/yoda.py +++ b/contrib_bots/bots/yoda/yoda.py @@ -50,11 +50,7 @@ class YodaSpeakHandler(object): ''' def handle_message(self, message, client, state_handler): - original_content = message['content'] - stream = message['display_recipient'] - subject = message['subject'] - - handle_input(client, original_content, stream, subject) + handle_input(message, client) handler_class = YodaSpeakHandler @@ -90,10 +86,11 @@ def format_input(original_content): return sentence -def handle_input(client, original_content, stream, subject): +def handle_input(message, client): + original_content = message['content'] if is_help(original_content): - send_message(client, HELP_MESSAGE, stream, subject) + client.send_reply(message, HELP_MESSAGE) else: sentence = format_input(original_content) @@ -109,7 +106,7 @@ def handle_input(client, original_content, stream, subject): '`readme.md` file?' logging.error(reply_message) - send_message(client, reply_message, stream, subject) + client.send_reply(message, reply_message) def get_api_key():