bots: Make trivia bot responses mention currently interacting user.

This commit is contained in:
Rohitt Vashishtha 2019-01-29 23:40:55 +05:30 committed by showell
parent 26c85bb9ed
commit 49d3f4cfbb
2 changed files with 19 additions and 18 deletions

View file

@ -95,13 +95,13 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
# test incorrect answer # test incorrect answer
with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id', with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id',
return_value=json.dumps(quiz)): return_value=json.dumps(quiz)):
self._test('answer Q001 B', '**WRONG!** B is not correct :disappointed:') self._test('answer Q001 B', '**:disappointed: WRONG, Foo Test User!** B is not correct.')
# test correct answer # test correct answer
with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id', with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id',
return_value=json.dumps(quiz)): return_value=json.dumps(quiz)):
with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.start_new_quiz') as mock_new_quiz: with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.start_new_quiz') as mock_new_quiz:
self._test('answer Q001 A', '**CORRECT!** Amphibian :tada:') self._test('answer Q001 A', '**:tada: Amphibian is correct, Foo Test User!**')
def test_update_quiz(self) -> None: def test_update_quiz(self) -> None:
quiz, bot_handler = self.get_test_quiz() quiz, bot_handler = self.get_test_quiz()
@ -120,14 +120,14 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
update_quiz(quiz, 'Q001', bot_handler) update_quiz(quiz, 'Q001', bot_handler)
# test for a correct answer # test for a correct answer
start_new_question, response = handle_answer(quiz, 'A', 'Q001', bot_handler) start_new_question, response = handle_answer(quiz, 'A', 'Q001', bot_handler, 'Test user')
self.assertTrue(start_new_question) self.assertTrue(start_new_question)
self.assertEqual(response, '**CORRECT!** Amphibian :tada:') self.assertEqual(response, '**:tada: Amphibian is correct, Test user!**')
# test for an incorrect answer # test for an incorrect answer
start_new_question, response = handle_answer(quiz, 'D', 'Q001', bot_handler) start_new_question, response = handle_answer(quiz, 'D', 'Q001', bot_handler, 'Test User')
self.assertFalse(start_new_question) self.assertFalse(start_new_question)
self.assertEqual(response, '**WRONG!** D is not correct :disappointed:') self.assertEqual(response, '**:disappointed: WRONG, Test User!** D is not correct.')
def test_handle_answer_three_failed_attempts(self) -> None: def test_handle_answer_three_failed_attempts(self) -> None:
quiz, bot_handler = self.get_test_quiz() quiz, bot_handler = self.get_test_quiz()
@ -136,8 +136,8 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
update_quiz(quiz, 'Q001', bot_handler) update_quiz(quiz, 'Q001', bot_handler)
# test response and storage after three failed attempts # test response and storage after three failed attempts
start_new_question, response = handle_answer(quiz, 'D', 'Q001', bot_handler) start_new_question, response = handle_answer(quiz, 'D', 'Q001', bot_handler, 'Test User')
self.assertEqual(response, '**WRONG!** :disappointed: The correct answer is Amphibian.') self.assertEqual(response, '**:disappointed: WRONG, Test User!** The correct answer is Amphibian.')
self.assertTrue(start_new_question) self.assertTrue(start_new_question)
quiz_reset = json.loads(bot_handler.storage.get('Q001')) quiz_reset = json.loads(bot_handler.storage.get('Q001'))
self.assertEqual(quiz_reset['pending'], False) self.assertEqual(quiz_reset['pending'], False)
@ -145,11 +145,11 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
# test response after question has ended # test response after question has ended
incorrect_answers = ['B', 'C', 'D'] incorrect_answers = ['B', 'C', 'D']
for ans in incorrect_answers: for ans in incorrect_answers:
start_new_question, response = handle_answer(quiz, ans, 'Q001', bot_handler) start_new_question, response = handle_answer(quiz, ans, 'Q001', bot_handler, 'Test User')
self.assertEqual(response, '**WRONG!** :disappointed: The correct answer is Amphibian.') self.assertEqual(response, '**:disappointed: WRONG, Test User!** The correct answer is Amphibian.')
self.assertFalse(start_new_question) self.assertFalse(start_new_question)
start_new_question, response = handle_answer(quiz, 'A', 'Q001', bot_handler) start_new_question, response = handle_answer(quiz, 'A', 'Q001', bot_handler, 'Test User')
self.assertEqual(response, '**CORRECT!** Amphibian :tada:') self.assertEqual(response, '**:tada: Amphibian is correct, Test User!**')
self.assertFalse(start_new_question) self.assertFalse(start_new_question)
# test storage after question has ended # test storage after question has ended

View file

@ -43,7 +43,8 @@ class TriviaQuizHandler:
bot_handler.send_reply(message, bot_response) bot_handler.send_reply(message, bot_response)
return return
quiz = json.loads(quiz_payload) quiz = json.loads(quiz_payload)
start_new_question, bot_response = handle_answer(quiz, answer, quiz_id, bot_handler) start_new_question, bot_response = handle_answer(quiz, answer, quiz_id,
bot_handler, message['sender_full_name'])
bot_handler.send_reply(message, bot_response) bot_handler.send_reply(message, bot_response)
if start_new_question: if start_new_question:
start_new_quiz(message, bot_handler) start_new_quiz(message, bot_handler)
@ -203,16 +204,16 @@ def update_quiz(quiz: Dict[str, Any], quiz_id: str, bot_handler: Any) -> None:
def build_response(is_correct: bool, num_answers: int) -> str: def build_response(is_correct: bool, num_answers: int) -> str:
if is_correct: if is_correct:
response = '**CORRECT!** {answer} :tada:' response = '**:tada: {answer} is correct, {sender_name}!**'
else: else:
if num_answers >= 3: if num_answers >= 3:
response = '**WRONG!** :disappointed: The correct answer is {answer}.' response = '**:disappointed: WRONG, {sender_name}!** The correct answer is {answer}.'
else: else:
response = '**WRONG!** {option} is not correct :disappointed:' response = '**:disappointed: WRONG, {sender_name}!** {option} is not correct.'
return response return response
def handle_answer(quiz: Dict[str, Any], option: str, quiz_id: str, def handle_answer(quiz: Dict[str, Any], option: str, quiz_id: str,
bot_handler: Any) -> Tuple[bool, str]: bot_handler: Any, sender_name: str) -> Tuple[bool, str]:
answer = quiz['answers'][quiz['correct_letter']] answer = quiz['answers'][quiz['correct_letter']]
is_new_answer = (option not in quiz['answered_options']) is_new_answer = (option not in quiz['answered_options'])
if is_new_answer: if is_new_answer:
@ -229,7 +230,7 @@ def handle_answer(quiz: Dict[str, Any], option: str, quiz_id: str,
update_quiz(quiz, quiz_id, bot_handler) update_quiz(quiz, quiz_id, bot_handler)
response = build_response(is_correct, num_answers).format( response = build_response(is_correct, num_answers).format(
option=option, answer=answer, id=quiz_id) option=option, answer=answer, id=quiz_id, sender_name=sender_name)
return start_new_question, response return start_new_question, response