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

0
bots_api/__init__.py Normal file
View file

177
bots_api/bot_lib.py Normal file
View file

@ -0,0 +1,177 @@
from __future__ import print_function
import logging
import os
import signal
import sys
import time
import re
if False:
from mypy_extensions import NoReturn
from typing import Any, Optional, List, Dict
from types import ModuleType
our_dir = os.path.dirname(os.path.abspath(__file__))
# For dev setups, we can find the API in the repo itself.
if os.path.exists(os.path.join(our_dir, '../zulip')):
sys.path.insert(0, os.path.join(our_dir, '../'))
from zulip import Client
def exit_gracefully(signum, frame):
# type: (int, Optional[Any]) -> None
sys.exit(0)
class RateLimit(object):
def __init__(self, message_limit, interval_limit):
# type: (int, int) -> None
self.message_limit = message_limit
self.interval_limit = interval_limit
self.message_list = [] # type: List[float]
self.error_message = '-----> !*!*!*MESSAGE RATE LIMIT REACHED, EXITING*!*!*! <-----\n'
'Is your bot trapped in an infinite loop by reacting to its own messages?'
def is_legal(self):
# type: () -> bool
self.message_list.append(time.time())
if len(self.message_list) > self.message_limit:
self.message_list.pop(0)
time_diff = self.message_list[-1] - self.message_list[0]
return time_diff >= self.interval_limit
else:
return True
def show_error_and_exit(self):
# type: () -> NoReturn
logging.error(self.error_message)
sys.exit(1)
class BotHandlerApi(object):
def __init__(self, client):
# type: (Client) -> None
# Only expose a subset of our Client's functionality
user_profile = client.get_profile()
self._rate_limit = RateLimit(20, 5)
self._client = client
try:
self.full_name = user_profile['full_name']
self.email = user_profile['email']
except KeyError:
logging.error('Cannot fetch user profile, make sure you have set'
' up the zuliprc file correctly.')
sys.exit(1)
def send_message(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
if self._rate_limit.is_legal():
return self._client.send_message(message)
else:
self._rate_limit.show_error_and_exit()
def update_message(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
if self._rate_limit.is_legal():
return self._client.update_message(message)
else:
self._rate_limit.show_error_and_exit()
def send_reply(self, message, response):
# type: (Dict[str, Any], str) -> Dict[str, Any]
if message['type'] == 'private':
return self.send_message(dict(
type='private',
to=[x['email'] for x in message['display_recipient'] if self.email != x['email']],
content=response,
))
else:
return self.send_message(dict(
type='stream',
to=message['display_recipient'],
subject=message['subject'],
content=response,
))
class StateHandler(object):
def __init__(self):
# type: () -> None
self.state = None # type: Any
def set_state(self, state):
# type: (Any) -> None
self.state = state
def get_state(self):
# type: () -> Any
return self.state
def run_message_handler_for_bot(lib_module, quiet, config_file):
# type: (Any, bool, str) -> Any
#
# lib_module is of type Any, since it can contain any bot's
# handler class. Eventually, we want bot's handler classes to
# inherit from a common prototype specifying the handle_message
# function.
#
# Make sure you set up your ~/.zuliprc
client = Client(config_file=config_file)
restricted_client = BotHandlerApi(client)
message_handler = lib_module.handler_class()
state_handler = StateHandler()
if not quiet:
print(message_handler.usage())
def extract_query_without_mention(message, client):
# type: (Dict[str, Any], BotHandlerApi) -> str
"""
If the bot is the first @mention in the message, then this function returns
the message with the bot's @mention removed. Otherwise, it returns None.
"""
bot_mention = r'^@(\*\*{0}\*\*)'.format(client.full_name)
start_with_mention = re.compile(bot_mention).match(message['content'])
if start_with_mention is None:
return None
query_without_mention = message['content'][len(start_with_mention.group()):]
return query_without_mention.lstrip()
def is_private(message, client):
# type: (Dict[str, Any], BotHandlerApi) -> bool
# bot will not reply if the sender name is the same as the bot name
# to prevent infinite loop
if message['type'] == 'private':
return client.full_name != message['sender_full_name']
return False
def handle_message(message):
# type: (Dict[str, Any]) -> None
logging.info('waiting for next message')
# is_mentioned is true if the bot is mentioned at ANY position (not necessarily
# the first @mention in the message).
is_mentioned = message['is_mentioned']
is_private_message = is_private(message, restricted_client)
# Strip at-mention botname from the message
if is_mentioned:
# message['content'] will be None when the bot's @-mention is not at the beginning.
# In that case, the message shall not be handled.
message['content'] = extract_query_without_mention(message=message, client=restricted_client)
if message['content'] is None:
return
if is_private_message or is_mentioned:
message_handler.handle_message(
message=message,
client=restricted_client,
state_handler=state_handler
)
signal.signal(signal.SIGINT, exit_gracefully)
logging.info('starting message handling...')
client.call_on_each_message(handle_message)

75
bots_api/bots_test_lib.py Normal file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import unittest
import logging
from mock import MagicMock, patch
from run import get_lib_module
from bot_lib import StateHandler
from bots_api import bot_lib
from six.moves import zip
from unittest import TestCase
from typing import List, Dict, Any
from types import ModuleType
current_dir = os.path.dirname(os.path.abspath(__file__))
class BotTestCase(TestCase):
bot_name = '' # type: str
def assert_bot_output(self, request, response):
# type: (Dict[str, Any], str) -> None
bot_module = os.path.normpath(os.path.join(current_dir, "../bots", self.bot_name, self.bot_name + ".py"))
self.bot_test(messages=[request], bot_module=bot_module,
bot_response=[response])
def check_expected_responses(self, expectations, email="foo", recipient="foo", subject="foo", type="all"):
# type: (Dict[str, str], str, str, str, str) -> None
if type not in ["private", "stream", "all"]:
logging.exception("check_expected_response expects type to be 'private', 'stream' or 'all'")
for m, r in expectations.items():
if type != "stream":
self.assert_bot_output(
{'content': m, 'type': "private", 'sender_email': email}, r)
if type != "private":
self.assert_bot_output(
{'content': m, 'type': "stream", 'display_recipient': recipient,
'subject': subject}, r)
def mock_test(self, messages, message_handler, bot_response):
# message_handler is of type Any, since it can contain any bot's
# handler class. Eventually, we want bot's handler classes to
# inherit from a common prototype specifying the handle_message
# function.
# type: (List[Dict[str, Any]], Any, List[str]) -> None
# Mocking BotHandlerApi
with patch('bots_api.bot_lib.BotHandlerApi') as MockClass:
instance = MockClass.return_value
for (message, response) in zip(messages, bot_response):
# Send message to the concerned bot
message_handler.handle_message(message, MockClass(), StateHandler())
# Check if BotHandlerApi is sending a reply message.
# This can later be modified to assert the contents of BotHandlerApi.send_message
instance.send_reply.assert_called_with(message, response)
def bot_to_run(self, bot_module):
# Returning Any, same argument as in mock_test function.
# type: (str) -> Any
lib_module = get_lib_module(bot_module)
message_handler = lib_module.handler_class()
return message_handler
def bot_test(self, messages, bot_module, bot_response):
# type: (List[Dict[str, Any]], str, List[str]) -> None
message_handler = self.bot_to_run(bot_module)
self.mock_test(messages=messages, message_handler=message_handler, bot_response=bot_response)

72
bots_api/run.py Executable file
View file

@ -0,0 +1,72 @@
#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
import importlib
import logging
import optparse
import os
import sys
from types import ModuleType
our_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, our_dir)
from bot_lib import run_message_handler_for_bot
def get_lib_module(bots_fn):
# type: (str) -> ModuleType
bots_fn = os.path.realpath(bots_fn)
if not os.path.dirname(bots_fn).startswith(os.path.normpath(os.path.join(our_dir, "../bots"))):
print('Sorry, we will only import code from api/bots.')
sys.exit(1)
if not bots_fn.endswith('.py'):
print('Please use a .py extension for library files.')
sys.exit(1)
base_bots_fn = os.path.basename(os.path.splitext(bots_fn)[0])
sys.path.append(os.path.dirname(bots_fn))
module_name = base_bots_fn
module = importlib.import_module(module_name)
return module
def run():
# type: () -> None
usage = '''
./run.py <lib file>
Example: ./run.py lib/followup.py
(This program loads bot-related code from the
library code and then runs a message loop,
feeding messages to the library code to handle.)
Please make sure you have a current ~/.zuliprc
file with the credentials you want to use for
this bot.
See lib/readme.md for more context.
'''
parser = optparse.OptionParser(usage=usage)
parser.add_option('--quiet', '-q',
action='store_true',
help='Turn off logging output.')
parser.add_option('--config-file',
action='store',
help='(alternate config file to ~/.zuliprc)')
(options, args) = parser.parse_args()
if len(args) == 0:
print('You must specify a library!')
sys.exit(1)
lib_module = get_lib_module(bots_fn=args[0])
if not options.quiet:
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
run_message_handler_for_bot(
lib_module=lib_module,
config_file=options.config_file,
quiet=options.quiet
)
if __name__ == '__main__':
run()

44
bots_api/test-bots Executable file
View file

@ -0,0 +1,44 @@
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
import argparse
import os
import sys
import unittest
from unittest import TestCase
def dir_join(dir1, dir2):
# type: (str, str) -> str
return os.path.abspath(os.path.join(dir1, dir2))
if __name__ == '__main__':
description = 'Script to run test_<bot>.py files in bots/<bot> directories'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--bot',
nargs=1,
type=str,
action='store',
help='test specified single bot')
args = parser.parse_args()
bots_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = dir_join(bots_dir, '..')
bots_test_dir = dir_join(bots_dir, '../bots')
sys.path.insert(0, root_dir)
sys.path.insert(0, bots_test_dir)
# mypy doesn't recognize the TestLoader attribute, even though the code
# is executable
loader = unittest.TestLoader() # type: ignore
if args.bot is not None:
bots_test_dir = dir_join(bots_test_dir, args.bot[0])
suite = loader.discover(start_dir=bots_test_dir, top_level_dir=root_dir)
runner = unittest.TextTestRunner(verbosity=2)
# same issue as for TestLoader
result = runner.run(suite) # type: ignore
if result.errors or result.failures:
raise Exception('Test failed!')