forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
61 lines (55 loc) · 2.7 KB
/
weather.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { LuisRecognizer } = require('botbuilder-ai');
// LUIS intent names. you can get this from the .lu file.
const GET_CONDITION_INTENT = 'Get_Weather_Condition';
const GET_FORECAST_INTENT = 'Get_Weather_Forecast';
const NONE_INTENT = 'None';
// LUIS entity names.
const LOCATION_ENTITY = 'Location';
const LOCATION_PATTERNANY_ENTITY = 'Location_PatternAny';
// this is the LUIS service type entry in the .bot file.
const WEATHER_LUIS_CONFIGURATION = 'Weather';
class Weather {
/**
*
* @param {BotConfiguration} bot configuration from .bot file
*/
constructor(botConfig) {
if (!botConfig) throw new Error('Need bot config');
// add recognizers
const luisConfig = botConfig.findServiceByNameOrId(WEATHER_LUIS_CONFIGURATION);
if (!luisConfig || !luisConfig.appId) throw new Error(`Weather LUIS model not found in .bot file. Please ensure you have all required LUIS models created and available in the .bot file. See readme.md for additional information\n`);
this.luisRecognizer = new LuisRecognizer({
applicationId: luisConfig.appId,
azureRegion: luisConfig.region,
// CAUTION: Its better to assign and use a subscription key instead of authoring key here.
endpointKey: luisConfig.authoringKey
});
}
/**
*
* @param {TurnContext} turn context object
*/
async onTurn(turnContext) {
// Call weather LUIS model.
const weatherResults = await this.luisRecognizer.recognize(turnContext);
const topWeatherIntent = LuisRecognizer.topIntent(weatherResults);
// Get location entity if available.
const locationEntity = (LOCATION_ENTITY in weatherResults.entities) ? weatherResults.entities[LOCATION_ENTITY][0] : undefined;
const locationPatternAnyEntity = (LOCATION_PATTERNANY_ENTITY in weatherResults.entities) ? weatherResults.entities[LOCATION_PATTERNANY_ENTITY][0] : undefined;
// Depending on intent, call "Turn On" or "Turn Off" or return unknown.
switch (topWeatherIntent) {
case GET_CONDITION_INTENT:
await turnContext.sendActivity(`You asked for current weather condition in Location = ` + (locationEntity || locationPatternAnyEntity));
break;
case GET_FORECAST_INTENT:
await turnContext.sendActivity(`You asked for weather forecast in Location = ` + (locationEntity || locationPatternAnyEntity));
break;
case NONE_INTENT:
default:
await turnContext.sendActivity(`Weather dialog cannot fulfill this request.`);
}
}
};
module.exports.Weather = Weather;