Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createBotLogic(conversationState) {
const dialogs = new DialogSet();
// Create prompt for name with string length validation
dialogs.add('namePrompt', new TextPrompt(async (context, value) => {
if (value && value.length < 2) {
await context.sendActivity('Your name should be at least 2 characters long.');
return undefined;
}
return value.trim();
}));
// Create prompt for age with number value validation
dialogs.add('agePrompt', new NumberPrompt(async (context, value) => {
if (0 > value || value > 122) {
await context.sendActivity('Your age should be between 0 and 122.');
return undefined;
}
return value;
}));
// Add a dialog that uses both prompts to gather information from the user
dialogs.add('gatherInfo', [
async (dialogContext) => {
await dialogContext.prompt('namePrompt', 'What is your name?');
},
async (dialogContext, value) => {
const state = conversationState.get(dialogContext.context);
state.name = value;
await dialogContext.prompt('agePrompt', 'What is your age?');
// Link the questions together into a parent group that contains references
// to both the fullname and address questions defined above.
const slots = [
new SlotDetails('fullname', 'fullname'),
new SlotDetails('age', 'number', 'Please enter your age.'),
new SlotDetails('shoesize', 'shoesize', 'Please enter your shoe size.', 'You must enter a size between 0 and 16. Half sizes are acceptable.'),
new SlotDetails('address', 'address')
];
// Add the individual child dialogs and prompts used.
// Note that the built-in prompts work hand-in-hand with our custom SlotFillingDialog class
// because they are both based on the provided Dialog class.
this.addDialog(new SlotFillingDialog('address', addressSlots));
this.addDialog(new SlotFillingDialog('fullname', fullnameSlots));
this.addDialog(new TextPrompt('text'));
this.addDialog(new NumberPrompt('number'));
this.addDialog(new NumberPrompt('shoesize', this.shoeSizeValidator));
this.addDialog(new SlotFillingDialog('slot-dialog', slots));
// Finally, add a 2-step WaterfallDialog that will initiate the SlotFillingDialog,
// and then collect and display the results.
this.addDialog(new WaterfallDialog('root', [
this.startDialog.bind(this),
this.processResults.bind(this)
]));
this.initialDialogId = 'root';
}
// Link the questions together into a parent group that contains references
// to both the fullname and address questions defined above.
const slots = [
new SlotDetails('fullname', 'fullname'),
new SlotDetails('age', 'number', 'Please enter your age.'),
new SlotDetails('shoesize', 'shoesize', 'Please enter your shoe size.', 'You must enter a size between 0 and 16. Half sizes are acceptable.'),
new SlotDetails('address', 'address')
];
// Add the individual child dialogs and prompts used.
// Note that the built-in prompts work hand-in-hand with our custom SlotFillingDialog class
// because they are both based on the provided Dialog class.
this.dialogs.add(new SlotFillingDialog('address', addressSlots));
this.dialogs.add(new SlotFillingDialog('fullname', fullnameSlots));
this.dialogs.add(new TextPrompt('text'));
this.dialogs.add(new NumberPrompt('number'));
this.dialogs.add(new NumberPrompt('shoesize', this.shoeSizeValidator));
this.dialogs.add(new SlotFillingDialog('slot-dialog', slots));
// Finally, add a 2-step WaterfallDialog that will initiate the SlotFillingDialog,
// and then collect and display the results.
this.dialogs.add(new WaterfallDialog('root', [
this.startDialog.bind(this),
this.processResults.bind(this)
]));
}
// to both the fullname and address questions defined above.
const slots = [
new SlotDetails('fullname', 'fullname'),
new SlotDetails('age', 'number', 'Please enter your age.'),
new SlotDetails('shoesize', 'shoesize', 'Please enter your shoe size.', 'You must enter a size between 0 and 16. Half sizes are acceptable.'),
new SlotDetails('address', 'address')
];
// Add the individual child dialogs and prompts used.
// Note that the built-in prompts work hand-in-hand with our custom SlotFillingDialog class
// because they are both based on the provided Dialog class.
this.dialogs.add(new SlotFillingDialog('address', addressSlots));
this.dialogs.add(new SlotFillingDialog('fullname', fullnameSlots));
this.dialogs.add(new TextPrompt('text'));
this.dialogs.add(new NumberPrompt('number'));
this.dialogs.add(new NumberPrompt('shoesize', this.shoeSizeValidator));
this.dialogs.add(new SlotFillingDialog('slot-dialog', slots));
// Finally, add a 2-step WaterfallDialog that will initiate the SlotFillingDialog,
// and then collect and display the results.
this.dialogs.add(new WaterfallDialog('root', [
this.startDialog.bind(this),
this.processResults.bind(this)
]));
}
// Show menu if no response sent
if (!context.responded) {
await dc.beginDialog('mainMenu');
}
}
});
});
const dialogs = new DialogSet();
// Add prompts
dialogs.add('choicePrompt', new ChoicePrompt());
dialogs.add('confirmPrompt', new ConfirmPrompt());
dialogs.add('datetimePrompt', new DatetimePrompt());
dialogs.add('numberPrompt', new NumberPrompt());
dialogs.add('textPrompt', new TextPrompt());
dialogs.add('attachmentPrompt', new AttachmentPrompt());
//-----------------------------------------------
// Main Menu
//-----------------------------------------------
dialogs.add('mainMenu', [
async function(dc) {
function choice(title: string, value: string): Choice {
return {
value: value,
action: { type: ActionTypes.ImBack, title: title, value: title }
};
}
constructor(conversationState) {
// Creates our state accessor properties.
// See https://aka.ms/about-bot-state-accessors to learn more about the bot state and state accessors.
this.dialogStateAccessor = conversationState.createProperty(DIALOG_STATE_ACCESSOR);
this.reservationAccessor = conversationState.createProperty(RESERVATION_ACCESSOR);
this.conversationState = conversationState;
// Create the dialog set and add the prompts, including custom validation.
this.dialogSet = new DialogSet(this.dialogStateAccessor);
this.dialogSet.add(new NumberPrompt(SIZE_RANGE_PROMPT, this.rangeValidator));
this.dialogSet.add(new ChoicePrompt(LOCATION_PROMPT));
this.dialogSet.add(new DateTimePrompt(RESERVATION_DATE_PROMPT, this.dateValidator));
// Define the steps of the waterfall dialog and add it to the set.
this.dialogSet.add(new WaterfallDialog(RESERVATION_DIALOG, [
this.promptForPartySize.bind(this),
this.promptForLocation.bind(this),
this.promptForReservationDate.bind(this),
this.acknowledgeReservation.bind(this),
]));
}
// Add control flow dialogs
this.addDialog(
new WaterfallDialog(TABLE_BOOK_DIALOG, [
this.welcomeStep.bind(this),
this.locationStep.bind(this),
this.patySizePrompt.bind(this),
this.timePrompt.bind(this),
this.confirmPrompt.bind(this),
this.confirmReadoutPrompt.bind(this)
])
);
this.addDialog(new TextPrompt(WELCOME_PROMPT));
this.addDialog(new TextPrompt(LOCATION_PROMPT));
this.addDialog(new NumberPrompt(PARTY_SIZE_PROMPT));
this.addDialog(new TextPrompt(TIME_PROMPT));
this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
this.addDialog(new TextPrompt(BOOKING_CONFIRMATION_READOUT_PROMPT));
// Save off our state accessor for later use
this.userProfileStateAccessor = userProfileStateAccessor;
this.lgEntitiesStateAccessor = lgEntitiesStateAccessor;
}
// Link the questions together into a parent group that contains references
// to both the fullname and address questions defined above.
const slots = [
new SlotDetails('fullname', 'fullname'),
new SlotDetails('age', 'number', 'Please enter your age.'),
new SlotDetails('shoesize', 'shoesize', 'Please enter your shoe size.', 'You must enter a size between 0 and 16. Half sizes are acceptable.'),
new SlotDetails('address', 'address')
];
// Add the individual child dialogs and prompts used.
// Note that the built-in prompts work hand-in-hand with our custom SlotFillingDialog class
// because they are both based on the provided Dialog class.
this.dialogs.add(new SlotFillingDialog('address', addressSlots));
this.dialogs.add(new SlotFillingDialog('fullname', fullnameSlots));
this.dialogs.add(new TextPrompt('text'));
this.dialogs.add(new NumberPrompt('number'));
this.dialogs.add(new NumberPrompt('shoesize', this.shoeSizeValidator));
this.dialogs.add(new SlotFillingDialog('slot-dialog', slots));
// Finally, add a 2-step WaterfallDialog that will initiate the SlotFillingDialog,
// and then collect and display the results.
this.dialogs.add(new WaterfallDialog('root', [
this.startDialog.bind(this),
this.processResults.bind(this)
]));
}
constructor(userState) {
super('userProfileDialog');
this.userProfile = userState.createProperty(USER_PROFILE);
this.addDialog(new TextPrompt(NAME_PROMPT));
this.addDialog(new ChoicePrompt(CHOICE_PROMPT));
this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
this.addDialog(new NumberPrompt(NUMBER_PROMPT, this.agePromptValidator));
this.addDialog(new AttachmentPrompt(ATTACHMENT_PROMPT, this.picturePromptValidator));
this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
this.transportStep.bind(this),
this.nameStep.bind(this),
this.nameConfirmStep.bind(this),
this.ageStep.bind(this),
this.pictureStep.bind(this),
this.confirmStep.bind(this),
this.summaryStep.bind(this)
]));
this.initialDialogId = WATERFALL_DIALOG;
}
constructor(userState) {
super('userProfileDialog');
this.userProfile = userState.createProperty(USER_PROFILE);
this.addDialog(new TextPrompt(NAME_PROMPT));
this.addDialog(new ChoicePrompt(CHOICE_PROMPT));
this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
this.addDialog(new NumberPrompt(NUMBER_PROMPT, this.agePromptValidator));
this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
this.transportStep.bind(this),
this.nameStep.bind(this),
this.nameConfirmStep.bind(this),
this.ageStep.bind(this),
this.confirmStep.bind(this),
this.summaryStep.bind(this)
]));
this.initialDialogId = WATERFALL_DIALOG;
this.lgTemplates = Templates.parseFile('./Resources/UserProfileDialog.lg');
}