class BotDialog(ActivityHandler): def __init__(self, conversation: ConversationState): self.con_statea = conversation self.state_prop = self.con_statea.create_property("dialog_set") self.dialog_set = DialogSet(self.state_prop) self.dialog_set.add(EmailPrompt("email_prompt")) self.dialog_set.add( WaterfallDialog("main_dialog", [self.FindEmailPrompt, self.Completed])) async def FindEmailPrompt(self, waterfall_step: WaterfallStepContext): return await waterfall_step.prompt( "email_prompt", PromptOptions( prompt=MessageFactory.text("Please enter email with Text"))) async def Completed(self, waterfall_step: WaterfallStepContext): email = waterfall_step.result await waterfall_step._turn_context.send_activity(email) return await waterfall_step.end_dialog() async def on_turn(self, turn_context: TurnContext): dialog_context = await self.dialog_set.create_context(turn_context) if (dialog_context.active_dialog is not None): await dialog_context.continue_dialog() else: await dialog_context.begin_dialog("main_dialog") await self.con_statea.save_changes(turn_context)
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")
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/")
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_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)
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_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_should_call_ChoicePrompt_using_dc_prompt(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('ChoicePrompt', 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) # 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) choice_prompt = ChoicePrompt('ChoicePrompt') 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(_answer_message) await step3.assert_reply('red')
async def test_should_not_recognize_if_choices_are_not_passed_in(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=None ) 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) choice_prompt = ChoicePrompt('prompt') choice_prompt.style = ListStyle.none dialogs.add(choice_prompt) step1 = await adapter.send('Hello') step2 = await step1.assert_reply('Please choose a color.')
async def test_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 phone number type api")) await dialog_context.prompt("phoneprompt", 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(PhoneNumberPrompt("phoneprompt")) step1 = await adapter.test('Hello', 'test the phone number type api') step2 = await step1.send('My phone number is 1 (877) 609-2233') await step2.assert_reply("1 (877) 609-2233")
async def test_should_not_send_retry_if_not_specified(self): async def exec_test(turn_context: TurnContext): dc = await dialogs.create_context(turn_context) results = await dc.continue_dialog() if results.status == DialogTurnStatus.Empty: await dc.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')
def __init__(self,conversation:ConversationState): self.con_statea = conversation self.state_prop = self.con_statea.create_property("dialog_set") self.dialog_set = DialogSet(self.state_prop) self.dialog_set.add(TextPrompt("text_prompt")) self.dialog_set.add(NumberPrompt("number_prompt")) self.dialog_set.add(WaterfallDialog("main_dialog",[self.GetUserName,self.GetMobileNumber,self.GetEmailId,self.Completed]))
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_percentage_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 percentage type api" ) ) await dialog_context.prompt("percentagePrompt", 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(NumberWithTypePrompt("percentagePrompt", NumberWithTypePromptType.Percentage)) step1 = await adapter.test('percentagePrompt', 'test the percentage type api') step2 = await step1.send('two hundred percents') await step2.assert_reply("200%")
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")
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_retry_activity_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="please send an event.")) await dialog_context.prompt("EventActivityPrompt", options) elif results.status == DialogTurnStatus.Complete: await turn_context.send_activity(results.result) 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(SimpleActivityPrompt("EventActivityPrompt", validator)) event_activity = Activity(type=ActivityTypes.event, value=2) step1 = await adapter.send("hello") step2 = await step1.assert_reply("please send an event.") step3 = await step2.send("hello again") step4 = await step3.assert_reply( "Please send an 'event'-type Activity with a value of 2.") step5 = await step4.send(event_activity) await step5.assert_reply("2")
async def test_should_create_prompt_with_inline_choices_when_specified(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) choice_prompt = ChoicePrompt('prompt') choice_prompt.style = ListStyle.in_line 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(_answer_message) await step3.assert_reply('red')
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_should_create_prompt_with_list_choices_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, ) 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") choice_prompt.style = ListStyle.list_style dialogs.add(choice_prompt) step1 = await adapter.send("Hello") step2 = await step1.assert_reply( "Please choose a color.\n\n 1. red\n 2. green\n 3. blue") step3 = await step2.send(_answer_message) await step3.assert_reply("red")
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")
async def run_dialog(dialog: Dialog, turn_context: TurnContext, accessor: StatePropertyAccessor): dialog_set = DialogSet(accessor) dialog_set.add(dialog) dialog_context = await dialog_set.create_context(turn_context) results = await dialog_context.continue_dialog() if results.status == DialogTurnStatus.Empty: await dialog_context.begin_dialog(dialog.id)
def __init__(self, conversation: ConversationState): self.con_statea = conversation self.state_prop = self.con_statea.create_property('dialog_set') self.dialog_set = DialogSet(self.state_prop) self.dialog_set.add(TextPrompt('text_prompt')) self.dialog_set.add(NumberPrompt('number_prompt', self.isValidNumber)) self.dialog_set.add(WaterfallDialog('main_dialog',[self.GetUserName,self.GetUserNumber,self.GetUserEmailId, self.GetUserIntention,self.Completed]))
def __init__(self, conversation: ConversationState): self.con_statea = conversation self.state_prop = self.con_statea.create_property("dialog_set") self.dialog_set = DialogSet(self.state_prop) self.dialog_set.add(EmailPrompt("email_prompt")) self.dialog_set.add( WaterfallDialog("main_dialog", [self.FindEmailPrompt, self.Completed]))
def __init__(self, conversation: ConversationState): self.con_statea = conversation self.state_prop = self.con_statea.create_property("dialog_set") self.dialog_set = DialogSet(self.state_prop) self.dialog_set.add(ChoicePrompt(ChoicePrompt.__name__)) self.dialog_set.add( WaterfallDialog("main_dialog", [self.DisplayChoiceList, self.ReadResult]))
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.")
async def default_callback(turn_context: TurnContext) -> None: dialog_set = DialogSet(dialog_state) dialog_set.add(target_dialog) dialog_context = await dialog_set.create_context(turn_context) self.dialog_turn_result = await dialog_context.continue_dialog() if self.dialog_turn_result.status == DialogTurnStatus.Empty: self.dialog_turn_result = await dialog_context.begin_dialog( target_dialog.id, initial_dialog_options)
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")
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
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'.")