Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testbot to parity with DotNet #47

Merged
merged 2 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions libraries/testbot/bots/myBot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { ActivityHandler } = require('botbuilder');

class MyBot extends ActivityHandler {
constructor(conversationState) {
super();
this.conversationState = conversationState;
this.conversationStateAccessor = this.conversationState.createProperty('test');
this.onMessage(async (context, next) => {

var state = await this.conversationStateAccessor.get(context, { count: 0 });

await context.sendActivity(`you said "${ context.activity.text }" ${ state.count }`);

state.count++;
await this.conversationState.saveChanges(context, false);

await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity(`welcome ${ membersAdded[cnt].name }`);
}
}
await next();
});
}
}

exports.MyBot = MyBot;
39 changes: 9 additions & 30 deletions libraries/testbot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

const restify = require('restify');

const { BotFrameworkAdapter, ActivityHandler, MemoryStorage, UserState, ConversationState, InspectionState, InspectionMiddleware } = require('botbuilder');
const { BotFrameworkAdapter, MemoryStorage, UserState, ConversationState, InspectionState, InspectionMiddleware } = require('botbuilder');
const { MyBot } = require('./bots/myBot')

const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
Expand All @@ -16,42 +17,14 @@ var inspectionState = new InspectionState(memoryStorage);
var userState = new UserState(memoryStorage);
var conversationState = new ConversationState(memoryStorage);

var conversationStateAccessor = conversationState.createProperty('test');

adapter.use(new InspectionMiddleware(inspectionState, userState, conversationState));

adapter.onTurnError = async (context, error) => {
console.error(`\n [onTurnError]: ${ error }`);
await context.sendActivity(`Oops. Something went wrong!`);
};

class TestBot extends ActivityHandler {
constructor() {
super();
this.onMessage(async (context, next) => {

var state = await conversationStateAccessor.get(context, { count: 0 });

await context.sendActivity(`you said "${ context.activity.text }" ${ state.count }`);

state.count++;
await conversationState.saveChanges(context, false);

await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity(`welcome ${ membersAdded[cnt].name }`);
}
}
await next();
});
}
}

var bot = new TestBot();
var bot = new MyBot(conversationState);

console.log('welcome to test bot - a local test tool for working with the emulator');

Expand All @@ -60,6 +33,12 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
});

server.post('/api/mybot', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
await bot.run(turnContext);
});
});

server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
await bot.run(turnContext);
Expand Down
Loading