Skip to content

Commit

Permalink
Updated to botbuilder 4.0.0-m4.2 and tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmayomsft committed Jun 1, 2018
2 parents c415333 + 46c3b56 commit 664798d
Show file tree
Hide file tree
Showing 50 changed files with 260 additions and 393 deletions.
3 changes: 2 additions & 1 deletion samples/alarmbot-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"dependencies": {
"@types/node": "^8.0.28",
"@types/restify": "^5.0.3",
"botbuilder": "4.0.0-m2.1",
"botbuilder": "4.0.0-m4.2",
"restify": "^6.0.0",
"botbuilder-botbldr": "0.2.1",
"promptly-bot": "~0.0.1"
}
}
14 changes: 5 additions & 9 deletions samples/alarmbot-ts/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ServiceBot } from 'botbuilder-botbldr';
import { PromptlyBotConversationState } from 'promptly-bot';
import { BotFrameworkBot, StateBotContext } from './bot/BotFrameworkBot';
import { RootTopic, RootTopicState } from './topics/rootTopic';
import { Alarm } from './alarms';
import { Alarm } from './models/alarms';

// Define conversation state shape
export interface BotConversationState extends PromptlyBotConversationState<RootTopicState> { }
Expand All @@ -11,14 +11,10 @@ export interface BotUserState {
alarms?: Alarm[];
}

const alarmBot = new BotFrameworkBot<BotConversationState, BotUserState>();
const alarmBot = new ServiceBot<BotConversationState, BotUserState>();

