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.
MODIFY
contrib_bots/bots/commute/commute.py
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(',')

MODIFY
contrib_bots/bots/converter/converter.py
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']

MODIFY
contrib_bots/bots/foursquare/foursquare.py
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()

MODIFY
contrib_bots/bots/giphy/giphy.py
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):

MODIFY
contrib_bots/bots/googlesearch/googlesearch.py
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

MODIFY
contrib_bots/bots/helloworld/helloworld.py
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

MODIFY
contrib_bots/bots/john/john.py
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

MODIFY
contrib_bots/bots/thesaurus/thesaurus.py
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

MODIFY
contrib_bots/bots/yoda/yoda.py
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():

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.