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:
		
							parent
							
								
									1cdb0bffe6
								
							
						
					
					
						commit
						81b207795f
					
				
					 4 changed files with 72 additions and 21 deletions
				
			
		
							
								
								
									
										2
									
								
								zulip_bots/zulip_bots/bots/followup/followup.config
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								zulip_bots/zulip_bots/bots/followup/followup.config
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,2 @@ | |||
| [followup] | ||||
| stream = 'followup' | ||||
|  | @ -23,15 +23,21 @@ class FollowupHandler(object): | |||
|             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: | ||||
|         if message['content'] == '': | ||||
|             bot_response = "Please specify the message you want to send to followup stream after @mention-bot" | ||||
|             bot_handler.send_reply(message, bot_response) | ||||
|         elif message['content'] == 'help': | ||||
|             bot_handler.send_reply(message, self.usage()) | ||||
|         else: | ||||
|             bot_response = self.get_bot_followup_response(message) | ||||
|             bot_handler.send_message(dict( | ||||
|                 type='stream', | ||||
|                 to='followup', | ||||
|                 to=self.stream, | ||||
|                 subject=message['sender_email'], | ||||
|                 content=bot_response, | ||||
|             )) | ||||
|  |  | |||
|  | @ -3,29 +3,60 @@ | |||
| from __future__ import absolute_import | ||||
| 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 | ||||
| 
 | ||||
| class TestFollowUpBot(BotTestCase): | ||||
| 
 | ||||
| class TestFollowUpBot(StubBotTestCase): | ||||
|     bot_name = "followup" | ||||
| 
 | ||||
|     def test_bot(self) -> None: | ||||
|         expected_send_reply = [ | ||||
|             ("", 'Please specify the message you want to send to followup stream after @mention-bot') | ||||
|         ] | ||||
|         self.check_expected_responses(expected_send_reply, expected_method='send_reply') | ||||
|     def test_followup_stream(self) -> None: | ||||
|         message = dict( | ||||
|             content='feed the cat', | ||||
|             type='stream', | ||||
|             sender_email='foo@example.com', | ||||
|         ) | ||||
| 
 | ||||
|         expected_send_message = [ | ||||
|             ("foo", | ||||
|              {'type': 'stream', | ||||
|               'to': 'followup', | ||||
|               'subject': 'foo_sender@zulip.com', | ||||
|               'content': 'from foo_sender@zulip.com: foo'}), | ||||
|             ("I have completed my task", | ||||
|              {'type': 'stream', | ||||
|               'to': 'followup', | ||||
|               'subject': 'foo_sender@zulip.com', | ||||
|               'content': 'from foo_sender@zulip.com: I have completed my task'}), | ||||
|         ] | ||||
|         self.check_expected_responses(expected_send_message, expected_method='send_message') | ||||
|         with self.mock_config_info({'stream': 'followup'}): | ||||
|             response = self.get_response(message) | ||||
| 
 | ||||
|         self.assertEqual(response['content'], 'from foo@example.com: feed the cat') | ||||
|         self.assertEqual(response['to'], 'followup') | ||||
| 
 | ||||
|     def test_different_stream(self) -> None: | ||||
|         message = dict( | ||||
|             content='feed the cat', | ||||
|             type='stream', | ||||
|             sender_email='foo@example.com', | ||||
|         ) | ||||
| 
 | ||||
|         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) | ||||
|  |  | |||
|  | @ -100,6 +100,18 @@ class StubBotTestCase(TestCase): | |||
| 
 | ||||
|     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): | ||||
|         # type: (str, str) -> None | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Nikhil Mishra
						Nikhil Mishra