-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathindex.js
67 lines (50 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const restify = require('restify');
const { BotFrameworkAdapter, ActivityHandler, MemoryStorage, UserState, ConversationState, InspectionState, InspectionMiddleware } = require('botbuilder');
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
var memoryStorage = new MemoryStorage();
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();
console.log('welcome to test bot - a local test tool for working with the emulator');
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
});
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
await bot.run(turnContext);
});
});