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.
This commit is contained in:
derAnfaenger 2017-05-24 21:49:44 +02:00 committed by Tim Abbott
parent f83ddd658c
commit c6d294385f
9 changed files with 13 additions and 112 deletions

View file

@ -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(',')

View file

@ -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']

View file

@ -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()

View file

@ -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):

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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():