forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
110 lines (101 loc) · 5.97 KB
/
bot.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
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Import required Bot Framework classes.
const { ActivityTypes } = require('botbuilder');
const { CardFactory } = require('botbuilder');
// Adaptive Card content
const IntroCard = require('./resources/IntroCard.json');
// Welcomed User property name
const WELCOMED_USER = 'welcomedUserProperty';
class WelcomeBot {
/**
*
* @param {UserState} User state to persist boolean flag to indicate
* if the bot had already welcomed the user
*/
constructor(userState) {
// Creates a new user property accessor.
// See https://aka.ms/about-bot-state-accessors to learn more about the bot state and state accessors.
this.welcomedUserProperty = userState.createProperty(WELCOMED_USER);
this.userState = userState;
}
/**
*
* @param {TurnContext} context on turn context object.
*/
async onTurn(turnContext) {
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
if (turnContext.activity.type === ActivityTypes.Message) {
// Read UserState. If the 'DidBotWelcomedUser' does not exist (first time ever for a user)
// set the default to false.
let didBotWelcomedUser = await this.welcomedUserProperty.get(turnContext, false);
// Your bot should proactively send a welcome message to a personal chat the first time
// (and only the first time) a user initiates a personal chat with your bot.
if (didBotWelcomedUser === false) {
// The channel should send the user name in the 'From' object
let userName = turnContext.activity.from.name;
await turnContext.sendActivity('You are seeing this message because this was your first message ever sent to this bot.');
await turnContext.sendActivity(`It is a good practice to welcome the user and provide personal greeting. For example, welcome ${ userName }.`);
// Set the flag indicating the bot handled the user's first message.
await this.welcomedUserProperty.set(turnContext, true);
} else {
// This example uses an exact match on user's input utterance.
// Consider using LUIS or QnA for Natural Language Processing.
let text = turnContext.activity.text.toLowerCase();
switch (text) {
case 'hello':
case 'hi':
await turnContext.sendActivity(`You said "${ turnContext.activity.text }"`);
break;
case 'intro':
case 'help':
await turnContext.sendActivity({
text: 'Intro Adaptive Card',
attachments: [CardFactory.adaptiveCard(IntroCard)]
});
break;
default :
await turnContext.sendActivity(`This is a simple Welcome Bot sample. You can say 'intro' to
see the introduction card. If you are running this bot in the Bot
Framework Emulator, press the 'Start Over' button to simulate user joining a bot or a channel`);
}
}
// Save state changes
await this.userState.saveChanges(turnContext);
} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
// Send greeting when users are added to the conversation.
await this.sendWelcomeMessage(turnContext);
} else {
// Generic message for all other activities
await turnContext.sendActivity(`[${ turnContext.activity.type } event detected]`);
}
}
/**
* Sends welcome messages to conversation members when they join the conversation.
* Messages are only sent to conversation members who aren't the bot.
* @param {TurnContext} turnContext
*/
async sendWelcomeMessage(turnContext) {
// Do we have any new members added to the conversation?
if (turnContext.activity.membersAdded.length !== 0) {
// Iterate over all new members added to the conversation
for (var idx in turnContext.activity.membersAdded) {
// Greet anyone that was not the target (recipient) of this message.
// Since the bot is the recipient for events from the channel,
// context.activity.membersAdded === context.activity.recipient.Id indicates the
// bot was added to the conversation, and the opposite indicates this is a user.
if (turnContext.activity.membersAdded[idx].id !== turnContext.activity.recipient.id) {
await turnContext.sendActivity(`Welcome to the 'Welcome User' Bot. This bot will introduce you to welcoming and greeting users.`);
await turnContext.sendActivity("You are seeing this message because the bot recieved atleast one 'ConversationUpdate'" +
'event,indicating you (and possibly others) joined the conversation. If you are using the emulator, ' +
"pressing the 'Start Over' button to trigger this event again. The specifics of the 'ConversationUpdate' " +
'event depends on the channel. You can read more information at https://aka.ms/about-botframework-welcome-user');
await turnContext.sendActivity(`It is a good pattern to use this event to send general greeting to user, explaining what your bot can do. ` +
`In this example, the bot handles 'hello', 'hi', 'help' and 'intro. ` +
`Try it now, type 'hi'`);
}
}
}
}
}
module.exports.WelcomeBot = WelcomeBot;