-
Notifications
You must be signed in to change notification settings - Fork 275
/
rootBot.js
99 lines (79 loc) · 4.26 KB
/
rootBot.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityHandler, TurnContext } = require('botbuilder');
const { ActivityTypes } = require('botframework-schema');
class RootBot extends ActivityHandler {
constructor(conversationState, conversationIdFactory, skillClient) {
super();
if (!conversationState) throw new Error('[RootBot]: Missing parameter. conversationState is required');
if (!conversationIdFactory) throw new Error('[RootBot]: Missing parameter. conversationIdFactory is required');
if (!skillClient) throw new Error('[RootBot]: Missing parameter. skillClient is required');
this.conversationState = conversationState;
this.conversationIdFactory = conversationIdFactory;
this.skillClient = skillClient;
this.activeSkillProperty = this.conversationState.createProperty('activeSkillProperty');
this.targetSkill = {};
this.targetSkill.id = process.env.SkillId;
this.targetSkill.appId = process.env.SkillAppId;
this.targetSkill.skillEndpoint = process.env.SkillEndpoint;
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
this.onMessage(async (context, next) => {
// Try to get the active skill
let activeSkill = await this.activeSkillProperty.get(context, null);
if (activeSkill) {
// Send the activity to the skill
await this.sendToSkill(context, activeSkill);
} else {
if (context.activity.text.toLowerCase() === 'skill') {
await context.sendActivity('Got it, connecting you to the skill...');
// Set active skill
activeSkill = this.targetSkill;
this.activeSkillProperty.set(context, activeSkill);
// Send the activity to the skill
await this.sendToSkill(context, activeSkill);
} else {
await context.sendActivity('Me no nothin\'. Say \'skill\' and I\'ll patch you through');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
this.onUnrecognizedActivityType(async (context, next) => {
// Handle EndOfConversation returned by the skill.
if (context.activity.type === ActivityTypes.EndOfConversation) {
// forget skill invocation
this.activeSkillProperty.set(context, undefined);
// We are back at the root
await context.sendActivity('Back in the root bot. Say \'skill\' and I\'ll patch you through');
// Save conversation state
await this.conversationState.saveChanges(context, true);
}
// By calling next() you ensure that the next BotHandler is run.
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('Hello and welcome!');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
async sendToSkill(context, botFrameworkSkill) {
// NOTE: Always SaveChanges() before calling a skill so that any activity generated by the skill
// will have access to current accurate state.
await this.conversationState.saveChanges(context, true);
// route the activity to the skill
await this.skillClient.postToSkill(process.env.MicrosoftAppId, botFrameworkSkill, process.env.SkillHostEndpoint, context.activity);
}
}
module.exports.RootBot = RootBot;