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
with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id',
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
with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id',
return_value=json.dumps(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:
quiz, bot_handler = self.get_test_quiz()
@ -120,14 +120,14 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
update_quiz(quiz, 'Q001', bot_handler)
# 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.assertEqual(response, '**CORRECT!** Amphibian :tada:')
self.assertEqual(response, '**:tada: Amphibian is correct, Test user!**')
# 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.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:
quiz, bot_handler = self.get_test_quiz()
@ -136,8 +136,8 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
update_quiz(quiz, 'Q001', bot_handler)
# test response and storage after three failed attempts
start_new_question, response = handle_answer(quiz, 'D', 'Q001', bot_handler)
self.assertEqual(response, '**WRONG!** :disappointed: The correct answer is Amphibian.')
start_new_question, response = handle_answer(quiz, 'D', 'Q001', bot_handler, 'Test User')
self.assertEqual(response, '**:disappointed: WRONG, Test User!** The correct answer is Amphibian.')
self.assertTrue(start_new_question)
quiz_reset = json.loads(bot_handler.storage.get('Q001'))
self.assertEqual(quiz_reset['pending'], False)
@ -145,11 +145,11 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
# test response after question has ended
incorrect_answers = ['B', 'C', 'D']
for ans in incorrect_answers:
start_new_question, response = handle_answer(quiz, ans, 'Q001', bot_handler)
self.assertEqual(response, '**WRONG!** :disappointed: The correct answer is Amphibian.')
start_new_question, response = handle_answer(quiz, ans, 'Q001', bot_handler, 'Test User')
self.assertEqual(response, '**:disappointed: WRONG, Test User!** The correct answer is Amphibian.')
self.assertFalse(start_new_question)
start_new_question, response = handle_answer(quiz, 'A', 'Q001', bot_handler)
self.assertEqual(response, '**CORRECT!** Amphibian :tada:')
start_new_question, response = handle_answer(quiz, 'A', 'Q001', bot_handler, 'Test User')
self.assertEqual(response, '**:tada: Amphibian is correct, Test User!**')
self.assertFalse(start_new_question)
# test storage after question has ended

View file

@ -43,7 +43,8 @@ class TriviaQuizHandler:
bot_handler.send_reply(message, bot_response)
return
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)
if start_new_question:
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:
if is_correct:
response = '**CORRECT!** {answer} :tada:'
response = '**:tada: {answer} is correct, {sender_name}!**'
else:
if num_answers >= 3:
response = '**WRONG!** :disappointed: The correct answer is {answer}.'
response = '**:disappointed: WRONG, {sender_name}!** The correct answer is {answer}.'
else:
response = '**WRONG!** {option} is not correct :disappointed:'
response = '**:disappointed: WRONG, {sender_name}!** {option} is not correct.'
return response
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']]
is_new_answer = (option not in quiz['answered_options'])
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)
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