def handle(self, handler_input): # type: (HandlerInput) -> Response logger.info("In QuizAnswerHandler") attr = handler_input.attributes_manager.session_attributes response_builder = handler_input.response_builder item = attr["quiz_item"] item_attr = attr["quiz_item"]['species'] is_ans_correct, option = util.compare_token_or_slots( handler_input=handler_input, value=item_attr) if is_ans_correct: speech = util.get_speechcon(correct_answer=True) speech += util.get_answer(item_attr, item) handler_input.attributes_manager.session_attributes = attr if option == 'miss': speech += data.SPEECH_PROUN.format(item_attr) speech += util.get_speech_description(item) speech += util.get_final_score(attr["quiz_score"], attr["counter"]) speech += data.EXIT_SKILL_MESSAGE #response_builder.set_should_end_session(True) if data.USE_CARDS_FLAG: #speech_card=speech.replace("<say-as interpret-as='interjection'>", "") #speech_card=speech_card.replace("</say-as><break strength='strong'/>, ") response_builder.set_card( ui.StandardCard(title="Final Score {}".format( str(attr["quiz_score"])), text=speech)) else: speech = util.get_speechcon(correct_answer=False) if attr['counter'] < data.MAX_QUESTIONS: attr["quiz_score"] -= 20 # Update item and item_attr for next question item = attr["quiz_item"] item_attr = attr["quiz_attr"] attr['ready'].append(item_attr) speech = util.get_hint(handler_input, attr['ready'], item) attr['counter'] += 1 if data.USE_CARDS_FLAG: response_builder.set_card( ui.StandardCard(title="Question #{}".format( str(attr["counter"])), text=speech)) else: speech = util.get_speechcon(correct_answer=False) speech += util.no_answer(item_attr) speech += util.get_speech_description(item) speech += data.EXIT_SKILL_MESSAGE return response_builder.speak(speech).ask(speech).response
def handle(self, handler_input): # type: (HandlerInput) -> Response logger.info("In QuizAnswerHandler") attr = handler_input.attributes_manager.session_attributes response_builder = handler_input.response_builder item = attr["current_word"] item_attr = attr["current_synonyms"] is_ans_correct = util.compare_token_or_slots( handler_input=handler_input, value=item_attr) if is_ans_correct: speech = util.get_speechcon(correct_answer=True) attr["quiz_score"] += 1 handler_input.attributes_manager.session_attributes = attr else: speech = util.get_speechcon(correct_answer=False) speech += util.get_answer(item_attr, item) if attr['counter'] < data.MAX_QUESTIONS: # Ask another question speech += util.get_current_score(attr["quiz_score"], attr["counter"]) question = util.ask_question(handler_input) speech += question reprompt = question # Update item and item_attr for next question item = attr["current_word"] item_attr = attr["current_synonyms"] return response_builder.speak(speech).ask(reprompt).response else: # Finished all questions. speech += util.get_final_score(attr["quiz_score"], attr["counter"]) speech += data.EXIT_SKILL_MESSAGE response_builder.set_should_end_session(True) return response_builder.speak(speech).response
def handle(self, handler_input): # type: (HandlerInput) -> Response logger.info("In QuizAnswerHandler") attr = handler_input.attributes_manager.session_attributes response_builder = handler_input.response_builder item = attr["quiz_item"] item_attr = attr["quiz_attr"] is_ans_correct = util.compare_token_or_slots( handler_input=handler_input, value=item[item_attr]) if is_ans_correct: speech = util.get_speechcon(correct_answer=True) attr["quiz_score"] += 1 handler_input.attributes_manager.session_attributes = attr else: speech = util.get_speechcon(correct_answer=False) speech += util.get_answer(item_attr, item) if attr['counter'] < data.MAX_QUESTIONS: # Ask another question speech += util.get_current_score(attr["quiz_score"], attr["counter"]) question = util.ask_question(handler_input) speech += question reprompt = question # Update item and item_attr for next question item = attr["quiz_item"] item_attr = attr["quiz_attr"] if data.USE_CARDS_FLAG: response_builder.set_card( ui.StandardCard( title="Question #{}".format(str(attr["counter"])), text=question, image=ui.Image( small_image_url=util.get_small_image(item), large_image_url=util.get_large_image(item)))) if util.supports_display(handler_input): title = "Question #{}".format(str(attr["counter"])) background_img = Image(sources=[ ImageInstance( util.get_image(ht=1024, wd=600, label=attr["quiz_item"] ["abbreviation"])) ]) item_list = [] for ans in util.get_multiple_choice_answers( item, item_attr, data.STATES_LIST): item_list.append( ListItem(token=ans, text_content=get_plain_text_content( primary_text=ans))) response_builder.add_directive( RenderTemplateDirective( ListTemplate1(token="Question", back_button=BackButtonBehavior.HIDDEN, background_image=background_img, title=title, list_items=item_list))) return response_builder.speak(speech).ask(reprompt).response else: # Finished all questions. speech += util.get_final_score(attr["quiz_score"], attr["counter"]) speech += data.EXIT_SKILL_MESSAGE response_builder.set_should_end_session(True) if data.USE_CARDS_FLAG: response_builder.set_card( ui.StandardCard(title="Final Score".format( str(attr["counter"])), text=(util.get_final_score( attr["quiz_score"], attr["counter"]) + data.EXIT_SKILL_MESSAGE))) if util.supports_display(handler_input): title = "Thank you for playing" primary_text = get_rich_text_content( primary_text=util.get_final_score(attr["quiz_score"], attr["counter"])) response_builder.add_directive( RenderTemplateDirective( BodyTemplate1(back_button=BackButtonBehavior.HIDDEN, title=title, text_content=primary_text))) return response_builder.speak(speech).response
def handle(self, handler_input): # type: (HandlerInput) -> Response logger.info("In QuizAnswerHandler") attr = handler_input.attributes_manager.session_attributes response_builder = handler_input.response_builder item = attr["quiz_item"] item_attr = attr["quiz_attr"] is_ans_correct = util.compare_token_or_slots( handler_input=handler_input, value=item[item_attr]) if is_ans_correct: speech = util.get_speechcon(correct_answer=True) attr["quiz_score"] += 1 handler_input.attributes_manager.session_attributes = attr else: speech = util.get_speechcon(correct_answer=False) speech += util.get_answer(item_attr, item) if attr['counter'] < data.MAX_ALL_QUESTIONS: # Ask another question speech += util.get_current_score( attr["quiz_score"], attr["counter"]) if attr['counter'] < data.MAX_QUESTIONS: if(attr.get("type")=="addition"): question = util.ask_addition(handler_input) elif(attr.get("type")=="subtraction"): question = util.ask_subtraction(handler_input) elif(attr.get("type")=="multiplication"): question = util.ask_multiplication(handler_input) elif(attr.get("type")=="division"): question = util.ask_division(handler_input) else: question = util.ask_question(handler_input) speech += question reprompt = question elif attr['counter'] == data.MAX_QUESTIONS: speech += data.NEXT_LEVEL_MESSAGE return response_builder.speak(speech).response else: if(attr.get("type")=="addition_1"): question = util.ask_addition_levelUp(handler_input) elif(attr.get("type")=="subtraction_1"): question = util.ask_subtraction_levelUp(handler_input) elif(attr.get("type")=="multiplication_1"): question = util.ask_multiplication_levelUp(handler_input) elif(attr.get("type")=="division_1"): question = util.ask_division_levelUp(handler_input) else: question = util.ask_question(handler_input) speech += question reprompt = question # Update item and item_attr for next question item = attr["quiz_item"] item_attr = attr["quiz_attr"] item_list = [] if util.supports_display(handler_input): if(attr.get("type")=="subtraction"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data.SUBTRACTION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="subtraction_1"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data_1.SUBTRACTION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="multiplication"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data.MULTIPLICATION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="multiplication_1"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data_1.MULTIPLICATION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="division"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data.DIVISION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="division_1"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data_1.DIVISION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="addition"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data.ADDITION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) elif(attr.get("type")=="addition_1"): background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) for ans in util.get_multiple_choice_answers( item, item_attr, data_1.ADDITION_LIST): item_list.append(ListItem( token=ans, text_content=get_plain_text_content(primary_text=ans))) title = "Question #{}".format(str(attr["counter"])) response_builder.add_directive( RenderTemplateDirective( ListTemplate1( token="Question", back_button=BackButtonBehavior.HIDDEN, background_image=background_img, title=title, list_items=item_list))) return response_builder.speak(speech).ask(reprompt).response else: # Finished all questions. speech += util.get_final_score(attr["quiz_score"], attr["counter"]) if attr["quiz_score"] >= data.MAX_QUESTIONS: speech += data.JOKE_MESSAGE speech += util.get_joke() else: speech += data.MATH_MESSAGE speech += util.get_math() speech += data.EXIT_SKILL_MESSAGE # response_builder.set_should_end_session(True) if util.supports_display(handler_input): title = "Thank you for playing" primary_text = get_rich_text_content( primary_text=util.get_final_score( attr["quiz_score"], attr["counter"])) # response_builder.add_directive( # RenderTemplateDirective( # BodyTemplate1( # back_button=BackButtonBehavior.HIDDEN, # title=title, # text_content=primary_text # ))) background_img = Image( sources=[ImageInstance( url="https://ppt.cc/[email protected]")]) response_builder.add_directive( RenderTemplateDirective( BodyTemplate3( title="", token="Question", background_image=background_img))) return response_builder.speak(speech).response