bots: Move contrib_bots to api/bots*.
This will make it convenient to include these bots in Zulip API releases on pypi. Fix #5009.
This commit is contained in:
parent
7531c4fb26
commit
894adb1e43
110 changed files with 36 additions and 27 deletions
0
bots/define/__init__.py
Normal file
0
bots/define/__init__.py
Normal file
BIN
bots/define/assets/correct_word.png
Normal file
BIN
bots/define/assets/correct_word.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 73 KiB |
BIN
bots/define/assets/wrong_word.png
Normal file
BIN
bots/define/assets/wrong_word.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
66
bots/define/define.py
Normal file
66
bots/define/define.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
# 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
|
||||
looks for messages starting with '@mention-bot'.
|
||||
'''
|
||||
|
||||
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.'
|
||||
|
||||
def usage(self):
|
||||
return '''
|
||||
This plugin will allow users to define a word. Users should preface
|
||||
messages with @mention-bot.
|
||||
'''
|
||||
|
||||
def handle_message(self, message, client, state_handler):
|
||||
original_content = message['content'].strip()
|
||||
bot_response = self.get_bot_define_response(original_content)
|
||||
|
||||
client.send_reply(message, bot_response)
|
||||
|
||||
def get_bot_define_response(self, original_content):
|
||||
split_content = original_content.split(' ')
|
||||
# If there are more than one word (a phrase)
|
||||
if len(split_content) > 1:
|
||||
return DefineHandler.PHRASE_ERROR_MESSAGE
|
||||
|
||||
to_define = split_content[0].strip()
|
||||
to_define_lower = to_define.lower()
|
||||
|
||||
# No word was entered.
|
||||
if not to_define_lower:
|
||||
return self.EMPTY_WORD_REQUEST_ERROR_MESSAGE
|
||||
else:
|
||||
response = '**{}**:\n'.format(to_define)
|
||||
|
||||
try:
|
||||
# Use OwlBot API to fetch definition.
|
||||
api_result = requests.get(self.DEFINITION_API_URL.format(to_define_lower))
|
||||
# Convert API result from string to JSON format.
|
||||
definitions = api_result.json()
|
||||
|
||||
# Could not fetch definitions for the given word.
|
||||
if not definitions:
|
||||
response += self.REQUEST_ERROR_MESSAGE
|
||||
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:
|
||||
response += self.REQUEST_ERROR_MESSAGE
|
||||
logging.exception(e)
|
||||
|
||||
return response
|
||||
|
||||
handler_class = DefineHandler
|
21
bots/define/readme.md
Normal file
21
bots/define/readme.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
# DefineBot
|
||||
|
||||
* This is a bot that defines a word that the user inputs. Whenever the user
|
||||
inputs a message starting with '@define', the bot defines the word
|
||||
that follows.
|
||||
|
||||
* The definitions are brought to the website using an API. The bot posts the
|
||||
definition of the word to the stream from which the user inputs the message.
|
||||
If the user inputs a word that does not exist or a word that is incorrect or
|
||||
is not in the dictionary, the definition is not displayed.
|
||||
|
||||
* For example, if the user says "@define crash", all the meanings of crash
|
||||
appear, each in a separate line.
|
||||
|
||||

|
||||
|
||||
* If the user enters a wrong word, like "@define cresh" or "@define crish",
|
||||
then an error message saying no definition is available is displayed.
|
||||
|
||||

|
||||
|
28
bots/define/test_define.py
Normal file
28
bots/define/test_define.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
our_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, os.path.normpath(os.path.join(our_dir)))
|
||||
# For dev setups, we can find the API in the repo itself.
|
||||
if os.path.exists(os.path.join(our_dir, '..')):
|
||||
sys.path.insert(0, '..')
|
||||
from bots_test_lib import BotTestCase
|
||||
|
||||
class TestDefineBot(BotTestCase):
|
||||
bot_name = "define"
|
||||
|
||||
def test_bot(self):
|
||||
expected = {
|
||||
"": 'Please enter a word to define.',
|
||||
"foo": "**foo**:\nDefinition not available.",
|
||||
"cat": ("**cat**:\n\n* (**noun**) a small domesticated carnivorous mammal "
|
||||
"with soft fur, a short snout, and retractile claws. It is widely "
|
||||
"kept as a pet or for catching mice, and many breeds have been "
|
||||
"developed.\n their pet cat\n\n"),
|
||||
}
|
||||
self.check_expected_responses(expected)
|
Loading…
Add table
Add a link
Reference in a new issue