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

        quiz_id = attr["selected_quiz"]["id"]
        idx = attr["counter"] - 1
        question = data.QUIZZES_LIST[quiz_id][idx]

        is_ans_correct = util.compare_slots(handler_input, question["correct"])

        if is_ans_correct:
            speech = question["correctResponse"]
            attr["quiz_score"] += 1
            handler_input.attributes_manager.session_attributes = attr
        else:
            speech = question["incorrectResponse"]

        if attr["counter"] < data.MAX_QUESTIONS:
            # Ask another question
            speech += util.get_current_score(attr["quiz_score"],
                                             attr["counter"])
            prompt = util.ask_question(handler_input, quiz_id)
            speech += prompt
            reprompt = prompt
            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
Exemplo n.º 2
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In QuizHandler")
        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "QUIZ"
        attr["counter"] = 0
        attr["quiz_score"] = 0
        attr["score"] = 0

        question = util.ask_question(handler_input)
        response_builder = handler_input.response_builder
        response_builder.speak(data.START_QUIZ_MESSAGE + (
            "Here is your {} question. "
        ).format(util.get_ordinal_indicator(attr["counter"])) + question)
        response_builder.ask(question)
        logger.info("after question asked")

        if data.USE_CARDS_FLAG:
            item = attr["quiz_item"]
            response_builder.set_card(
                ui.StandardCard(
                    title="Question #1",
                    text=data.START_QUIZ_MESSAGE + question,
                    image=ui.Image(
                        small_image_url=util.get_small_image(item),
                        large_image_url=util.get_large_image(item))))

        logger.info("after USE_CARDS_FLAG")

        if util.supports_display(handler_input):
            item = attr["quiz_item"]
            item_attr = attr["quiz_attr"]
            title = "Question #{}".format(str(attr["counter"]))
            background_img = Image(sources=[
                ImageInstance(url=util.get_image(
                    ht=1024, wd=600, label=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)))

        logger.info("about to return")

        return response_builder.response
Exemplo n.º 3
0
    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

        slots = handler_input.request_envelope.request.intent.slots
        choice = slots["choice"].value

        if choice != 'skip':
            qid = attr['counter']
            agree_candidates = data.ISSUES_LIST[qid][choice]
            agree_statement = util.get_speechcon(neutral=True, singular=True)
            for candidate in agree_candidates:
                attr['candidates_scores'][candidate] += 1
                if len(agree_candidates) == 1:
                    agree_statement += " " + candidate + " " + util.get_speechcon(
                        neutral=False, singular=True)
                elif agree_candidates.index(
                        candidate) != len(agree_candidates) - 1:
                    agree_statement += " " + candidate + ","
                else:
                    agree_statement += " and " + candidate + " "
            if len(agree_candidates) > 1:
                agree_statement += util.get_speechcon(neutral=False,
                                                      singular=False)
            if len(agree_candidates) == 0:
                agree_statement = " Okay. None of the top democrats agree with you. "
            speech = agree_statement
        else:
            attr['counter'] = data.MAX_QUESTIONS
            speech = ""

        if attr['counter'] < data.MAX_QUESTIONS:

            # Ask another question
            question = util.ask_question(handler_input)
            speech += question
            reprompt = question

            return response_builder.speak(speech).ask(reprompt).response
        else:
            # Finished all questions.
            k = Counter(attr['candidates_scores'])
            h = k.most_common(3)
            speech += " You have completed the 2020 Democratic Candidate Quiz! These 3 candidates agree with you on the most issues: {}, {}, and {}. ".format(
                h[0][0], h[1][0], h[2][0])
            speech += data.EXIT_SKILL_MESSAGE

            response_builder.set_should_end_session(True)

            return response_builder.speak(speech).response
Exemplo n.º 4
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In QuizHandler")
        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "QUIZ"
        attr["counter"] = 0
        attr["quiz_score"] = 0

        question = util.ask_question(handler_input)
        response_builder = handler_input.response_builder
        response_builder.speak("{} {}".format(data.START_QUIZ_MESSAGE,
                                              question))

        response_builder.ask(question)

        return response_builder.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

        # logger.info(handler_input.request_envelope.request.intent.slots["letter"])
        # logger.info(handler_input.request_envelope.request.intent.slots["letter"].resolutions)
        answer = handler_input.request_envelope.request.intent.slots[
            "letter"].resolutions.resolutions_per_authority[0].values[
                0].value.id
        is_ans_correct = (answer == attr["right"])
        # logger.info(f"attr: {attr}")

        if is_ans_correct:
            speech = "<audio src='soundbank://soundlibrary/human/amzn_sfx_crowd_applause_01'/>"
            attr["quiz_score"] += 1
            handler_input.attributes_manager.session_attributes = attr
        else:
            speech = "<audio src='soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_negative_response_02'/>"
            speech += f' The correct answer was. {attr["right_detail"]}. '

        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"]

            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
Exemplo n.º 6
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In QuizHandler")
        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "QUIZ"
        attr["counter"] = 0
        attr["quiz_score"] = 100
        attr['ready'] = []

        question = util.ask_question(handler_input)
        response_builder = handler_input.response_builder
        response_builder.speak(data.START_QUIZ_MESSAGE + question)
        response_builder.ask(question)

        if data.USE_CARDS_FLAG:
            item = attr["quiz_item"]
            response_builder.set_card(
                ui.StandardCard(
                    title="Question #1",
                    text=data.START_QUIZ_MESSAGE + question,
                ))

        if util.supports_display(handler_input):
            item = attr["quiz_item"]
            item_attr = attr["quiz_attr"]
            title = "Question #{}".format(str(attr["counter"]))
            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,
                                  title=title,
                                  list_items=item_list)))

        return response_builder.response
