Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(dialogId: string, userState: UserState) {
super(dialogId);
// Add control flow dialogs (first added is initial dialog)
this.add(new SequenceDialog(DELETE_ALARM_DLG, [
new CodeStep(async (dc, step) => {
// Divert to appropriate dialog
const user = userState.get(dc.context) as AlarmUser;
if (user.alarms.length > 1) {
return await dc.begin(DELETE_ALARM_MULTI_DLG);
} else if (user.alarms.length === 1) {
return await dc.begin(DELETE_ALARM_SINGLE_DLG);
} else {
await dc.context.sendActivity(`No alarms set to delete.`);
return await dc.end();
}
})
]));
this.add(new SequenceDialog(DELETE_ALARM_MULTI_DLG, [
new CodeStep('choice', async (dc, step) => {
// Compute list of choices based on alarm titles
constructor(dialogId, userState) {
super(dialogId);
// Add control flow dialogs (first added is initial dialog)
this.add(new botbuilder_dialogs_1.SequenceDialog(SHOW_ALARMS_DLG, [
new botbuilder_dialogs_1.CodeStep((dc, step) => __awaiter(this, void 0, void 0, function* () {
let msg = `No alarms found.`;
const user = userState.get(dc.context);
if (user.alarms.length > 0) {
msg = `**Current Alarms**\n\n`;
let connector = '';
user.alarms.forEach((alarm) => {
msg += connector + `- ${alarm.title} (${moment(alarm.time).format("ddd, MMM Do, h:mm a")})`;
connector = '\n';
});
}
yield dc.context.sendActivity(msg);
return yield dc.end();
}))
]));
}
}
alarm.time = time.toISOString();
// Alarm completed so set alarm.
const user = userState.get(dc.context) as AlarmUser;
user.alarms.push(alarm);
// Confirm to user
await dc.context.sendActivity(`Your alarm named "${alarm.title}" is set for "${moment(alarm.time).format("ddd, MMM Do, h:mm a")}".`);
await dc.end();
}
]);
this.dialogs.add('addAlarm', new Sequence([
new FillFieldStep('title', 'titlePrompt', `What would you like to call your alarm?`),
new FillFieldStep('time', 'timePrompt', `What time would you like to set the alarm for?`),
new CodeStep(async (dc, step) => {
// Convert to Alarm
const alarm: Alarm = {
title: step.form['title'],
time: step.form['time'].toISOString()
};
// Set alarm.
const user = userState.get(dc.context) as AlarmUser;
user.alarms.push(alarm);
// Confirm to user
await dc.context.sendActivity(`Your alarm named "${alarm.title}" is set for "${moment(alarm.time).format("ddd, MMM Do, h:mm a")}".`);
await dc.end();
})
]));
new CodeStep(async (dc, step) => {
// Divert to appropriate dialog
const user = userState.get(dc.context) as AlarmUser;
if (user.alarms.length > 1) {
return await dc.begin(DELETE_ALARM_MULTI_DLG);
} else if (user.alarms.length === 1) {
return await dc.begin(DELETE_ALARM_SINGLE_DLG);
} else {
await dc.context.sendActivity(`No alarms set to delete.`);
return await dc.end();
}
})
]));
this.add(new SequenceDialog(DELETE_ALARM_MULTI_DLG, [
new CodeStep('choice', async (dc, step) => {
// Compute list of choices based on alarm titles
const user = userState.get(dc.context) as AlarmUser;
const choices = user.alarms.map((value) => value.title);
// Prompt user for choice (force use of "list" style)
const prompt = `Which alarm would you like to delete? Say "cancel" to quit.`;
return await dc.prompt(CHOICE_PROMPT_DLG, prompt, choices);
}),
new CodeStep(async (dc, step) => {
// Delete alarm by position
const choice = step.values['choice'];
const user = userState.get(dc.context);
if (choice.index < user.alarms.length) { user.alarms.splice(choice.index, 1) }
// Notify user of delete
await dc.context.sendActivity(`Deleted "${choice.value}" alarm.`);
constructor(dialogId: string, userState: UserState) {
super(dialogId);
// Add control flow dialogs (first added is initial dialog)
this.add(new SequenceDialog(SHOW_ALARMS_DLG, [
new CodeStep(async (dc, step) => {
let msg = `No alarms found.`;
const user = userState.get(dc.context) as AlarmUser;
if (user.alarms.length > 0) {
msg = `**Current Alarms**\n\n`;
let connector = '';
user.alarms.forEach((alarm) => {
msg += connector + `- ${alarm.title} (${moment(alarm.time).format("ddd, MMM Do, h:mm a")})`;
connector = '\n';
});
}
await dc.context.sendActivity(msg);
return await dc.end();
})
]));
}
}
// Divert to appropriate dialog
const user = userState.get(dc.context);
if (user.alarms.length > 1) {
return yield dc.begin(DELETE_ALARM_MULTI_DLG);
}
else if (user.alarms.length === 1) {
return yield dc.begin(DELETE_ALARM_SINGLE_DLG);
}
else {
yield dc.context.sendActivity(`No alarms set to delete.`);
return yield dc.end();
}
}))
]));
this.add(new botbuilder_dialogs_1.SequenceDialog(DELETE_ALARM_MULTI_DLG, [
new botbuilder_dialogs_1.CodeStep('choice', (dc, step) => __awaiter(this, void 0, void 0, function* () {
// Compute list of choices based on alarm titles
const user = userState.get(dc.context);
const choices = user.alarms.map((value) => value.title);
// Prompt user for choice (force use of "list" style)
const prompt = `Which alarm would you like to delete? Say "cancel" to quit.`;
return yield dc.prompt(CHOICE_PROMPT_DLG, prompt, choices);
})),
new botbuilder_dialogs_1.CodeStep((dc, step) => __awaiter(this, void 0, void 0, function* () {
// Delete alarm by position
const choice = step.values['choice'];
const user = userState.get(dc.context);
if (choice.index < user.alarms.length) {
user.alarms.splice(choice.index, 1);
}
// Notify user of delete
yield dc.context.sendActivity(`Deleted "${choice.value}" alarm.`);
return yield dc.prompt(CHOICE_PROMPT_DLG, prompt, choices);
})),
new botbuilder_dialogs_1.CodeStep((dc, step) => __awaiter(this, void 0, void 0, function* () {
// Delete alarm by position
const choice = step.values['choice'];
const user = userState.get(dc.context);
if (choice.index < user.alarms.length) {
user.alarms.splice(choice.index, 1);
}
// Notify user of delete
yield dc.context.sendActivity(`Deleted "${choice.value}" alarm.`);
return yield dc.end();
}))
]));
this.add(new botbuilder_dialogs_1.SequenceDialog(DELETE_ALARM_SINGLE_DLG, [
new botbuilder_dialogs_1.CodeStep('confirm', (dc, step) => __awaiter(this, void 0, void 0, function* () {
const user = userState.get(dc.context);
const alarm = user.alarms[0];
return yield dc.prompt(CONFIRM_PROMPT_DLG, `Are you sure you want to delete the "${alarm.title}" alarm?`);
})),
new botbuilder_dialogs_1.CodeStep((dc, step) => __awaiter(this, void 0, void 0, function* () {
const confirm = step.values['confirm'];
if (confirm) {
const user = userState.get(dc.context);
user.alarms = [];
yield dc.context.sendActivity(`alarm deleted...`);
}
else {
yield dc.context.sendActivity(`ok...`);
}
return yield dc.end();
}))
constructor(dialogId, userState) {
super(dialogId);
// Add control flow dialogs (first added is initial dialog)
this.add(new botbuilder_dialogs_1.SequenceDialog(ADD_ALARM_DLG, [
new botbuilder_dialogs_1.PromptStep('title', TITLE_PROMPT_DLG, `What would you like to call your alarm?`),
new botbuilder_dialogs_1.PromptStep('time', TIME_PROMPT_DLG, `What time would you like to set the alarm for?`),
new botbuilder_dialogs_1.CodeStep((dc, step) => __awaiter(this, void 0, void 0, function* () {
// Convert to Alarm
const alarm = {
title: step.values['title'],
time: step.values['time'].toISOString()
};
// Set alarm.
const user = userState.get(dc.context);
user.alarms.push(alarm);
// Confirm to user
yield dc.context.sendActivity(`Your alarm named "${alarm.title}" is set for "${moment(alarm.time).format("ddd, MMM Do, h:mm a")}".`);
return yield dc.end();
}))
]));
// Add prompts
this.add(new titlePrompt_1.TitlePrompt(TITLE_PROMPT_DLG));
this.add(new timePrompt_1.TimePrompt(TIME_PROMPT_DLG));
return await dc.prompt(CHOICE_PROMPT_DLG, prompt, choices);
}),
new CodeStep(async (dc, step) => {
// Delete alarm by position
const choice = step.values['choice'];
const user = userState.get(dc.context);
if (choice.index < user.alarms.length) { user.alarms.splice(choice.index, 1) }
// Notify user of delete
await dc.context.sendActivity(`Deleted "${choice.value}" alarm.`);
return await dc.end();
})
]));
this.add(new SequenceDialog(DELETE_ALARM_SINGLE_DLG, [
new CodeStep('confirm', async (dc, step) => {
const user = userState.get(dc.context) as AlarmUser;
const alarm = user.alarms[0];
return await dc.prompt(CONFIRM_PROMPT_DLG, `Are you sure you want to delete the "${alarm.title}" alarm?`);
}),
new CodeStep(async (dc, step) => {
const confirm = step.values['confirm'];
if (confirm) {
const user = userState.get(dc.context) as AlarmUser;
user.alarms = [];
await dc.context.sendActivity(`alarm deleted...`);
} else {
await dc.context.sendActivity(`ok...`);
}
return await dc.end();
})
]));
constructor(dialogId: string, userState: UserState) {
super(dialogId);
// Add control flow dialogs (first added is initial dialog)
this.add(new SequenceDialog(ADD_ALARM_DLG, [
new PromptStep('title', TITLE_PROMPT_DLG, `What would you like to call your alarm?`),
new PromptStep('time', TIME_PROMPT_DLG, `What time would you like to set the alarm for?`),
new CodeStep(async (dc, step) => {
// Convert to Alarm
const alarm: Alarm = {
title: step.values['title'],
time: step.values['time'].toISOString()
};
// Set alarm.
const user = userState.get(dc.context) as AlarmUser;
user.alarms.push(alarm);
// Confirm to user
await dc.context.sendActivity(`Your alarm named "${alarm.title}" is set for "${moment(alarm.time).format("ddd, MMM Do, h:mm a")}".`);
return await dc.end();
})
]));