alarmBot.onReceiveActivity(async context => {
// State isn't fully initialized until the contact/conversation messages are sent, so have to require
// activity type is message. Will affect welcome message. Refactor after bug has been addressed.
if(context.request.type === 'message') {
alarmBot.onTurn(async context => {

return new RootTopic(context)
.onReceiveActivity(context);
}
.onTurn(context);
});
18 changes: 0 additions & 18 deletions samples/alarmbot-ts/src/bot/BaseBot.ts

This file was deleted.

32 changes: 0 additions & 32 deletions samples/alarmbot-ts/src/bot/BotFrameworkBot.ts

This file was deleted.

27 changes: 0 additions & 27 deletions samples/alarmbot-ts/src/bot/StateBotContext.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { BotContext } from 'botbuilder';
import { BotConversationState, BotUserState } from './app';
import { StateBotContext } from './bot/StateBotContext';
import { TurnContext } from 'botbuilder';

export interface Alarm {
title: string;
Expand All @@ -13,22 +11,22 @@ export function findAlarmIndex(alarms: Alarm[], title: string): number {
});
}

export function showAlarms(context: BotContext, alarms: Alarm[]) {
export function showAlarms(context: TurnContext, alarms: Alarm[]) {

if (!alarms || (alarms.length === 0)) {
context.sendActivity(`You have no alarms.`);
return;
}

if (alarms.length == 1) {
context.sendActivity(`You have one alarm named '${alarms[0].title}', set for ${alarms[0].time}.`);
context.sendActivity(`You have one alarm named '${ alarms[0].title }', set for ${ alarms[0].time }.`);
return;
}

let message = `You have ${alarms.length} alarms: \n\n`;
let message = `You have ${ alarms.length } alarms: \n`;

alarms.forEach((alarm) => {
message += `'${alarm.title}' set for ${alarm.time}\n\n`;
message += `'${ alarm.title }' set for ${ alarm.time }\n`;
});

context.sendActivity(message);
Expand Down
57 changes: 29 additions & 28 deletions samples/alarmbot-ts/src/topics/addAlarmTopic.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Alarm } from '../alarms';
import { StateContext } from 'botbuilder-botbldr';
import { Alarm } from '../models/alarms';
import { ConversationTopic, ConversationTopicState, Prompt, Validator } from 'promptly-bot';
import { StateBotContext } from '../bot/StateBotContext';
import { BotConversationState, BotUserState } from '../app';
import { ResourceResponse } from 'botbuilder';

export interface AddAlarmTopicState extends ConversationTopicState {
alarm: Alarm;
}

export class AddAlarmTopic extends ConversationTopic<StateBotContext<BotConversationState, BotUserState>, AddAlarmTopicState, Alarm> {
export class AddAlarmTopic extends ConversationTopic<StateContext<BotConversationState, BotUserState>, AddAlarmTopicState, Alarm> {

public constructor(state: AddAlarmTopicState = { alarm: {} as Alarm, activeTopic: undefined }) {
super(state);

this.subTopics
.set("titlePrompt", () => new Prompt<StateBotContext<BotConversationState, BotUserState>, string>()
.onPrompt((context, lastTurnReason) => {
.set("titlePrompt", () => new Prompt<StateContext<BotConversationState, BotUserState>, string>()
.onPrompt(async (context, lastTurnReason) => {

if(lastTurnReason && lastTurnReason === 'titletoolong') {
context.sendActivity(`Sorry, alarm titles must be less that 20 characters.`,
await context.sendActivity(`Sorry, alarm titles must be less that 20 characters.`,
`Let's try again.`);
}

return context.sendActivity(`What would you like to name your alarm?`);
})
.validator(new AlarmTitleValidator())
Expand All @@ -30,19 +31,19 @@ export class AddAlarmTopic extends ConversationTopic<StateBotContext<BotConversa

this.state.alarm.title = value;

return this.onReceiveActivity(context);
return this.onTurn(context);
})
.onFailure((context, reason) => {
.onFailure(async (context, reason) => {
this.clearActiveTopic();

if(reason && reason === 'toomanyattempts') {
context.sendActivity(`I'm sorry I'm having issues understanding you.`);
await context.sendActivity(`I'm sorry I'm having issues understanding you.`);
}

return this._onFailure(context, reason);
return this._onFailure!(context, reason);
})
)
.set("timePrompt", () => new Prompt<StateBotContext<BotConversationState, BotUserState>, string>()
.set("timePrompt", () => new Prompt<StateContext<BotConversationState, BotUserState>, string>()
.onPrompt((context, lastTurnReason) => {
return context.sendActivity(`What time would you like to set your alarm for?`);
})
Expand All @@ -53,52 +54,52 @@ export class AddAlarmTopic extends ConversationTopic<StateBotContext<BotConversa

this.state.alarm.time = value;

return this.onReceiveActivity(context);
return this.onTurn(context);
})
.onFailure((context, reason) => {
.onFailure(async (context, reason) => {
this.clearActiveTopic();

if(reason && reason === 'toomanyattempts') {
return context.sendActivity(`I'm sorry I'm having issues understanding you.`);
await context.sendActivity(`I'm sorry I'm having issues understanding you.`);
}

return this._onFailure(context, reason);;
return this._onFailure!(context, reason);;
})
);
};

public onReceiveActivity(context: StateBotContext<BotConversationState, BotUserState>) {
public onTurn(context: StateContext<BotConversationState, BotUserState>) {

if(this.hasActiveTopic) {
return this.activeTopic.onReceiveActivity(context);
return this.activeTopic!.onTurn(context);
}

if (!this.state.alarm.title) {
return this.setActiveTopic("titlePrompt")
.onReceiveActivity(context);
.onTurn(context);
}

if (!this.state.alarm.time) {
return this.setActiveTopic("timePrompt")
.onReceiveActivity(context);
.onTurn(context);
}

return this._onSuccess(context, this.state.alarm);
return this._onSuccess!(context, this.state.alarm);
}
}

class AlarmTitleValidator extends Validator<StateBotContext<BotConversationState, BotUserState>, string> {
public validate(context: StateBotContext<BotConversationState, BotUserState>) {
if(context.request.text.length > 20) {
class AlarmTitleValidator extends Validator<StateContext<BotConversationState, BotUserState>, string> {
public validate(context: StateContext<BotConversationState, BotUserState>) {
if(context.activity.text.length > 20) {
return { reason: 'titletoolong' };
} else {
return { value: context.request.text };
return { value: context.activity.text };
}
}
}

class AlarmTimeValidator extends Validator<StateBotContext<BotConversationState, BotUserState>, string> {
public validate(context: StateBotContext<BotConversationState, BotUserState>) {
return { value: context.request.text };
class AlarmTimeValidator extends Validator<StateContext<BotConversationState, BotUserState>, string> {
public validate(context: StateContext<BotConversationState, BotUserState>) {
return { value: context.activity.text };
}
}
Loading

0 comments on commit 664798d

Please sign in to comment.