示例#1
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In LaunchRequestHandler")
        attr = handler_input.attributes_manager.session_attributes
        attr["state"] = "WELCOME"

        ### Fun stuff for display backgorund###
        if util.supports_display(handler_input):
            background_img = Image(sources=[
                ImageInstance(url=util.get_image(
                    ht=1024, wd=600, label=item["abbreviation"]))
            ])

            response_builder.add_directive(
                RenderTemplateDirective(
                    ListTemplate1(token="Hear-o",
                                  back_button=BackButtonBehavior.HIDDEN,
                                  background_image=background_img,
                                  title="Hear-o",
                                  list_items=item_list)))
        ###################################################################

        handler_input.response_builder.speak(
            util.speak_in_english(data.WELCOME_MESSAGE)).ask(
                util.speak_in_english(data.WELCOME_MESSAGE))
        return handler_input.response_builder.response
示例#2
0
 def handle(self, handler_input):
     # type: (HandlerInput) -> Response
     logger.info("In DivisionHandler")
     attr = handler_input.attributes_manager.session_attributes
     attr["state"] = "QUIZ"
     
     if is_intent_name("DivisionIntent")(handler_input):
         attr["counter"] = 0
         attr["quiz_score"] = 0
         question = util.ask_division(handler_input)
         level = "division level 1"
         attr["type"] = "division"
     
     else:
         question = util.ask_division_levelUp(handler_input)
         level = "division level 2"
         attr["type"] = "division_1"
         
     response_builder = handler_input.response_builder
     response_builder.speak(data.START_QUIZ_MESSAGE.format(str(level)) + question)
     response_builder.ask(question)
     
     item_list = []
     if util.supports_display(handler_input):
         item = attr["quiz_item"]
         item_attr = attr["quiz_attr"]
         title = "Question #{} Khan: {}".format(str(attr["counter"]),kahn.URL_generate(str(attr["type"])))
         # title = "Khan URL: {}".format(kahn.URL_generate(str(attr["type"])))
         if is_intent_name("DivisionIntent")(handler_input):
             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)))
         else:
             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)))
             
         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
    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 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
示例#5
0
    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
示例#6
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
示例#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["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