Skip to content

Commit

Permalink
[[FIX]] Language selection algorithm (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
palmerabollo authored Nov 22, 2016
1 parent e858f4c commit e1c4afa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
16 changes: 14 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ export class Bot extends BotBuilder.UniversalBot {
resetCommand: /^reset/i
}));

let supportedLanguages = this.supportedLanguages();
logger.debug('Bot supported languages', supportedLanguages);

let middlewares = [
Audio,
DirectLinePrompts,
Logger,
Normalizer,
LanguageDetector,
LanguageDetector(supportedLanguages),
Admin,
EventHub,
Slack
];
middlewares.forEach((middleware) => this.use(middleware));
this.use(middlewares);

this.on('error', err => logger.error(err));

Expand Down Expand Up @@ -118,6 +121,15 @@ export class Bot extends BotBuilder.UniversalBot {
return new BotBuilder.LuisRecognizer(modelMap);
});
}

private supportedLanguages(): string[] {
let modelMapSet = this.get('modelMapSet') as BotBuilder.ILuisModelMap[];
let languages = modelMapSet.map((modelMap) => Object.keys(modelMap))
.reduce((a, b) => a.concat(b))
.filter((v, i, a) => a.indexOf(v) === i); // unique

return languages;
}
}

function createkUnhandledMessageResponse(session: BotBuilder.Session, args: any): BotBuilder.Message {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/eventhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function sendEventHub(payload: any) {
let eventHubKeyName = process.env.EVENTHUB_KEYNAME; // ex. 'send'
let eventHubKey = process.env.EVENTHUB_KEY; // ex. 'key';

let eventHubPulbisherUri = 'https://' + namespace + '.servicebus.windows.net' + '/' + hubname + '/publishers/' + publisher + '/messages';
let eventHubPulbisherUri = `https://${namespace}.servicebus.windows.net/${hubname}/publishers/${publisher}/messages`;

// See http://msdn.microsoft.com/library/azure/dn170477.aspx
function createTokenSAS(uri: string, keyName: string, key: string) {
Expand Down
54 changes: 28 additions & 26 deletions src/middlewares/language-detector.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
import * as BotBuilder from 'botbuilder';
import * as logger from 'logops';

export default {
botbuilder: (session: BotBuilder.Session, next: Function) => {
resolveLocale(session)
.then((locale) => setSessionLocale(session, locale))
.then(() => next())
.catch((err) => next(err));
}
} as BotBuilder.IMiddlewareMap;

function setSessionLocale(session: BotBuilder.Session, locale: string): Promise<void> {
return new Promise((resolve, reject) => {
session.preferredLocale(locale, (err) => {
if (err) {
logger.error(err, 'Not able to set preferred locale');
return reject(err);
}

// Save the locale as part of userData because a fallback value might be needed in future messages
session.userData.preferredLocale = locale;

logger.info({preferredLocale: session.preferredLocale(), textLocale: session.message.textLocale}, 'Language detector');
return resolve();
});
});
export default function factory(supportedLanguages: string[]): BotBuilder.IMiddlewareMap {
return {
botbuilder: (session: BotBuilder.Session, next: Function) => {
resolveLocale(session, supportedLanguages)
.then((locale) => setSessionLocale(session, locale))
.then(() => next())
.catch((err) => next(err));
}
} as BotBuilder.IMiddlewareMap;
}

function resolveLocale(session: BotBuilder.Session): Promise<string> {
function resolveLocale(session: BotBuilder.Session, supportedLanguages: string[]): Promise<string> {
let locale = detectClientLocale(session.message);
if (locale) {
if (locale && supportedLanguages.indexOf(locale) >= 0) {
return Promise.resolve(locale);
}

Expand All @@ -49,3 +34,20 @@ function detectClientLocale(message: BotBuilder.IMessage): string {

return null;
}

function setSessionLocale(session: BotBuilder.Session, locale: string): Promise<void> {
return new Promise((resolve, reject) => {
session.preferredLocale(locale, (err) => {
if (err) {
logger.error(err, 'Not able to set preferred locale');
return reject(err);
}

// Save the locale as part of userData because a fallback value might be needed in future messages
session.userData.preferredLocale = locale;

logger.info({preferredLocale: session.preferredLocale(), textLocale: session.message.textLocale}, 'Language detector');
return resolve();
});
});
}

0 comments on commit e1c4afa

Please sign in to comment.