async def _choose_person( self, step_context: WaterfallStepContext) -> DialogTurnResult: user_profile: UserProfile = await self.user_profile_accessor.get( step_context.context, UserProfile) user = user_dao.get_user_by_id(user_profile.id) if not user: await step_context.context.send_activity( "Devi aggiungere almeno una persona nel Database per inizializzare il tuo account." ) return await step_context.end_dialog() step_context.values['person_group_id'] = user.person_group_id people = people_dao.retrive_people(user.person_group_id) if not people: await step_context.context.send_activity( "Non è presente alcuna persona nel Database.") return await step_context.end_dialog() people_list = [ Choice(f'{person.id}) {person.name} {person.surname}') for person in people ] return await step_context.prompt( ChoicePrompt.__name__, PromptOptions(prompt=MessageFactory.text( 'Seleziona il profilo della persona da eliminare. '), retry_prompt=MessageFactory.text( "Per favore seleziona un'opzione dalla lista."), style=ListStyle.hero_card, choices=people_list))
async def confirm_step( self, step_context: WaterfallStepContext) -> DialogTurnResult: result = step_context.result user = step_context.values["user"] book_to_remove = None for book in user.wishlist: if book.name.replace(",", "").lower() == result.replace(",", "").lower(): book_to_remove = book break step_context.values["book_to_remove"] = book_to_remove if book_to_remove is not None: message_text = "Sei sicuro di voler cancellare {}?".format( book_to_remove.name) prompt_message = MessageFactory.text(message_text, message_text, InputHints.expecting_input) return await step_context.prompt( ConfirmPrompt.__name__, PromptOptions( prompt=prompt_message, retry_prompt=MessageFactory.text( '''Sei sicuro di voler cancellare? Scrivi yes o no''')) ) return await step_context.end_dialog()
async def _select_picture( self, step_context: WaterfallStepContext) -> DialogTurnResult: if step_context.result: if 'image' not in step_context.result[ 0].content_type: # check if there is an image await step_context.context.send_activity( "Tipo di file non valido. Cancello tutte le operazioni in corso." ) return await step_context.cancel_all_dialogs() step_context.values['image_url'] = step_context.result[ 0].content_url return await step_context.prompt( ChoicePrompt.__name__, PromptOptions(prompt=MessageFactory.text( "Vuoi aggiungere un nuovo utente, o aggiornarne uno già presente?" ), retry_prompt=MessageFactory.text( "Per favore seleziona un'opzione dalla lista." ), choices=[Choice('Nuovo'), Choice('Esistente')])) await step_context.context.send_activity('Operazione annullata.') return await step_context.cancel_all_dialogs()
async def select_third(self, step_context: WaterfallStepContext) -> DialogTurnResult: result = step_context.result selected = step_context.values["selected"] categories = step_context.values["categories"] for i,c in enumerate(categories): if result.lower()==c.name.lower(): selected.append(c) del categories[i] step_context.values["selected"] = selected step_context.values["categories"] = categories break card=self.create_card(step_context) message_text = ("Hai selezionato correttamente la categoria {}".format(result)) message = MessageFactory.text(message_text, message_text, InputHints.ignoring_input) await step_context.context.send_activity(message) return await step_context.prompt( TextPrompt.__name__, PromptOptions( prompt = MessageFactory.attachment(card), retry_prompt= MessageFactory.text("Inserisci una categoria valida.") ) )
async def showBooks_step( self, step_context: WaterfallStepContext) -> DialogTurnResult: iduser = step_context.context.activity.from_property.id genres_user = [] user_with_info = DatabaseManager.find_user_info(iduser) genres_user = user_with_info.categories index = random.randint(0, len(genres_user) - 1) category = genres_user[index] code = cat_and_code[category.name] books = SuggestBooksDialog.call_amazon(code) card = SuggestBooksDialog.create_card(books, category.name) step_context.values["books"] = books dic[iduser] = books await step_context.context.send_activity(card) message_text = "Vuoi aggiungere un libro alla tua wishlist?" prompt_message = MessageFactory.text(message_text, message_text, InputHints.expecting_input) return await step_context.prompt( ConfirmPrompt.__name__, PromptOptions(prompt=prompt_message, retry_prompt=MessageFactory.text( '''Vuoi aggiungere un libro alla tua wishlist? Scrivi yes o no''')))
async def _select_from_db( self, step_context: WaterfallStepContext) -> DialogTurnResult: step_context.values.update(step_context.options) user_info = await self.user_profile_accessor.get( step_context.context, UserProfile) try: user = user_dao.get_user_by_id(user_info.id) people = people_dao.retrive_people(user.person_group_id) people_list = [ Choice(f'{person.id}) {person.name} {person.surname}') for person in people ] if not people_list: raise Exception('No people in Database') except: await step_context.context.send_activity( 'Non sono riuscito ad ottenere le informazioni. ' 'Assicurati di aver aggiunto almeno una persona nel Database.') return await step_context.cancel_all_dialogs() return await step_context.prompt( ChoicePrompt.__name__, PromptOptions(prompt=MessageFactory.text( 'Seleziona la persona a cui associare la foto. '), retry_prompt=MessageFactory.text( "Per favore seleziona un'opzione dalla lista."), style=ListStyle.hero_card, choices=people_list))
async def login_step(self, step_context: WaterfallStepContext): # Get the token from the previous step. token_response = step_context.result if token_response: # Workaround, step_context.result value using DirectLine returns a 'dict' instead of TokenResponse if isinstance(token_response, dict): step_context.values["token"] = token_response.get("token") else: step_context.values["token"] = token_response.token # Show the token logged_in_message = "You are now logged in." await step_context.context.send_activity( MessageFactory.text(logged_in_message, logged_in_message, InputHints.ignoring_input)) options = PromptOptions(prompt=MessageFactory.text( "Would you like to view your token?")) return await step_context.prompt(ConfirmPrompt.__name__, options) try_again_message = "Login was not successful please try again." await step_context.context.send_activity( MessageFactory.text(try_again_message, try_again_message, InputHints.ignoring_input)) return await step_context.replace_dialog(self.initial_dialog_id)
async def prompt_for_name(self, step_context: WaterfallStepContext): return await step_context.prompt( "TextPrompt", PromptOptions( prompt=MessageFactory.text("Hello, what is your name?"), retry_prompt=MessageFactory.text("Hello, what is your name again?"), ), )
async def _insert_name( self, step_context: WaterfallStepContext) -> DialogTurnResult: step_context.values.update(step_context.options) return await step_context.prompt( TextPrompt.__name__, PromptOptions( prompt=MessageFactory.text('Per favore inserisci il nome'), ))
async def step_2(step_context: WaterfallStepContext) -> None: assert isinstance(step_context.active_dialog.state["stepIndex"], int) await step_context.prompt( TextPrompt.__name__, PromptOptions( prompt=MessageFactory.text("Please type your name")), )
async def admission_number_step(step_context: WaterfallStepContext) -> DialogTurnResult: step_context.values[StudentProfileAttributes.NAME.value] = step_context.result await step_context.context.send_activity(MessageFactory.text(f'Hello {step_context.result}')) return await step_context.prompt( TextPrompt.__name__, PromptOptions(prompt=MessageFactory.text("Please enter your admission number?")) )
async def __step2(self, step_context: WaterfallStepContext) -> DialogTurnResult: first: int = step_context.result step_context.values["first"] = first return await step_context.prompt( "number", PromptOptions(prompt=MessageFactory.text( f"I have {first}, now enter another number")), )
async def _get_pic(self, step_context: WaterfallStepContext) -> DialogTurnResult: return await step_context.prompt( AttachmentPrompt.__name__, PromptOptions( prompt=MessageFactory.text( "Per favore inviami l'immagine che devo testare"), retry_prompt=MessageFactory.text( 'Devi inviarmi una foto. Per cancellare l\'operazione digita "cancel".' )))
async def _init_step( self, step_context: WaterfallStepContext) -> DialogTurnResult: return await step_context.prompt( AttachmentPrompt.__name__, PromptOptions( prompt=MessageFactory.text( 'Inviami la foto della persona che vuoi aggiungere.'), retry_prompt=MessageFactory.text( 'Devi inviarmi una foto. Per cancellare l\'operazione digita "cancel".' ), validations=self._validate_picture))
async def _insert_surname( self, step_context: WaterfallStepContext) -> DialogTurnResult: if step_context.result: step_context.values['name'] = step_context.result return await step_context.prompt( TextPrompt.__name__, PromptOptions(prompt=MessageFactory.text( 'Per favore inserisci il cognome'), )) await step_context.context.send_activity("Operazione cancellata.") return await step_context.cancel_all_dialogs()
async def login_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: # Get the token from the previous step. Note that we could also have gotten the # token directly from the prompt itself. There is an example of this in the next method. if step_context.result: await step_context.context.send_activity("You are now logged in.") return await step_context.prompt(ConfirmPrompt.__name__, PromptOptions( prompt=MessageFactory.text("Would you like to view your token?") )) await step_context.context.send_activity("Login was not successful please try again.") return await step_context.end_dialog()
def __init__(self, name: str, dialog_id: str, options: PromptOptions = None, prompt: str = None, retry_prompt: str = None): self.name = name self.dialog_id = dialog_id self.options = options if options else PromptOptions( prompt=MessageFactory.text(prompt), retry_prompt=None if retry_prompt is None else MessageFactory.text(retry_prompt))
async def help_step(self, step_context: WaterfallStepContext): actions = SuggestedActions(actions=[ CardAction(title="Key", type=ActionTypes.im_back, value="key"), CardAction(title="ID", type=ActionTypes.im_back, value="id"), ]) response = activity_helper.create_activity_reply( step_context.context.activity, "To use this bot simply tell me the ID or any keyword and I'll try to find it", '', actions) return await step_context.prompt(TextPrompt.__name__, PromptOptions(prompt=response))
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)
async def __step3(self, step_context: WaterfallStepContext) -> DialogTurnResult: first: int = step_context.values["first"] second: int = step_context.result await step_context.prompt( "number", PromptOptions(prompt=MessageFactory.text( f"The result of the first minus the second is {first - second}." )), ) return await step_context.end_dialog()
async def callback_handler(turn_context: TurnContext): dc = await dialogs.create_context(turn_context) results = await dc.continue_dialog() if (results.status == DialogTurnStatus.Empty): await dc.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)
async def select_first(self, step_context: WaterfallStepContext) -> DialogTurnResult: step_context.values["categories"] = CATEGORIES step_context.values["selected"] = [] card=self.create_card(step_context) return await step_context.prompt( TextPrompt.__name__, PromptOptions( prompt = MessageFactory.attachment(card), retry_prompt= MessageFactory.text("Inserisci una categoria valida.") ) )
async def options_step( self, step_context: WaterfallStepContext ) -> DialogTurnResult: card = self._create_adaptive_card_attachment() response = Dialog.create_activity_reply( step_context.context.activity, "", "", [card] ) return await step_context.prompt( TextPrompt.__name__, PromptOptions(prompt=response) )
async def confirm_step( self, step_context: WaterfallStepContext) -> DialogTurnResult: result = step_context.result message_text = "Inserisci il titolo del libro che vuoi aggiungere alla wishlist" prompt_message = MessageFactory.text(message_text, message_text, InputHints.expecting_input) if result: return await step_context.prompt( "TextPromptTitle", PromptOptions(prompt=prompt_message, retry_prompt=MessageFactory.text( '''Inserisci un titolo valido'''))) else: return await step_context.end_dialog()
async def first_step( self, step_context: WaterfallStepContext) -> DialogTurnResult: iduser = step_context.context.activity.from_property.id user = DatabaseManager.find_user_info(iduser) step_context.values["user"] = user dic[iduser] = user.wishlist card, flag = self.create_wishlist_card(user.wishlist) await step_context.context.send_activity(card) if flag: message_text = "Puoi cancellare un libro dalla tua wishlist oppure tornare al menu principale. Cosa desideri fare?" prompt_message = MessageFactory.text(message_text, message_text, InputHints.expecting_input) return await step_context.prompt( TextPrompt.__name__, PromptOptions(prompt=prompt_message)) return await step_context.end_dialog()
async def choose_cloud_step( self, step_context: WaterfallStepContext) -> DialogTurnResult: choices = [] if self.azure_dialog: choices.append(Choice(Cloud.azure.value)) if self.gcp_dialog: choices.append(Choice(Cloud.gcp.value)) return await step_context.prompt( ChoicePrompt.__name__, PromptOptions( prompt=MessageFactory.text( "Please choose the cloud you want me to have a look at"), choices=choices, ))
async def prompt_action_step(self, step_context: WaterfallStepContext): message_text = "What SSO action would you like to perform on the skill?" reprompt_message_text = ( "That was not a valid choice, please select a valid choice.") options = PromptOptions( prompt=MessageFactory.text(message_text, message_text, InputHints.expecting_input), retry_prompt=MessageFactory.text(reprompt_message_text, reprompt_message_text, InputHints.expecting_input), choices=await self.get_prompt_choices(step_context), ) return await step_context.prompt(ChoicePrompt.__name__, options)
async def handle_update_dialog_step(self, step_context: WaterfallStepContext): channel = step_context.context.activity.channel_id if channel in self._update_supported: if step_context.context.activity.conversation.id in self._update_tracker: conversation_id = step_context.context.activity.conversation.id tracked_tuple = self._update_tracker[conversation_id] activity = MessageFactory.text( f"This message has been updated {tracked_tuple.item2} time(s)." ) tracked_tuple.item2 += 1 activity.id = tracked_tuple.item1 self._update_tracker[conversation_id] = tracked_tuple await step_context.context.update_activity(activity) else: response = await step_context.context.send_activity( MessageFactory.text("Here is the original activity.")) self._update_tracker[ step_context.context.activity.conversation.id] = ( response.id, 1, ) else: await step_context.context.send_activity( MessageFactory.text( f"Delete is not supported in the {channel} channel.")) await step_context.context.send_activity( Activity( type=ActivityTypes.end_of_conversation, code=EndOfConversationCodes.completed_successfully, )) return DialogTurnResult(DialogTurnStatus.Complete) # Ask if we want to update the activity again. message_text = "Do you want to update the activity again?" reprompt_message_text = "Please select a valid answer" options = PromptOptions( prompt=MessageFactory.text(message_text, message_text), retry_prompt=MessageFactory.text(reprompt_message_text, reprompt_message_text), ) return await step_context.prompt(ConfirmPrompt.__name__, options)
async def picture_step(step_context: WaterfallStepContext) -> DialogTurnResult: step_context.values[StudentProfileAttributes.ADMISSION_NUMBER.value] = step_context.result name = step_context.values[StudentProfileAttributes.NAME.value] msg = f'Hello {name}, thanks for providing us with admission number: {step_context.result}' await step_context.context.send_activity(MessageFactory.text(msg)) prompt_options = PromptOptions( prompt=MessageFactory.text( "Please attach a profile picture. " ), retry_prompt=MessageFactory.text( "The attachment must be a jpeg/png image file. Please attach again" ), ) return await step_context.prompt(AttachmentPrompt.__name__, prompt_options)
async def _confirm_name_surname( self, step_context: WaterfallStepContext) -> DialogTurnResult: if step_context.result: step_context.values['surname'] = step_context.result return await step_context.prompt( ChoicePrompt.__name__, PromptOptions(prompt=MessageFactory.text( f'Desideri inserire {step_context.values["name"]} {step_context.values["surname"]} nel database?' ), retry_prompt=MessageFactory.text( "Per favore seleziona un'opzione dalla lista." ), choices=[Choice('Si'), Choice('No')])) await step_context.context.send_activity("Operazione cancellata.") return await step_context.cancel_all_dialogs()