bots: Minor changes to yoda.py to make it run smoothly.
Modifications done so that the bot runs using 'zulip-run-bot' command.
This commit is contained in:
parent
09060afcab
commit
a58fae5cab
|
@ -33,7 +33,6 @@ class YodaSpeakHandler(object):
|
||||||
This bot will allow users to translate a sentence into 'Yoda speak'.
|
This bot will allow users to translate a sentence into 'Yoda speak'.
|
||||||
It looks for messages starting with '@mention-bot'.
|
It looks for messages starting with '@mention-bot'.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def initialize(self, bot_handler):
|
def initialize(self, bot_handler):
|
||||||
self.api_key = bot_handler.get_config_info('yoda')['api_key']
|
self.api_key = bot_handler.get_config_info('yoda')['api_key']
|
||||||
|
|
||||||
|
@ -52,78 +51,77 @@ class YodaSpeakHandler(object):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler, state_handler):
|
||||||
handle_input(message, bot_handler)
|
self.handle_input(message, bot_handler)
|
||||||
|
|
||||||
|
def send_to_yoda_api(self, sentence):
|
||||||
|
# function for sending sentence to api
|
||||||
|
|
||||||
|
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
|
||||||
|
headers={
|
||||||
|
"X-Mashape-Key": self.api_key,
|
||||||
|
"Accept": "text/plain"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.text
|
||||||
|
if response.status_code == 403:
|
||||||
|
raise ApiKeyError
|
||||||
|
else:
|
||||||
|
error_message = response.text['message']
|
||||||
|
logging.error(error_message)
|
||||||
|
error_code = response.status_code
|
||||||
|
error_message = error_message + 'Error code: ' + error_code +\
|
||||||
|
' Did you follow the instructions in the `readme.md` file?'
|
||||||
|
return error_message
|
||||||
|
|
||||||
|
|
||||||
|
def format_input(self, original_content):
|
||||||
|
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
||||||
|
message_content = original_content.strip()
|
||||||
|
# replaces all spaces with '+' to be in the format the api requires
|
||||||
|
sentence = message_content.replace(' ', '+')
|
||||||
|
return sentence
|
||||||
|
|
||||||
|
|
||||||
|
def handle_input(self, message, bot_handler):
|
||||||
|
|
||||||
|
original_content = message['content']
|
||||||
|
if self.is_help(original_content):
|
||||||
|
bot_handler.send_reply(message, HELP_MESSAGE)
|
||||||
|
|
||||||
|
else:
|
||||||
|
sentence = self.format_input(original_content)
|
||||||
|
try:
|
||||||
|
reply_message = self.send_to_yoda_api(sentence)
|
||||||
|
|
||||||
|
except ssl.SSLError or TypeError:
|
||||||
|
reply_message = 'The service is temporarily unavailable, please try again.'
|
||||||
|
logging.error(reply_message)
|
||||||
|
|
||||||
|
except ApiKeyError:
|
||||||
|
reply_message = 'Invalid Api Key. Did you follow the instructions in the ' \
|
||||||
|
'`readme.md` file?'
|
||||||
|
logging.error(reply_message)
|
||||||
|
|
||||||
|
bot_handler.send_reply(message, reply_message)
|
||||||
|
|
||||||
|
def send_message(self, bot_handler, message, stream, subject):
|
||||||
|
# function for sending a message
|
||||||
|
bot_handler.send_message(dict(
|
||||||
|
type='stream',
|
||||||
|
to=stream,
|
||||||
|
subject=subject,
|
||||||
|
content=message
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
|
def is_help(self, original_content):
|
||||||
|
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
||||||
|
message_content = original_content.strip()
|
||||||
|
if message_content == 'help':
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
handler_class = YodaSpeakHandler
|
handler_class = YodaSpeakHandler
|
||||||
|
|
||||||
|
|
||||||
def send_to_yoda_api(sentence, api_key):
|
|
||||||
# function for sending sentence to api
|
|
||||||
|
|
||||||
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
|
|
||||||
headers={
|
|
||||||
"X-Mashape-Key": api_key,
|
|
||||||
"Accept": "text/plain"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
|
||||||
return response.text
|
|
||||||
if response.status_code == 403:
|
|
||||||
raise ApiKeyError
|
|
||||||
else:
|
|
||||||
error_message = response.text['message']
|
|
||||||
logging.error(error_message)
|
|
||||||
error_code = response.status_code
|
|
||||||
error_message = error_message + 'Error code: ' + error_code +\
|
|
||||||
' Did you follow the instructions in the `readme.md` file?'
|
|
||||||
return error_message
|
|
||||||
|
|
||||||
|
|
||||||
def format_input(original_content):
|
|
||||||
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
|
||||||
message_content = original_content.strip()
|
|
||||||
# replaces all spaces with '+' to be in the format the api requires
|
|
||||||
sentence = message_content.replace(' ', '+')
|
|
||||||
return sentence
|
|
||||||
|
|
||||||
|
|
||||||
def handle_input(message, bot_handler):
|
|
||||||
|
|
||||||
original_content = message['content']
|
|
||||||
if is_help(original_content):
|
|
||||||
bot_handler.send_reply(message, HELP_MESSAGE)
|
|
||||||
|
|
||||||
else:
|
|
||||||
sentence = format_input(original_content)
|
|
||||||
try:
|
|
||||||
reply_message = send_to_yoda_api(sentence, handler_class.api_key)
|
|
||||||
|
|
||||||
except ssl.SSLError or TypeError:
|
|
||||||
reply_message = 'The service is temporarily unavailable, please try again.'
|
|
||||||
logging.error(reply_message)
|
|
||||||
|
|
||||||
except ApiKeyError:
|
|
||||||
reply_message = 'Invalid Api Key. Did you follow the instructions in the ' \
|
|
||||||
'`readme.md` file?'
|
|
||||||
logging.error(reply_message)
|
|
||||||
|
|
||||||
bot_handler.send_reply(message, reply_message)
|
|
||||||
|
|
||||||
def send_message(bot_handler, message, stream, subject):
|
|
||||||
# function for sending a message
|
|
||||||
bot_handler.send_message(dict(
|
|
||||||
type='stream',
|
|
||||||
to=stream,
|
|
||||||
subject=subject,
|
|
||||||
content=message
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
def is_help(original_content):
|
|
||||||
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
|
||||||
message_content = original_content.strip()
|
|
||||||
if message_content == 'help':
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
Loading…
Reference in a new issue