followup bot: Fix help response and configure streams.

From @showell:

We had a PR here with lots going on, and the commits weren't
very well organized, and then there were some tricky merge
conflicts from another PR.  So I just squashed them all into
one commit.

What this does:
    * allow you to configure your followup stream
    * provide help in followup stream
    * add more testing to followup stream
    * add get_response() helper for tests

Fixes #173
Fixes #174
This commit is contained in:
Nikhil Mishra 2017-12-09 04:04:21 +05:30 committed by Steve Howell
parent 1cdb0bffe6
commit 81b207795f
4 changed files with 72 additions and 21 deletions

View file

@ -0,0 +1,2 @@
[followup]
stream = 'followup'

View file

@ -23,15 +23,21 @@ class FollowupHandler(object):
called "followup" that your API user can send to. called "followup" that your API user can send to.
''' '''
def initialize(self, bot_handler: Any) -> None:
self.config_info = bot_handler.get_config_info('followup', optional=False)
self.stream = self.config_info.get("stream", 'followup')
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None: def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
if message['content'] == '': if message['content'] == '':
bot_response = "Please specify the message you want to send to followup stream after @mention-bot" bot_response = "Please specify the message you want to send to followup stream after @mention-bot"
bot_handler.send_reply(message, bot_response) bot_handler.send_reply(message, bot_response)
elif message['content'] == 'help':
bot_handler.send_reply(message, self.usage())
else: else:
bot_response = self.get_bot_followup_response(message) bot_response = self.get_bot_followup_response(message)
bot_handler.send_message(dict( bot_handler.send_message(dict(
type='stream', type='stream',
to='followup', to=self.stream,
subject=message['sender_email'], subject=message['sender_email'],
content=bot_response, content=bot_response,
)) ))

View file

@ -3,29 +3,60 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
from zulip_bots.test_lib import BotTestCase from zulip_bots.test_lib import (
StubBotHandler,
StubBotTestCase,
get_bot_message_handler,
)
from typing import Any from typing import Any
class TestFollowUpBot(BotTestCase):
class TestFollowUpBot(StubBotTestCase):
bot_name = "followup" bot_name = "followup"
def test_bot(self) -> None: def test_followup_stream(self) -> None:
expected_send_reply = [ message = dict(
("", 'Please specify the message you want to send to followup stream after @mention-bot') content='feed the cat',
] type='stream',
self.check_expected_responses(expected_send_reply, expected_method='send_reply') sender_email='foo@example.com',
)
expected_send_message = [ with self.mock_config_info({'stream': 'followup'}):
("foo", response = self.get_response(message)
{'type': 'stream',
'to': 'followup', self.assertEqual(response['content'], 'from foo@example.com: feed the cat')
'subject': 'foo_sender@zulip.com', self.assertEqual(response['to'], 'followup')
'content': 'from foo_sender@zulip.com: foo'}),
("I have completed my task", def test_different_stream(self) -> None:
{'type': 'stream', message = dict(
'to': 'followup', content='feed the cat',
'subject': 'foo_sender@zulip.com', type='stream',
'content': 'from foo_sender@zulip.com: I have completed my task'}), sender_email='foo@example.com',
] )
self.check_expected_responses(expected_send_message, expected_method='send_message')
with self.mock_config_info({'stream': 'issue'}):
response = self.get_response(message)
self.assertEqual(response['content'], 'from foo@example.com: feed the cat')
self.assertEqual(response['to'], 'issue')
def test_bot_responds_to_empty_message(self) -> None:
bot_response = 'Please specify the message you want to send to followup stream after @mention-bot'
with self.mock_config_info({'stream': 'followup'}):
self.verify_reply('', bot_response)
def test_help_text(self) -> None:
request = 'help'
bot_response = '''
This plugin will allow users to flag messages
as being follow-up items. Users should preface
messages with "@mention-bot".
Before running this, make sure to create a stream
called "followup" that your API user can send to.
'''
with self.mock_config_info({'stream': 'followup'}):
self.verify_reply(request, bot_response)

View file

@ -100,6 +100,18 @@ class StubBotTestCase(TestCase):
bot_name = '' bot_name = ''
def get_response(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
bot = get_bot_message_handler(self.bot_name)
bot_handler = StubBotHandler()
if hasattr(bot, 'initialize'):
bot.initialize(bot_handler)
bot_handler.reset_transcript()
bot.handle_message(message, bot_handler)
return bot_handler.unique_response()
def verify_reply(self, request, response): def verify_reply(self, request, response):
# type: (str, str) -> None # type: (str, str) -> None