2017-02-08 16:05:24 -05:00
|
|
|
# See readme.md for instructions on running this code.
|
2017-01-04 11:55:52 -05:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import ssl
|
|
|
|
import sys
|
|
|
|
try:
|
2017-02-02 21:56:47 -05:00
|
|
|
import requests
|
|
|
|
except ImportError as e:
|
|
|
|
logging.error("Dependency missing!!\n{}".format(e))
|
2017-01-04 11:55:52 -05:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
HELP_MESSAGE = '''
|
|
|
|
This bot allows users to translate a sentence into
|
|
|
|
'Yoda speak'.
|
2017-02-16 16:56:32 -05:00
|
|
|
Users should preface messages with '@mention-bot'.
|
2017-01-04 11:55:52 -05:00
|
|
|
|
|
|
|
Before running this, make sure to get a Mashape Api token.
|
2017-02-08 16:05:24 -05:00
|
|
|
Instructions are in the 'readme.md' file.
|
2017-03-05 13:42:39 -05:00
|
|
|
Store it in the 'yoda.config' file.
|
|
|
|
The 'yoda.config' file should be located at '~/yoda.config'.
|
2017-01-04 11:55:52 -05:00
|
|
|
Example input:
|
2017-02-16 16:56:32 -05:00
|
|
|
@mention-bot You will learn how to speak like me someday.
|
2017-01-04 11:55:52 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
class ApiKeyError(Exception):
|
|
|
|
'''raise this when there is an error with the Mashape Api Key'''
|
|
|
|
|
|
|
|
|
|
|
|
class YodaSpeakHandler(object):
|
|
|
|
'''
|
|
|
|
This bot will allow users to translate a sentence into 'Yoda speak'.
|
2017-02-16 16:56:32 -05:00
|
|
|
It looks for messages starting with '@mention-bot'.
|
2017-01-04 11:55:52 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
def usage(self):
|
|
|
|
return '''
|
|
|
|
This bot will allow users to translate a sentence into
|
|
|
|
'Yoda speak'.
|
2017-02-16 16:56:32 -05:00
|
|
|
Users should preface messages with '@mention-bot'.
|
2017-01-04 11:55:52 -05:00
|
|
|
|
|
|
|
Before running this, make sure to get a Mashape Api token.
|
2017-02-08 16:05:24 -05:00
|
|
|
Instructions are in the 'readme.md' file.
|
2017-03-05 13:42:39 -05:00
|
|
|
Store it in the 'yoda.config' file.
|
|
|
|
The 'yoda.config' file should be located at '~/yoda.config'.
|
2017-01-04 11:55:52 -05:00
|
|
|
Example input:
|
2017-02-16 16:56:32 -05:00
|
|
|
@mention-bot You will learn how to speak like me someday.
|
2017-01-04 11:55:52 -05:00
|
|
|
'''
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
def handle_message(self, message, bot_handler, state_handler):
|
|
|
|
handle_input(message, bot_handler)
|
2017-01-04 11:55:52 -05:00
|
|
|
|
|
|
|
handler_class = YodaSpeakHandler
|
|
|
|
|
|
|
|
|
|
|
|
def send_to_yoda_api(sentence, api_key):
|
|
|
|
# function for sending sentence to api
|
|
|
|
|
2017-02-02 21:56:47 -05:00
|
|
|
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
|
|
|
|
headers={
|
|
|
|
"X-Mashape-Key": api_key,
|
|
|
|
"Accept": "text/plain"
|
|
|
|
}
|
|
|
|
)
|
2017-01-04 11:55:52 -05:00
|
|
|
|
2017-02-02 21:56:47 -05:00
|
|
|
if response.status_code == 200:
|
|
|
|
return response.text
|
|
|
|
if response.status_code == 403:
|
2017-01-04 11:55:52 -05:00
|
|
|
raise ApiKeyError
|
|
|
|
else:
|
2017-02-02 21:56:47 -05:00
|
|
|
error_message = response.text['message']
|
2017-01-04 11:55:52 -05:00
|
|
|
logging.error(error_message)
|
2017-02-02 21:56:47 -05:00
|
|
|
error_code = response.status_code
|
2017-01-04 11:55:52 -05:00
|
|
|
error_message = error_message + 'Error code: ' + error_code +\
|
2017-02-08 16:05:24 -05:00
|
|
|
' Did you follow the instructions in the `readme.md` file?'
|
2017-01-04 11:55:52 -05:00
|
|
|
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
|
2017-02-16 16:56:32 -05:00
|
|
|
message_content = original_content.strip()
|
2017-01-04 11:55:52 -05:00
|
|
|
# replaces all spaces with '+' to be in the format the api requires
|
|
|
|
sentence = message_content.replace(' ', '+')
|
|
|
|
return sentence
|
|
|
|
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
def handle_input(message, bot_handler):
|
2017-01-04 11:55:52 -05:00
|
|
|
|
2017-05-24 15:49:44 -04:00
|
|
|
original_content = message['content']
|
2017-01-04 11:55:52 -05:00
|
|
|
if is_help(original_content):
|
2017-06-11 09:03:39 -04:00
|
|
|
bot_handler.send_reply(message, HELP_MESSAGE)
|
2017-01-04 11:55:52 -05:00
|
|
|
|
|
|
|
else:
|
|
|
|
sentence = format_input(original_content)
|
|
|
|
try:
|
|
|
|
reply_message = send_to_yoda_api(sentence, get_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 ' \
|
2017-02-08 16:05:24 -05:00
|
|
|
'`readme.md` file?'
|
2017-01-04 11:55:52 -05:00
|
|
|
logging.error(reply_message)
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
bot_handler.send_reply(message, reply_message)
|
2017-01-04 11:55:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_api_key():
|
|
|
|
# function for getting Mashape api key
|
|
|
|
home = os.path.expanduser('~')
|
2017-03-05 13:42:39 -05:00
|
|
|
with open(home + '/yoda.config') as api_key_file:
|
2017-01-04 11:55:52 -05:00
|
|
|
api_key = api_key_file.read().strip()
|
|
|
|
return api_key
|
|
|
|
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
def send_message(bot_handler, message, stream, subject):
|
2017-01-04 11:55:52 -05:00
|
|
|
# function for sending a message
|
2017-06-11 09:03:39 -04:00
|
|
|
bot_handler.send_message(dict(
|
2017-01-04 11:55:52 -05:00
|
|
|
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
|
2017-02-16 16:56:32 -05:00
|
|
|
message_content = original_content.strip()
|
2017-01-04 11:55:52 -05:00
|
|
|
if message_content == 'help':
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|