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:
parent
f83ddd658c
commit
c6d294385f
|
@ -90,19 +90,7 @@ class CommuteHandler(object):
|
||||||
|
|
||||||
# determines if bot will respond as a private message/ stream message
|
# determines if bot will respond as a private message/ stream message
|
||||||
def send_info(self, message, letter, client):
|
def send_info(self, message, letter, client):
|
||||||
if message['type'] == 'private':
|
client.send_reply(message, letter)
|
||||||
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,
|
|
||||||
))
|
|
||||||
|
|
||||||
def calculate_seconds(self, time_str):
|
def calculate_seconds(self, time_str):
|
||||||
times = time_str.split(',')
|
times = time_str.split(',')
|
||||||
|
|
|
@ -49,19 +49,7 @@ class ConverterHandler(object):
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
bot_response = get_bot_converter_response(message, client)
|
bot_response = get_bot_converter_response(message, client)
|
||||||
if message['type'] == 'private':
|
client.send_reply(message, bot_response)
|
||||||
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,
|
|
||||||
))
|
|
||||||
|
|
||||||
def get_bot_converter_response(message, client):
|
def get_bot_converter_response(message, client):
|
||||||
content = message['content']
|
content = message['content']
|
||||||
|
|
|
@ -60,19 +60,7 @@ Example Inputs:
|
||||||
return '\n'.join(format_venue(venue) for venue in venues)
|
return '\n'.join(format_venue(venue) for venue in venues)
|
||||||
|
|
||||||
def send_info(self, message, letter, client):
|
def send_info(self, message, letter, client):
|
||||||
if message['type'] == 'private':
|
client.send_reply(message, letter)
|
||||||
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,
|
|
||||||
))
|
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
words = message['content'].split()
|
words = message['content'].split()
|
||||||
|
|
|
@ -35,20 +35,7 @@ class GiphyHandler(object):
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
bot_response = get_bot_giphy_response(message, client)
|
bot_response = get_bot_giphy_response(message, client)
|
||||||
|
client.send_reply(message, bot_response)
|
||||||
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,
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
class GiphyNoResultException(Exception):
|
class GiphyNoResultException(Exception):
|
||||||
|
|
|
@ -74,22 +74,8 @@ class GoogleSearchHandler(object):
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
original_content = message['content']
|
original_content = message['content']
|
||||||
original_sender = message['sender_email']
|
|
||||||
result = get_google_result(original_content)
|
result = get_google_result(original_content)
|
||||||
|
client.send_reply(message, result)
|
||||||
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,
|
|
||||||
))
|
|
||||||
|
|
||||||
handler_class = GoogleSearchHandler
|
handler_class = GoogleSearchHandler
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,6 @@ class HelloWorldHandler(object):
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
content = 'beep boop'
|
content = 'beep boop'
|
||||||
|
client.send_reply(message, content)
|
||||||
client.send_message(dict(
|
|
||||||
type='stream',
|
|
||||||
to=message['display_recipient'],
|
|
||||||
subject=message['subject'],
|
|
||||||
content=content,
|
|
||||||
))
|
|
||||||
|
|
||||||
handler_class = HelloWorldHandler
|
handler_class = HelloWorldHandler
|
||||||
|
|
|
@ -118,19 +118,6 @@ class JohnHandler(object):
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
original_content = message['content']
|
original_content = message['content']
|
||||||
bot_response = str(bota.get_response(original_content))
|
bot_response = str(bota.get_response(original_content))
|
||||||
|
client.send_reply(message, bot_response)
|
||||||
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,
|
|
||||||
))
|
|
||||||
|
|
||||||
handler_class = JohnHandler
|
handler_class = JohnHandler
|
||||||
|
|
|
@ -62,21 +62,7 @@ class ThesaurusHandler(object):
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
original_content = message['content'].strip()
|
original_content = message['content'].strip()
|
||||||
original_sender = message['sender_email']
|
|
||||||
new_content = get_thesaurus_result(original_content)
|
new_content = get_thesaurus_result(original_content)
|
||||||
|
client.send_reply(message, new_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,
|
|
||||||
))
|
|
||||||
|
|
||||||
handler_class = ThesaurusHandler
|
handler_class = ThesaurusHandler
|
||||||
|
|
|
@ -50,11 +50,7 @@ class YodaSpeakHandler(object):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def handle_message(self, message, client, state_handler):
|
def handle_message(self, message, client, state_handler):
|
||||||
original_content = message['content']
|
handle_input(message, client)
|
||||||
stream = message['display_recipient']
|
|
||||||
subject = message['subject']
|
|
||||||
|
|
||||||
handle_input(client, original_content, stream, subject)
|
|
||||||
|
|
||||||
handler_class = YodaSpeakHandler
|
handler_class = YodaSpeakHandler
|
||||||
|
|
||||||
|
@ -90,10 +86,11 @@ def format_input(original_content):
|
||||||
return sentence
|
return sentence
|
||||||
|
|
||||||
|
|
||||||
def handle_input(client, original_content, stream, subject):
|
def handle_input(message, client):
|
||||||
|
|
||||||
|
original_content = message['content']
|
||||||
if is_help(original_content):
|
if is_help(original_content):
|
||||||
send_message(client, HELP_MESSAGE, stream, subject)
|
client.send_reply(message, HELP_MESSAGE)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
sentence = format_input(original_content)
|
sentence = format_input(original_content)
|
||||||
|
@ -109,7 +106,7 @@ def handle_input(client, original_content, stream, subject):
|
||||||
'`readme.md` file?'
|
'`readme.md` file?'
|
||||||
logging.error(reply_message)
|
logging.error(reply_message)
|
||||||
|
|
||||||
send_message(client, reply_message, stream, subject)
|
client.send_reply(message, reply_message)
|
||||||
|
|
||||||
|
|
||||||
def get_api_key():
|
def get_api_key():
|
||||||
|
|
Loading…
Reference in a new issue