Skip to content

Instantly share code, notes, and snippets.

@poul-kg
Created November 14, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poul-kg/5c48bd6e17ed26974da1811940316e78 to your computer and use it in GitHub Desktop.
Save poul-kg/5c48bd6e17ed26974da1811940316e78 to your computer and use it in GitHub Desktop.
MS BotFramework LUIS timezoneOffset
/**
* How to change timestamp in ms botframework on every request to LUIS by the bot
*/
const builder = require('botbuilder');
/**
* Create custom LUIS recognizer which can change timezoneOffset in LUIS URL to another offset if we have
* timezone offset somewhere in the session
*
* context.userData has value of session.userData, if you saved something there previously via session.userData.x = 2
*
* @param luisModelUrlStr
* @return {{recognize: recognize}}
*/
function getLuisRecognizer(luisModelUrlStr) {
let luisModelUrl = luisModelUrlStr;
return {
recognize: (context, callback) => {
// console.log('context #################');
// console.log(context);
// Use UTC offset of selected location
if (context.userData !== undefined && context.userData.location !== undefined && context.userData.location.utc_offset) {
luisModelUrl = luisModelUrl.replace('timezoneOffset=0', `timezoneOffset=${context.userData.location.utc_offset}`);
console.log('############# TIMEZONE ROCKS!!!');
console.log(luisModelUrl)
}
builder.LuisRecognizer.recognize(context.message.text, luisModelUrl, (err, intents, entities) => {
if (err) {
callback(err, null);
} else {
const intentRecognizerResult = {
intent: intents[0].intent,
score: intents[0].score,
intents: intents,
entities: entities
};
// console.log('################# ENTITIES ###########');
// console.log(JSON.stringify(entities));
callback(null, intentRecognizerResult);
}
});
}
}
}
const recognizers = [];
// list of LUIS app's connected to the bot
const luisModels = [
'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/...',
];
// Create custom LUIS recognizers, which can modify timezoneOffset
luisModels.forEach(model => {
recognizers.push(getLuisRecognizer(model));
});
const luisConfig = {
recognizers: recognizers,
// recognizeOrder: builder.RecognizeOrder.series,
recognizeOrder: builder.RecognizeOrder.parallel,
intentThreshold: 0.6
};
const intentDialog = new builder.IntentDialog(luisConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment