async def test_should_recognize_valid_number_choice(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message,
                                    text="Please choose a color."),
                    choices=_color_choices,
                )
                await dialog_context.prompt("prompt", options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)

            await convo_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        choice_prompt = ChoicePrompt("prompt")

        dialogs.add(choice_prompt)

        step1 = await adapter.send("Hello")
        step2 = await step1.assert_reply(
            "Please choose a color. (1) red, (2) green, or (3) blue")
        step3 = await step2.send("1")
        await step3.assert_reply("red")
Exemplo n.º 2
0
    async def test_number_prompt_defaults_to_en_us_culture(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)
            results = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message, text="Enter a number.")
                )
                await dialog_context.prompt("NumberPrompt", options)

            elif results.status == DialogTurnStatus.Complete:
                number_result = float(results.result)
                await turn_context.send_activity(
                    MessageFactory.text(f"Bot received the number '{number_result}'.")
                )

            await conver_state.save_changes(turn_context)

        conver_state = ConversationState(MemoryStorage())
        dialog_state = conver_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        number_prompt = NumberPrompt("NumberPrompt")
        dialogs.add(number_prompt)

        adapter = TestAdapter(exec_test)

        step1 = await adapter.send("hello")
        step2 = await step1.assert_reply("Enter a number.")
        step3 = await step2.send("3.14")
        await step3.assert_reply("Bot received the number '3.14'.")
    async def test_guid_phone_prompt(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if (results.status == DialogTurnStatus.Empty):
                options = PromptOptions(prompt=Activity(
                    type=ActivityTypes.message, text="test the guid format"))
                await dialog_context.prompt("guidprompt", options)

            elif results.status == DialogTurnStatus.Complete:
                reply = results.result
                await turn_context.send_activity(reply)

            await conv_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        conv_state = ConversationState(MemoryStorage())

        dialog_state = conv_state.create_property("dialog-state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(GuidPrompt("guidprompt"))

        step1 = await adapter.test('Hello', 'test the guid format')
        step2 = await step1.send(
            'my azure id is 7d7b0205-9411-4a29-89ac-b9cd905886fa')
        await step2.assert_reply("7d7b0205-9411-4a29-89ac-b9cd905886fa")
Exemplo n.º 4
0
    async def test_email_prompt(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if (results.status == DialogTurnStatus.Empty):
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message,
                                    text="What is your email address?"))
                await dialog_context.prompt("emailprompt", options)

            elif results.status == DialogTurnStatus.Complete:
                reply = results.result
                await turn_context.send_activity(reply)

            await conv_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        conv_state = ConversationState(MemoryStorage())

        dialog_state = conv_state.create_property("dialog-state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(EmailPrompt("emailprompt"))

        step1 = await adapter.test('Hello', 'What is your email address?')
        step2 = await step1.send('My email id is [email protected]')
        await step2.assert_reply("*****@*****.**")
    async def test_ip_address_prompt(self):
        async def exec_test(turn_context:TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if (results.status == DialogTurnStatus.Empty):
                options = PromptOptions(
                    prompt = Activity(
                        type = ActivityTypes.message, 
                        text = "What is your DNS?"
                        )
                    )
                await dialog_context.prompt("ipaddressprompt", options)

            elif results.status == DialogTurnStatus.Complete:
                reply = results.result
                await turn_context.send_activity(reply)

            await conv_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        conv_state = ConversationState(MemoryStorage())

        dialog_state = conv_state.create_property("dialog-state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(InternetProtocolPrompt("ipaddressprompt",InternetProtocolPromptType.IPAddress))

        step1 = await adapter.test('Hello', 'What is your DNS?')
        step2 = await step1.send('am using microsoft DNS 127.0.0.1')
        await step2.assert_reply("127.0.0.1")
    async def test_confirm_prompt(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(prompt=Activity(
                    type=ActivityTypes.message, text="Please confirm."))
                await dialog_context.prompt("ConfirmPrompt", options)
            elif results.status == DialogTurnStatus.Complete:
                message_text = "Confirmed" if results.result else "Not confirmed"
                await turn_context.send_activity(
                    MessageFactory.text(message_text))

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(exec_test)

        # Create new ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet, and ChoicePrompt.
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)
        confirm_prompt = ConfirmPrompt("ConfirmPrompt",
                                       default_locale="English")
        dialogs.add(confirm_prompt)

        step1 = await adapter.send("hello")
        step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No")
        step3 = await step2.send("yes")
        await step3.assert_reply("Confirmed")
    async def test_ip_url_prompt(self):
        async def exec_test(turn_context:TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if (results.status == DialogTurnStatus.Empty):
                options = PromptOptions(
                    prompt = Activity(
                        type = ActivityTypes.message, 
                        text = "What is your favorite web site?"
                        )
                    )
                await dialog_context.prompt("urlprompt", options)

            elif results.status == DialogTurnStatus.Complete:
                reply = results.result
                await turn_context.send_activity(reply)

            await conv_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        conv_state = ConversationState(MemoryStorage())

        dialog_state = conv_state.create_property("dialog-state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(InternetProtocolPrompt("urlprompt",InternetProtocolPromptType.URL))

        step1 = await adapter.test('Hello', 'What is your favorite web site?')
        step2 = await step1.send('My favorite web site is http://rvinothrajendran.github.io/')
        await step2.assert_reply("http://rvinothrajendran.github.io/")
Exemplo n.º 8
0
    async def test_attachment_prompt_with_input_hint(self):
        prompt_activity = Activity(
            type=ActivityTypes.message,
            text="please add an attachment.",
            input_hint=InputHints.accepting_input,
        )

        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(prompt=copy.copy(prompt_activity))
                await dialog_context.prompt("AttachmentPrompt", options)
            elif results.status == DialogTurnStatus.Complete:
                attachment = results.result[0]
                content = MessageFactory.text(attachment.content)
                await turn_context.send_activity(content)

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(exec_test)

        # Create ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and AttachmentPrompt.
        dialog_state = convo_state.create_property("dialog_state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(AttachmentPrompt("AttachmentPrompt"))

        step1 = await adapter.send("hello")
        await step1.assert_reply(prompt_activity)
Exemplo n.º 9
0
    async def test_should_not_send_retry_if_not_specified(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if results.status == DialogTurnStatus.Empty:
                await dialog_context.begin_dialog("AttachmentPrompt", PromptOptions())
            elif results.status == DialogTurnStatus.Complete:
                attachment = results.result[0]
                content = MessageFactory.text(attachment.content)
                await turn_context.send_activity(content)

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(exec_test)

        # Create ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and AttachmentPrompt.
        dialog_state = convo_state.create_property("dialog_state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(AttachmentPrompt("AttachmentPrompt"))

        # Create incoming activity with attachment.
        attachment = Attachment(content="some content", content_type="text/plain")
        attachment_activity = Activity(
            type=ActivityTypes.message, attachments=[attachment]
        )

        step1 = await adapter.send("hello")
        step2 = await step1.send("what?")
        step3 = await step2.send(attachment_activity)
        await step3.assert_reply("some content")
Exemplo n.º 10
0
    async def test_should_create_prompt_with_suggested_action_style_when_specified(
        self, ):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message,
                                    text="Please choose a color."),
                    choices=_color_choices,
                    style=ListStyle.suggested_action,
                )
                await dialog_context.prompt("prompt", options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)

            await convo_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        choice_prompt = ChoicePrompt("prompt")

        dialogs.add(choice_prompt)

        step1 = await adapter.send("Hello")
        step2 = await step1.assert_reply("Please choose a color.")
        step3 = await step2.send(_answer_message)
        await step3.assert_reply("red")
Exemplo n.º 11
0
    async def test_number_prompt_uses_locale_specified_in_activity(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)
            results = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message, text="Enter a number.")
                )
                await dialog_context.prompt("NumberPrompt", options)

            elif results.status == DialogTurnStatus.Complete:
                number_result = float(results.result)
                self.assertEqual(3.14, number_result)

            await conver_state.save_changes(turn_context)

        conver_state = ConversationState(MemoryStorage())
        dialog_state = conver_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        number_prompt = NumberPrompt("NumberPrompt", None, None)
        dialogs.add(number_prompt)

        adapter = TestAdapter(exec_test)

        step1 = await adapter.send("hello")
        step2 = await step1.assert_reply("Enter a number.")
        await step2.send(
            Activity(type=ActivityTypes.message, text="3,14", locale=Culture.Spanish)
        )
    async def test_number_prompt_retry(self):
        async def exec_test(turn_context: TurnContext) -> None:
            dialog_context: DialogContext = await dialogs.create_context(
                turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message,
                                    text="Enter a number."),
                    retry_prompt=Activity(type=ActivityTypes.message,
                                          text="You must enter a number."),
                )
                await dialog_context.prompt("NumberPrompt", options)
            elif results.status == DialogTurnStatus.Complete:
                number_result = results.result
                await turn_context.send_activity(
                    MessageFactory.text(
                        f"Bot received the number '{number_result}'."))

            await convo_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)
        number_prompt = NumberPrompt(dialog_id="NumberPrompt",
                                     validator=None,
                                     default_locale=Culture.English)
        dialogs.add(number_prompt)

        step1 = await adapter.send("hello")
        await step1.assert_reply("Enter a number.")
    async def test_should_recognize_and_use_custom_locale_dict(self, ):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(prompt=Activity(
                    type=ActivityTypes.message, text="Please confirm."))
                await dialog_context.prompt("prompt", options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)

            await convo_state.save_changes(turn_context)

        async def validator(prompt: PromptValidatorContext) -> bool:
            assert prompt

            if not prompt.recognized.succeeded:
                await prompt.context.send_activity("Bad input.")

            return prompt.recognized.succeeded

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        culture = PromptCultureModel(
            locale="custom-locale",
            no_in_language="customNo",
            yes_in_language="customYes",
            separator="customSeparator",
            inline_or="customInlineOr",
            inline_or_more="customInlineOrMore",
        )

        custom_dict = {
            culture.locale: (
                Choice(culture.yes_in_language),
                Choice(culture.no_in_language),
                ChoiceFactoryOptions(culture.separator, culture.inline_or,
                                     culture.inline_or_more, True),
            )
        }

        confirm_prompt = ConfirmPrompt("prompt",
                                       validator,
                                       choice_defaults=custom_dict)
        dialogs.add(confirm_prompt)

        step1 = await adapter.send(
            Activity(type=ActivityTypes.message,
                     text="Hello",
                     locale=culture.locale))
        await step1.assert_reply(
            "Please confirm. (1) customYescustomInlineOr(2) customNo")
Exemplo n.º 14
0
 async def test_conversation_state_bad_conversation_throws(self):
     dictionary = {}
     user_state = ConversationState(MemoryStorage(dictionary))
     context = TestUtilities.create_empty_context()
     context.activity.conversation = None
     test_property = user_state.create_property("test")
     with self.assertRaises(AttributeError):
         await test_property.get(context)
Exemplo n.º 15
0
    async def test_should_call_oauth_prompt(self):
        connection_name = "myConnection"
        token = "abc123"

        async def callback_handler(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if results.status == DialogTurnStatus.Empty:
                await dialog_context.prompt("prompt", PromptOptions())
            elif results.status == DialogTurnStatus.Complete:
                if results.result.token:
                    await turn_context.send_activity("Logged in.")
                else:
                    await turn_context.send_activity("Failed")

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(callback_handler)

        # Create ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and AttachmentPrompt.
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)
        dialogs.add(
            OAuthPrompt(
                "prompt",
                OAuthPromptSettings(connection_name, "Login", None, 300000)))

        async def inspector(activity: Activity, description: str = None):  # pylint: disable=unused-argument

            self.assertTrue(len(activity.attachments) == 1)
            self.assertTrue(activity.attachments[0].content_type ==
                            CardFactory.content_types.oauth_card)

            # send a mock EventActivity back to the bot with the token
            adapter.add_user_token(connection_name, activity.channel_id,
                                   activity.recipient.id, token)

            event_activity = create_reply(activity)
            event_activity.type = ActivityTypes.event
            event_activity.from_property, event_activity.recipient = (
                event_activity.recipient,
                event_activity.from_property,
            )
            event_activity.name = "tokens/response"
            event_activity.value = TokenResponse(
                connection_name=connection_name, token=token)

            context = TurnContext(adapter, event_activity)
            await callback_handler(context)

        step1 = await adapter.send("Hello")
        step2 = await step1.assert_reply(inspector)
        await step2.assert_reply("Logged in.")
Exemplo n.º 16
0
    async def test_should_end_oauth_prompt_on_invalid_message_when_end_on_invalid_message(
        self, ):
        connection_name = "myConnection"
        token = "abc123"
        magic_code = "888999"

        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                await dialog_context.prompt("prompt", PromptOptions())
            elif results.status == DialogTurnStatus.Complete:
                if results.result and results.result.token:
                    await turn_context.send_activity("Failed")

                else:
                    await turn_context.send_activity("Ended")

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(exec_test)

        # Create ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and AttachmentPrompt.
        dialog_state = convo_state.create_property("dialog_state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(
            OAuthPrompt(
                "prompt",
                OAuthPromptSettings(connection_name, "Login", None, 300000,
                                    None, True),
            ))

        def inspector(activity: Activity, description: str = None):  # pylint: disable=unused-argument
            assert len(activity.attachments) == 1
            assert (activity.attachments[0].content_type ==
                    CardFactory.content_types.oauth_card)

            # send a mock EventActivity back to the bot with the token
            adapter.add_user_token(
                connection_name,
                activity.channel_id,
                activity.recipient.id,
                token,
                magic_code,
            )

        step1 = await adapter.send("Hello")
        step2 = await step1.assert_reply(inspector)
        step3 = await step2.send("test invalid message")
        await step3.assert_reply("Ended")
Exemplo n.º 17
0
    async def test_waterfall_with_class(self):
        convo_state = ConversationState(MemoryStorage())
        TestAdapter()
        # TODO: Fix Autosave Middleware
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        dialogs.add(MyWaterfallDialog("test"))
        self.assertNotEqual(dialogs, None)
        self.assertEqual(len(dialogs._dialogs), 1)  # pylint: disable=protected-access
Exemplo n.º 18
0
    async def test_should_send_ignore_retry_rompt_if_validator_replies(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(
                        type=ActivityTypes.message, text="please add an attachment."
                    ),
                    retry_prompt=Activity(
                        type=ActivityTypes.message, text="please try again."
                    ),
                )
                await dialog_context.prompt("AttachmentPrompt", options)
            elif results.status == DialogTurnStatus.Complete:
                attachment = results.result[0]
                content = MessageFactory.text(attachment.content)
                await turn_context.send_activity(content)

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(exec_test)

        # Create ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and AttachmentPrompt.
        dialog_state = convo_state.create_property("dialog_state")
        dialogs = DialogSet(dialog_state)

        async def aux_validator(prompt_context: PromptValidatorContext):
            assert prompt_context, "Validator missing prompt_context"

            if not prompt_context.recognized.succeeded:
                await prompt_context.context.send_activity("Bad input.")

            return prompt_context.recognized.succeeded

        dialogs.add(AttachmentPrompt("AttachmentPrompt", aux_validator))

        # Create incoming activity with attachment.
        attachment = Attachment(content="some content", content_type="text/plain")
        attachment_activity = Activity(
            type=ActivityTypes.message, attachments=[attachment]
        )
        invalid_activty = Activity(type=ActivityTypes.message, text="invalid")

        step1 = await adapter.send("hello")
        step2 = await step1.assert_reply("please add an attachment.")
        step3 = await step2.send(invalid_activty)
        step4 = await step3.assert_reply("Bad input.")
        step5 = await step4.send(attachment_activity)
        await step5.assert_reply("some content")
Exemplo n.º 19
0
    async def test_number_prompt_validator(self):
        async def exec_test(turn_context: TurnContext) -> None:
            dialog_context = await dialogs.create_context(turn_context)
            results = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message, text="Enter a number."),
                    retry_prompt=Activity(
                        type=ActivityTypes.message,
                        text="You must enter a positive number less than 100.",
                    ),
                )
                await dialog_context.prompt("NumberPrompt", options)

            elif results.status == DialogTurnStatus.Complete:
                number_result = int(results.result)
                await turn_context.send_activity(
                    MessageFactory.text(f"Bot received the number '{number_result}'.")
                )

            await conver_state.save_changes(turn_context)

        # Create new ConversationState with MemoryStorage and register the state as middleware.
        conver_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and register the WaterfallDialog.
        dialog_state = conver_state.create_property("dialogState")

        dialogs = DialogSet(dialog_state)

        # Create and add number prompt to DialogSet.
        async def validator(prompt_context: PromptValidatorContext):
            result = prompt_context.recognized.value

            if 0 < result < 100:
                return True

            return False

        number_prompt = NumberPrompt(
            "NumberPrompt", validator, default_locale=Culture.English
        )
        dialogs.add(number_prompt)

        adapter = TestAdapter(exec_test)

        step1 = await adapter.send("hello")
        step2 = await step1.assert_reply("Enter a number.")
        step3 = await step2.send("150")
        step4 = await step3.assert_reply(
            "You must enter a positive number less than 100."
        )
        step5 = await step4.send("64")
        await step5.assert_reply("Bot received the number '64'.")
    async def test_should_display_choices_on_hero_card_with_additional_attachment(self):
        size_choices = ["large", "medium", "small"]
        card = CardFactory.adaptive_card(
            {
                "type": "AdaptiveCard",
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
                "version": "1.2",
                "body": [],
            }
        )
        card_activity = Activity(attachments=[card])

        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(prompt=card_activity, choices=size_choices)
                await dialog_context.prompt("prompt", options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)

            await convo_state.save_changes(turn_context)

        def assert_expected_activity(
            activity: Activity, description
        ):  # pylint: disable=unused-argument
            assert len(activity.attachments) == 2
            assert (
                activity.attachments[0].content_type
                == CardFactory.content_types.adaptive_card
            )
            assert (
                activity.attachments[1].content_type
                == CardFactory.content_types.hero_card
            )

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        choice_prompt = ChoicePrompt("prompt")

        # Change the ListStyle of the prompt to ListStyle.none.
        choice_prompt.style = ListStyle.hero_card

        dialogs.add(choice_prompt)

        step1 = await adapter.send("Hello")
        await step1.assert_reply(assert_expected_activity)
    async def test_confirm_prompt_should_default_to_english_locale(self):
        async def exec_test(turn_context: TurnContext):

            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message,
                                    text="Please confirm."),
                    retry_prompt=Activity(
                        type=ActivityTypes.message,
                        text=
                        "Please confirm, say 'yes' or 'no' or something like that.",
                    ),
                )
                await dialog_context.prompt("ConfirmPrompt", options)
            elif results.status == DialogTurnStatus.Complete:
                message_text = "Confirmed" if results.result else "Not confirmed"
                await turn_context.send_activity(
                    MessageFactory.text(message_text))

            await convo_state.save_changes(turn_context)

        locales = [None, "", "not-supported"]

        for locale in locales:
            # Initialize TestAdapter.
            adapter = TestAdapter(exec_test)

            # Create new ConversationState with MemoryStorage and register the state as middleware.
            convo_state = ConversationState(MemoryStorage())

            # Create a DialogState property, DialogSet, and ChoicePrompt.
            dialog_state = convo_state.create_property("dialogState")
            dialogs = DialogSet(dialog_state)
            confirm_prompt = ConfirmPrompt("ConfirmPrompt")
            confirm_prompt.choice_options = ChoiceFactoryOptions(
                include_numbers=True)
            dialogs.add(confirm_prompt)

            step1 = await adapter.send(
                Activity(type=ActivityTypes.message,
                         text="Hello",
                         locale=locale))
            step2 = await step1.assert_reply(
                "Please confirm. (1) Yes or (2) No")
            step3 = await step2.send("lala")
            step4 = await step3.assert_reply(
                "Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No"
            )
            step5 = await step4.send("2")
            await step5.assert_reply("Not confirmed")
