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:
Rohitt Vashishtha 2017-05-30 11:40:19 +05:30 committed by Tim Abbott
parent 7531c4fb26
commit 894adb1e43
110 changed files with 36 additions and 27 deletions

View file

View file

@ -0,0 +1,48 @@
#!/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 TestThesaurusBot(BotTestCase):
bot_name = "thesaurus"
def test_bot(self):
self.assert_bot_output(
{'content': "synonym good", 'type': "private", 'sender_email': "foo"},
"great, satisfying, exceptional, positive, acceptable"
)
self.assert_bot_output(
{'content': "synonym nice", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
"cordial, kind, good, okay, fair"
)
self.assert_bot_output(
{'content': "synonym foo", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
"bar, thud, X, baz, corge"
)
self.assert_bot_output(
{'content': "antonym dirty", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
"ordered, sterile, spotless, moral, clean"
)
self.assert_bot_output(
{'content': "antonym bar", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
"loss, whole, advantage, aid, failure"
)
self.assert_bot_output(
{'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
("To use this bot, start messages with either "
"@mention-bot synonym (to get the synonyms of a given word) "
"or @mention-bot antonym (to get the antonyms of a given word). "
"Phrases are not accepted so only use single words "
"to search. For example you could search '@mention-bot synonym hello' "
"or '@mention-bot antonym goodbye'."),
)

View file

@ -0,0 +1,71 @@
# See zulip/api/bots/readme.md for instructions on running this code.
from __future__ import print_function
import sys
import logging
try:
from PyDictionary import PyDictionary as Dictionary
except ImportError:
logging.error("Dependency Missing!")
sys.exit(0)
#Uses Python's Dictionary module
# pip install PyDictionary
def get_clean_response(m, method):
try:
response = method(m)
except Exception as e:
logging.exception(e)
return e
if isinstance(response, str):
return response
elif isinstance(response, list):
return ', '.join(response)
else:
return "Sorry, no result found! Please check the word."
def get_thesaurus_result(original_content):
help_message = ("To use this bot, start messages with either "
"@mention-bot synonym (to get the synonyms of a given word) "
"or @mention-bot antonym (to get the antonyms of a given word). "
"Phrases are not accepted so only use single words "
"to search. For example you could search '@mention-bot synonym hello' "
"or '@mention-bot antonym goodbye'.")
query = original_content.strip().split(' ', 1)
if len(query) < 2:
return help_message
else:
search_keyword = query[1]
if original_content.startswith('synonym'):
result = get_clean_response(search_keyword, method = Dictionary.synonym)
elif original_content.startswith('antonym'):
result = get_clean_response(search_keyword, method = Dictionary.antonym)
else:
result = help_message
return result
class ThesaurusHandler(object):
'''
This plugin allows users to enter a word in zulip
and get synonyms, and antonyms, for that word sent
back to the context (stream or private) in which
it was sent. It looks for messages starting with
'@mention-bot synonym' or '@mention-bot @antonym'.
'''
def usage(self):
return '''
This plugin will allow users to get both synonyms
and antonyms for a given word from zulip. To use this
plugin, users need to install the PyDictionary module
using 'pip install PyDictionary'.Use '@mention-bot synonym help' or
'@mention-bot antonym help' for more usage information. Users should
preface messages with @mention-bot synonym or @mention-bot antonym.
'''
def handle_message(self, message, client, state_handler):
original_content = message['content'].strip()
new_content = get_thesaurus_result(original_content)
client.send_reply(message, new_content)
handler_class = ThesaurusHandler