2016-12-21 17:16:59 +01:00
|
|
|
# See readme.md for instructions on running this code.
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import html2text
|
|
|
|
|
|
|
|
class DefineHandler(object):
|
|
|
|
'''
|
|
|
|
This plugin define a word that the user inputs. It
|
2017-02-16 22:56:32 +01:00
|
|
|
looks for messages starting with '@mention-bot'.
|
2016-12-21 17:16:59 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
DEFINITION_API_URL = 'https://owlbot.info/api/v1/dictionary/{}?format=json'
|
|
|
|
REQUEST_ERROR_MESSAGE = 'Definition not available.'
|
|
|
|
EMPTY_WORD_REQUEST_ERROR_MESSAGE = 'Please enter a word to define.'
|
|
|
|
PHRASE_ERROR_MESSAGE = 'Definitions for phrases are not available.'
|
|
|
|
|
2017-03-05 19:58:18 +01:00
|
|
|
def usage(self):
|
2016-12-21 17:16:59 +01:00
|
|
|
return '''
|
|
|
|
This plugin will allow users to define a word. Users should preface
|
2017-02-16 22:56:32 +01:00
|
|
|
messages with @mention-bot.
|
2016-12-21 17:16:59 +01:00
|
|
|
'''
|
|
|
|
|
2017-03-05 19:58:18 +01:00
|
|
|
def handle_message(self, message, client, state_handler):
|
|
|
|
original_content = message['content'].strip()
|
|
|
|
bot_response = self.get_bot_define_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,
|
|
|
|
))
|
2016-12-21 17:16:59 +01:00
|
|
|
|
2017-03-05 19:58:18 +01:00
|
|
|
def get_bot_define_response(self, original_content):
|
|
|
|
split_content = original_content.split(' ')
|
2016-12-21 17:16:59 +01:00
|
|
|
# If there are more than one word (a phrase)
|
2017-02-16 22:56:32 +01:00
|
|
|
if len(split_content) > 1:
|
2016-12-21 17:16:59 +01:00
|
|
|
return DefineHandler.PHRASE_ERROR_MESSAGE
|
|
|
|
|
2017-02-16 22:56:32 +01:00
|
|
|
to_define = split_content[0].strip()
|
2016-12-21 17:16:59 +01:00
|
|
|
to_define_lower = to_define.lower()
|
|
|
|
|
|
|
|
# No word was entered.
|
|
|
|
if not to_define_lower:
|
2017-03-05 19:58:18 +01:00
|
|
|
return self.EMPTY_WORD_REQUEST_ERROR_MESSAGE
|
2016-12-21 17:16:59 +01:00
|
|
|
else:
|
|
|
|
response = '**{}**:\n'.format(to_define)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Use OwlBot API to fetch definition.
|
2017-03-05 19:58:18 +01:00
|
|
|
api_result = requests.get(self.DEFINITION_API_URL.format(to_define_lower))
|
2016-12-21 17:16:59 +01:00
|
|
|
# Convert API result from string to JSON format.
|
|
|
|
definitions = api_result.json()
|
|
|
|
|
|
|
|
# Could not fetch definitions for the given word.
|
|
|
|
if not definitions:
|
2017-03-05 19:58:18 +01:00
|
|
|
response += self.REQUEST_ERROR_MESSAGE
|
2016-12-21 17:16:59 +01:00
|
|
|
else: # Definitions available.
|
|
|
|
# Show definitions line by line.
|
|
|
|
for d in definitions:
|
|
|
|
example = d['example'] if d['example'] else '*No example available.*'
|
|
|
|
response += '\n' + '* (**{}**) {}\n {}'.format(d['type'], d['defenition'], html2text.html2text(example))
|
|
|
|
|
|
|
|
except Exception as e:
|
2017-03-05 19:58:18 +01:00
|
|
|
response += self.REQUEST_ERROR_MESSAGE
|
2016-12-21 17:16:59 +01:00
|
|
|
logging.exception(e)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
handler_class = DefineHandler
|