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
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In DefinitionHandler")
        response_builder = handler_input.response_builder
        item, is_resolved = util.get_item(
            slots=handler_input.request_envelope.request.intent.slots,
            states_list=data.STATES_LIST)

        if is_resolved:
            if data.USE_CARDS_FLAG:
                response_builder.set_card(
                    ui.StandardCard(
                        title=util.get_card_title(item),
                        text=util.get_card_description(item),
                        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):
                img = Image(
                    sources=[ImageInstance(url=util.get_large_image(item))])
                title = util.get_card_title(item)
                primary_text = get_plain_text_content(
                    primary_text=util.get_card_description(item))

                response_builder.add_directive(
                    RenderTemplateDirective(
                        BodyTemplate2(
                            back_button=BackButtonBehavior.VISIBLE,
                            image=img, title=title,
                            text_content=primary_text)))

            response_builder.speak(
                util.get_speech_description(item)).ask(data.REPROMPT_SPEECH)

        else:
            response_builder.speak(
                util.get_bad_answer(item)).ask(util.get_bad_answer(item))

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

        response_builder = handler_input.response_builder
        response_builder.speak(data.START_QUIZ_MESSAGE)
        util.initdata()
        #        question = util.ask_question(handler_input)
        question = util.ask_my_question(handler_input)
        response_builder.speak(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,
                    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):
#            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)))

        return response_builder.response
Beispiel #4
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