|
| 1 | +import { Attachment, CardFactory } from 'botbuilder'; |
| 2 | +import { |
| 3 | + ComponentDialog, |
| 4 | + DialogTurnResult, |
| 5 | + OAuthPrompt, |
| 6 | + PromptValidatorContext, |
| 7 | + TextPrompt, |
| 8 | + WaterfallDialog, |
| 9 | + WaterfallStepContext |
| 10 | +} from 'botbuilder-dialogs'; |
| 11 | +import { GraphHelper } from '../helpers/graphHelper'; |
| 12 | +import GenericCard from '../resources/generic.json'; |
| 13 | + |
| 14 | +const TEXT_PROMPT = 'textPrompt'; |
| 15 | +const WATERFALL_DIALOG = 'waterfallDialog'; |
| 16 | +const OAUTH_PROMPT = 'OAuthPrompt'; |
| 17 | + |
| 18 | +export class AliasResolverDialog extends ComponentDialog { |
| 19 | + private static tokenResponse: any; |
| 20 | + |
| 21 | + private static async aliasPromptValidator(promptContext: PromptValidatorContext<string>): Promise<boolean> { |
| 22 | + if (promptContext.recognized.succeeded) { |
| 23 | + |
| 24 | + const alias: string = promptContext.recognized.value; |
| 25 | + |
| 26 | + if (await GraphHelper.aliasExistsPnP(alias, AliasResolverDialog.tokenResponse)) { |
| 27 | + promptContext.context.sendActivity('Alias already exist.'); |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + return true; |
| 32 | + |
| 33 | + } else { |
| 34 | + return false; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + constructor(id: string) { |
| 39 | + super(id || 'ownerResolverDialog'); |
| 40 | + |
| 41 | + this |
| 42 | + .addDialog(new TextPrompt(TEXT_PROMPT, AliasResolverDialog.aliasPromptValidator.bind(this))) |
| 43 | + .addDialog(new OAuthPrompt(OAUTH_PROMPT, { |
| 44 | + connectionName: process.env.connectionName, |
| 45 | + text: 'Please Sign In', |
| 46 | + timeout: 300000, |
| 47 | + title: 'Sign In' |
| 48 | + })) |
| 49 | + .addDialog(new WaterfallDialog(WATERFALL_DIALOG, [ |
| 50 | + this.promptStep.bind(this), |
| 51 | + this.initialStep.bind(this), |
| 52 | + this.finalStep.bind(this) |
| 53 | + ])); |
| 54 | + |
| 55 | + this.initialDialogId = WATERFALL_DIALOG; |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Prompt step in the waterfall. |
| 61 | + */ |
| 62 | + private async promptStep(stepContext: WaterfallStepContext): Promise<DialogTurnResult> { |
| 63 | + return await stepContext.beginDialog(OAUTH_PROMPT); |
| 64 | + } |
| 65 | + |
| 66 | + private async initialStep(stepContext: WaterfallStepContext): Promise<DialogTurnResult> { |
| 67 | + const tokenResponse = stepContext.result; |
| 68 | + if (tokenResponse && tokenResponse.token) { |
| 69 | + |
| 70 | + AliasResolverDialog.tokenResponse = tokenResponse; |
| 71 | + |
| 72 | + const siteDetails = (stepContext.options as any).siteDetails; |
| 73 | + const promptMsg = `Provide an alias for your ${siteDetails.siteType} site`; |
| 74 | + |
| 75 | + if (!siteDetails.alias) { |
| 76 | + |
| 77 | + const aliasCard: Attachment = CardFactory.adaptiveCard(JSON.parse( |
| 78 | + JSON.stringify(GenericCard).replace('$Placeholder', promptMsg))); |
| 79 | + |
| 80 | + await stepContext.context.sendActivity({ attachments: [aliasCard] }); |
| 81 | + return await stepContext.prompt(TEXT_PROMPT, |
| 82 | + { |
| 83 | + prompt: '' |
| 84 | + }); |
| 85 | + } else { |
| 86 | + return await stepContext.next(siteDetails.alias); |
| 87 | + } |
| 88 | + } |
| 89 | + await stepContext.context.sendActivity('Login was not successful please try again.'); |
| 90 | + return await stepContext.endDialog(); |
| 91 | + } |
| 92 | + |
| 93 | + private async finalStep(stepContext: WaterfallStepContext): Promise<DialogTurnResult> { |
| 94 | + const owner = stepContext.result; |
| 95 | + return await stepContext.endDialog(owner); |
| 96 | + } |
| 97 | +} |
0 commit comments