This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathrootBot.js
More file actions
61 lines (49 loc) · 2.24 KB
/
rootBot.js
File metadata and controls
61 lines (49 loc) · 2.24 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// @ts-check
const { ActivityHandler, ActivityTypes, MessageFactory } = require('botbuilder');
const { runDialog } = require('botbuilder-dialogs');
/** @import { ConversationState } from 'botbuilder' */
/** @import { MainDialog } from '../dialogs/mainDialog' */
class RootBot extends ActivityHandler {
/**
*
* @param {ConversationState} conversationState
* @param {MainDialog} dialog
*/
constructor(conversationState, dialog) {
super();
if (!conversationState) throw new Error('[RootBot]: Missing parameter. conversationState is required');
if (!dialog) throw new Error('[RootBot]: Missing parameter. dialog is required');
this.conversationState = conversationState;
this.dialog = dialog;
this.onTurn(async (turnContext, next) => {
if (turnContext.activity.type !== ActivityTypes.ConversationUpdate) {
// Run the Dialog with the activity.
await runDialog(this.dialog, turnContext, this.conversationState.createProperty('DialogState'));
}
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded ?? [];
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
// Greet anyone that was not the target (recipient) of this message.
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity(MessageFactory.text('Hello and welcome!'));
await runDialog(this.dialog, context, conversationState.createProperty('DialogState'));
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
}
}
module.exports.RootBot = RootBot;