Exemplo n.º 22
0
    async def test_on_prompt_with_null_options_fails(self):
        conver_state = ConversationState(MemoryStorage())
        dialog_state = conver_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        number_prompt_mock = NumberPromptMock(
            dialog_id="NumberPromptMock", validator=None, default_locale=Culture.English
        )
        dialogs.add(number_prompt_mock)

        with self.assertRaises(TypeError):
            await number_prompt_mock.on_recognize_null_context()
Exemplo n.º 23
0
    def __init__(
        self,
        conversation_state: ConversationState,
        skills_config: SkillConfiguration,
        skill_client: SkillHttpClient,
        config: DefaultConfig,
        dialog: Dialog,
    ):
        self._bot_id = config.APP_ID
        self._skill_client = skill_client
        self._skills_config = skills_config
        self._conversation_state = conversation_state
        self._dialog = dialog
        self._dialog_state_property = conversation_state.create_property(
            "DialogState")

        # Create state property to track the delivery mode and active skill.
        self._delivery_mode_property = conversation_state.create_property(
            DELIVERY_MODE_PROPERTY_NAME)
        self._active_skill_property = conversation_state.create_property(
            ACTIVE_SKILL_PROPERTY_NAME)
    async def test_oauth_prompt_doesnt_detect_code_in_begin_dialog(self):
        connection_name = "myConnection"
        token = "abc123"
        magic_code = "888999"

        async def exec_test(turn_context: TurnContext):
            # Add a magic code to the adapter preemptively so that we can test if the message that triggers
            # BeginDialogAsync uses magic code detection
            adapter.add_user_token(
                connection_name,
                turn_context.activity.channel_id,
                turn_context.activity.from_property.id,
                token,
                magic_code,
            )

            dialog_context = await dialogs.create_context(turn_context)

            results = await dialog_context.continue_dialog()
            if results.status == DialogTurnStatus.Empty:

                # If magicCode is detected when prompting, this will end the dialog and return the token in tokenResult
                token_result = await dialog_context.prompt("prompt", PromptOptions())
                if isinstance(token_result.result, TokenResponse):
                    self.assertTrue(False)  # pylint: disable=redundant-unittest-assert

            await convo_state.save_changes(turn_context)

        # Initialize TestAdapter.
        adapter = TestAdapter(exec_test)

        # Create ConversationState with MemoryStorage and register the state as middleware.
        convo_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and AttachmentPrompt.
        dialog_state = convo_state.create_property("dialog_state")
        dialogs = DialogSet(dialog_state)
        dialogs.add(
            OAuthPrompt(
                "prompt", OAuthPromptSettings(connection_name, "Login", None, 300000)
            )
        )

        def inspector(activity: Activity, description: str = None):  # pylint: disable=unused-argument
            assert len(activity.attachments) == 1
            assert (
                activity.attachments[0].content_type
                == CardFactory.content_types.oauth_card
            )

        step1 = await adapter.send(magic_code)
        await step1.assert_reply(inspector)