Exemplo n.º 7
0
    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 QuizHandler")
        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "QUIZ"
        logger.info(
            handler_input.request_envelope.request.intent.slots["category"])
        #category = handler_input.request_envelope.request.intent.slots["category"].value
        category = handler_input.request_envelope.request.intent.slots[
            "category"].resolutions.resolutions_per_authority[0].values[
                0].value.id
        attr["category"] = category
        attr["counter"] = 0
        attr["quiz_score"] = 0

        question = util.ask_question(handler_input)
        response_builder = handler_input.response_builder
        response_builder.speak(data.START_QUIZ_MESSAGE + " " + question)
        response_builder.ask(question)

        return response_builder.response
Exemplo n.º 9
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In QuizHandler")
        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "QUIZ"
        attr["counter"] = -1
        attr["candidates_scores"] = {
            'Bernie Sanders': 0,
            'Elizabeth Warren': 0,
            'Joe Biden': 0,
            'Pete Buttigieg': 0,
            'Mike Bloomberg': 0,
            'Tom Steyer': 0,
            'Tulsi Gabbard': 0,
            'Amy Klobuchar': 0
        }

        question = util.ask_question(handler_input)
        response_builder = handler_input.response_builder
        response_builder.speak(data.START_QUIZ_MESSAGE + question)
        response_builder.ask(question)

        return response_builder.response
    def handle(self, handler_input):
        #figure out which quiz we're giving
        fulldate = datetime.date.today()
        month = fulldate.month
        day = fulldate.day

        for q in data.HOLIDAYS_LIST:
            if q["id"] == month:
                selected_quiz = q
                break
        else:
            selected_quiz = None

        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "QUIZ"
        attr["counter"] = 0
        attr["quiz_score"] = 0
        attr["selected_quiz"] = selected_quiz

        if day == selected_quiz["day"]:
            today_is_the_day = True
            attr["quiz_score"] += 2
        else:
            today_is_the_day = False

        # give the player 2 points bonus if they are playing on the actual holiday
        bonus = util.get_bonus_message(selected_quiz["name"],
                                       selected_quiz["explanation"],
                                       today_is_the_day)

        question = util.ask_question(handler_input,
                                     attr["selected_quiz"]["id"])
        response_builder = handler_input.response_builder
        response_builder.speak(data.START_QUIZ_MESSAGE + bonus + question)
        response_builder.ask(question)

        return response_builder.response
Exemplo n.º 11
0
    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
Exemplo n.º 12
0
    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