forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
57 lines (51 loc) · 3.28 KB
/
Copy pathbot.js
File metadata and controls
57 lines (51 loc) · 3.28 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityTypes } = require('botbuilder');
const { LuisRecognizer } = require('botbuilder-ai');
/**
* A simple bot that responds to utterances with answers from the Language Understanding (LUIS) service.
* If an answer is not found for an utterance, the bot responds with help.
*/
class LuisBot {
/**
* The LuisBot constructor requires one argument (`application`) which is used to create an instance of `LuisRecognizer`.
* @param {LuisApplication} luisApplication The basic configuration needed to call LUIS. In this sample the configuration is retrieved from the .bot file.
* @param {LuisPredictionOptions} luisPredictionOptions (Optional) Contains additional settings for configuring calls to LUIS.
*/
constructor(application, luisPredictionOptions, includeApiResults) {
this.luisRecognizer = new LuisRecognizer(application, luisPredictionOptions, true);
}
/**
* Every conversation turn calls this method.
* There are no dialogs used, since it's "single turn" processing, meaning a single request and
* response, with no stateful conversation.
* @param {TurnContext} turnContext A TurnContext instance, containing all the data needed for processing the conversation turn.
*/
async onTurn(turnContext) {
// By checking the incoming Activity type, the bot only calls LUIS in appropriate cases.
if (turnContext.activity.type === ActivityTypes.Message) {
// Perform a call to LUIS to retrieve results for the user's message.
const results = await this.luisRecognizer.recognize(turnContext);
// Since the LuisRecognizer was configured to include the raw results, get the `topScoringIntent` as specified by LUIS.
const topIntent = results.luisResult.topScoringIntent;
if (topIntent.intent !== 'None') {
await turnContext.sendActivity(`LUIS Top Scoring Intent: ${ topIntent.intent }, Score: ${ topIntent.score }`);
} else {
// If the top scoring intent was "None" tell the user no valid intents were found and provide help.
await turnContext.sendActivity(`No LUIS intents were found.
\nThis sample is about identifying two user intents:
\n - 'Calendar.Add'
\n - 'Calendar.Find'
\nTry typing 'Add Event' or 'Show me tomorrow'.`);
}
} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate &&
turnContext.activity.recipient.id !== turnContext.activity.membersAdded[0].id) {
// If the Activity is a ConversationUpdate, send a greeting message to the user.
await turnContext.sendActivity('Welcome to the NLP with LUIS sample! Send me a message and I will try to predict your intent.');
} else if (turnContext.activity.type !== ActivityTypes.ConversationUpdate) {
// Respond to all other Activity types.
await turnContext.sendActivity(`[${ turnContext.activity.type }]-type activity detected.`);
}
}
}
module.exports.LuisBot = LuisBot;