Exemplo n.º 25
0
    async def test_number_uses_locale_specified_in_constructor(self):
        # Create new ConversationState with MemoryStorage and register the state as middleware.
        conver_state = ConversationState(MemoryStorage())

        # Create a DialogState property, DialogSet and register the WaterfallDialog.
        dialog_state = conver_state.create_property("dialogState")

        dialogs = DialogSet(dialog_state)

        # Create and add number prompt to DialogSet.
        number_prompt = NumberPrompt(
            "NumberPrompt", None, default_locale=Culture.Spanish
        )
        dialogs.add(number_prompt)

        async def exec_test(turn_context: TurnContext) -> None:

            dialog_context = await dialogs.create_context(turn_context)
            results = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                await dialog_context.begin_dialog(
                    "NumberPrompt",
                    PromptOptions(
                        prompt=MessageFactory.text(
                            "How much money is in your gaming account?"
                        )
                    ),
                )
            else:
                if results.status == DialogTurnStatus.Complete:
                    number_result = results.result
                    await turn_context.send_activity(
                        MessageFactory.text(
                            f"You say you have ${number_result} in your gaming account."
                        )
                    )

            await conver_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        test_flow = TestFlow(None, adapter)

        test_flow2 = await test_flow.send("Hello")
        test_flow3 = await test_flow2.assert_reply(
            "How much money is in your gaming account?"
        )
        test_flow4 = await test_flow3.send("I've got $1.200.555,42 in my account.")
        await test_flow4.assert_reply(
            "You say you have $1200555.42 in your gaming account."
        )
