Skip to content

Commit 031dd07

Browse files
author
Ågren
committed
Additions
1 parent 8dc373c commit 031dd07

13 files changed

+554
-48
lines changed

package-lock.json

+21-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bots/bot.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
BotState,
3+
CardFactory,
34
ConversationState,
45
SigninStateVerificationQuery,
56
StatePropertyAccessor,
@@ -9,6 +10,7 @@ import {
910
} from 'botbuilder';
1011
import { Dialog, DialogState } from 'botbuilder-dialogs';
1112
import { MainDialog } from '../dialogs/mainDialog';
13+
import WelcomeCard from '../resources/welcome.json';
1214

1315
export class SimonBot extends TeamsActivityHandler {
1416
private conversationState: BotState;
@@ -43,6 +45,11 @@ export class SimonBot extends TeamsActivityHandler {
4345

4446
this.onMessage(async (context, next) => {
4547

48+
// If result comes from an Adaptive Card
49+
if (context.activity.text === undefined && context.activity.value ) {
50+
context.activity.text = JSON.stringify(context.activity.value);
51+
}
52+
4653
// Run the Dialog with the new message Activity.
4754
await (this.dialog as MainDialog).run(context, this.dialogState);
4855

@@ -61,10 +68,10 @@ export class SimonBot extends TeamsActivityHandler {
6168

6269
this.onMembersAdded(async (context, next) => {
6370
const membersAdded = context.activity.membersAdded;
71+
const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
6472
for (const member of membersAdded) {
6573
if (member.id !== context.activity.recipient.id) {
66-
const welcome = `Welcome to Simon Bot ${ member.name }. This Bot is a work in progress. At this time we have some dialogs working. Type anything to get started.`;
67-
await context.sendActivity(welcome);
74+
await context.sendActivity({ attachments: [welcomeCard] });
6875
}
6976
}
7077
// By calling next() you ensure that the next BotHandler is run.
@@ -82,6 +89,17 @@ export class SimonBot extends TeamsActivityHandler {
8289
});
8390
}
8491

92+
/**
93+
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
94+
*/
95+
public async run(context): Promise<void> {
96+
await super.run(context);
97+
98+
// Save any state changes. The load happened during the execution of the Dialog.
99+
await this.conversationState.saveChanges(context, false);
100+
await this.userState.saveChanges(context, false);
101+
}
102+
85103
protected async handleTeamsSigninVerifyState(context: TurnContext, query: SigninStateVerificationQuery): Promise<void> {
86104
await (this.dialog as MainDialog).run(context, this.dialogState);
87105
}

src/dialogs/aliasResolverDialog.ts

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

src/dialogs/ownerResolverDialog.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Attachment, CardFactory } from 'botbuilder';
12
import {
23
ComponentDialog,
34
DialogTurnResult,
@@ -8,6 +9,7 @@ import {
89
WaterfallStepContext
910
} from 'botbuilder-dialogs';
1011
import { GraphHelper } from '../helpers/graphHelper';
12+
import GenericCard from '../resources/generic.json';
1113

1214
const TEXT_PROMPT = 'textPrompt';
1315
const WATERFALL_DIALOG = 'waterfallDialog';
@@ -77,12 +79,18 @@ export class OwnerResolverDialog extends ComponentDialog {
7779
OwnerResolverDialog.tokenResponse = tokenResponse;
7880

7981
const siteDetails = (stepContext.options as any).siteDetails;
80-
const promptMsg = 'Provide an owner email';
82+
const promptMsg = `Provide an owner email for your ${siteDetails.siteType} site`;
8183

8284
if (!siteDetails.owner) {
83-
return await stepContext.prompt(TEXT_PROMPT, {
84-
prompt: promptMsg
85-
});
85+
86+
const ownerCard: Attachment = CardFactory.adaptiveCard(JSON.parse(
87+
JSON.stringify(GenericCard).replace('$Placeholder', promptMsg)));
88+
89+
await stepContext.context.sendActivity({ attachments: [ownerCard] });
90+
return await stepContext.prompt(TEXT_PROMPT,
91+
{
92+
prompt: ''
93+
});
8694
} else {
8795
return await stepContext.next(siteDetails.owner);
8896
}

0 commit comments

Comments
 (0)