Exemplo n.º 26
0
 def __init__(
     self,
     conversation_state: ConversationState,
     skills_config: SkillConfiguration,
     skill_client: SkillHttpClient,
     config: DefaultConfig,
 ):
     self._bot_id = config.APP_ID
     self._skill_client = skill_client
     self._skills_config = skills_config
     self._conversation_state = conversation_state
     self._active_skill_property = conversation_state.create_property(
         ACTIVE_SKILL_PROPERTY_NAME)
Exemplo n.º 27
0
 def __init__(
     self,
     conversation_state: ConversationState,
     skills_config: SkillConfiguration,
     skill_client: BotFrameworkHttpClient,
     config: DefaultConfig,
 ):
     self._bot_id = config.APP_ID
     self._skill_client = skill_client
     self._skills_config = skills_config
     self._conversation_state = conversation_state
     self._active_skill_property = conversation_state.create_property(
         "activeSkillProperty")
Exemplo n.º 28
0
    async def test_should_send_ignore_retry_prompt_if_validator_replies(self):
        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(
                        type=ActivityTypes.message, text="Please choose a color."
                    ),
                    retry_prompt=Activity(
                        type=ActivityTypes.message,
                        text="Please choose red, blue, or green.",
                    ),
                    choices=_color_choices,
                )
                await dialog_context.prompt("prompt", options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)

            await convo_state.save_changes(turn_context)

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        async def validator(prompt: PromptValidatorContext) -> bool:
            assert prompt

            if not prompt.recognized.succeeded:
                await prompt.context.send_activity("Bad input.")

            return prompt.recognized.succeeded

        choice_prompt = ChoicePrompt("prompt", validator)

        dialogs.add(choice_prompt)

        step1 = await adapter.send("Hello")
        step2 = await step1.assert_reply(
            "Please choose a color. (1) red, (2) green, or (3) blue"
        )
        step3 = await step2.send(_invalid_message)
        step4 = await step3.assert_reply("Bad input.")
        step5 = await step4.send(_answer_message)
        await step5.assert_reply("red")
    async def test_should_use_default_locale_when_rendering_choices(self):
        async def exec_test(turn_context: TurnContext):
            dc = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dc.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(type=ActivityTypes.message, text='Please choose a color.'),
                    choices=_color_choices
                )
                await dc.prompt('prompt', options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)
            
            await convo_state.save_changes(turn_context)
        
        adapter = TestAdapter(exec_test)
        
        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property('dialogState')
        dialogs = DialogSet(dialog_state)

        async def validator(prompt: PromptValidatorContext) -> bool:
            assert prompt
            
            if not prompt.recognized.succeeded:
                await prompt.context.send_activity('Bad input.')
            
            return prompt.recognized.succeeded
        
        choice_prompt = ChoicePrompt(
            'prompt',
            validator,
            default_locale=Culture.Spanish
        )

        dialogs.add(choice_prompt)

        step1 = await adapter.send(Activity(type=ActivityTypes.message, text='Hello'))
        # TODO ChoiceFactory.inline() is broken, where it only uses hard-coded English locale.
        # commented out the CORRECT assertion below, until .inline() is fixed to use proper locale
        # step2 = await step1.assert_reply('Please choose a color. (1) red, (2) green, o (3) blue')
        step2 = await step1.assert_reply('Please choose a color. (1) red, (2) green, or (3) blue')
        step3 = await step2.send(_invalid_message)
        step4 = await step3.assert_reply('Bad input.')
        step5 = await step4.send(Activity(type=ActivityTypes.message, text='red'))
        await step5.assert_reply('red')
    async def test_should_display_choices_on_hero_card(self):
        size_choices = ["large", "medium", "small"]

        async def exec_test(turn_context: TurnContext):
            dialog_context = await dialogs.create_context(turn_context)

            results: DialogTurnResult = await dialog_context.continue_dialog()

            if results.status == DialogTurnStatus.Empty:
                options = PromptOptions(
                    prompt=Activity(
                        type=ActivityTypes.message, text="Please choose a size."
                    ),
                    choices=size_choices,
                )
                await dialog_context.prompt("prompt", options)
            elif results.status == DialogTurnStatus.Complete:
                selected_choice = results.result
                await turn_context.send_activity(selected_choice.value)

            await convo_state.save_changes(turn_context)

        def assert_expected_activity(
            activity: Activity, description
        ):  # pylint: disable=unused-argument
            assert len(activity.attachments) == 1
            assert (
                activity.attachments[0].content_type
                == CardFactory.content_types.hero_card
            )
            assert activity.attachments[0].content.text == "Please choose a size."

        adapter = TestAdapter(exec_test)

        convo_state = ConversationState(MemoryStorage())
        dialog_state = convo_state.create_property("dialogState")
        dialogs = DialogSet(dialog_state)

        choice_prompt = ChoicePrompt("prompt")

        # Change the ListStyle of the prompt to ListStyle.none.
        choice_prompt.style = ListStyle.hero_card

        dialogs.add(choice_prompt)

        step1 = await adapter.send("Hello")
        step2 = await step1.assert_reply(assert_expected_activity)
        step3 = await step2.send("1")
        await step3.assert_reply(size_choices[0])