fix(core.gbapp): Fixing after language new fetures being added.
This commit is contained in:
parent
09facb7aea
commit
0ecaa8a877
5 changed files with 72 additions and 15 deletions
|
@ -43,6 +43,7 @@ import { Messages } from '../strings';
|
|||
import { SecService } from '../../security.gbapp/services/SecService';
|
||||
import { GBServer } from '../../../src/app';
|
||||
import { GBConversationalService } from '../services/GBConversationalService';
|
||||
import { CollectionUtil } from 'pragmatismo-io-framework';
|
||||
/**
|
||||
* Dialog for the bot explains about itself.
|
||||
*/
|
||||
|
@ -58,18 +59,36 @@ export class LanguageDialog extends IGBDialog {
|
|||
|
||||
async step => {
|
||||
const locale = step.context.activity.locale;
|
||||
|
||||
return await min.conversationalService.prompt (min, step,
|
||||
|
||||
return await min.conversationalService.prompt(min, step,
|
||||
Messages[locale].which_language);
|
||||
},
|
||||
async step => {
|
||||
},
|
||||
async step => {
|
||||
|
||||
const locale = step.context.activity.locale;
|
||||
const user = await min.userProfile.get(step.context, {});
|
||||
|
||||
const list = [
|
||||
{ name: 'english', code: 'en' },
|
||||
{ name: 'portuguese', code: 'pt' },
|
||||
{ name: 'spanish', code: 'es' },
|
||||
{ name: 'german', code: 'de' },
|
||||
{ name: 'deutsch', code: 'de' }
|
||||
];
|
||||
let translatorLocale = null;
|
||||
await CollectionUtil.asyncForEach(list, async item => {
|
||||
if (GBConversationalService.kmpSearch(step.result, item.name) != -1) {
|
||||
translatorLocale = item.code;
|
||||
}
|
||||
});
|
||||
|
||||
let sec = new SecService();
|
||||
let from = step.context.activity.from.id;
|
||||
const botId = step.result;
|
||||
const instance = await min.core.loadInstanceByBotId(botId);
|
||||
await sec.updateUserInstance(from, instance.instanceId);
|
||||
await min.conversationalService.sendText(min, step, `Opa, vamos lá!`);
|
||||
|
||||
user.systemUser = await sec.updateUserLocale(user.systemUser.userId, translatorLocale);
|
||||
|
||||
await min.userProfile.set(step.context, user);
|
||||
await min.conversationalService.sendText(min, step,
|
||||
Messages[locale].language_chosen );
|
||||
|
||||
return await step.next();
|
||||
}
|
||||
]));
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
import { GBDialogStep, GBLog, GBMinInstance, IGBCoreService, IGBPackage } from 'botlib';
|
||||
import { Sequelize } from 'sequelize-typescript';
|
||||
import { BroadcastDialog } from './dialogs/BroadcastDialog';
|
||||
import { LanguageDialog } from './dialogs/LanguageDialog';
|
||||
import { SwitchBotDialog } from './dialogs/SwitchBot';
|
||||
import { WelcomeDialog } from './dialogs/WelcomeDialog';
|
||||
import { WhoAmIDialog } from './dialogs/WhoAmIDialog';
|
||||
|
@ -78,5 +79,6 @@ export class GBCorePackage implements IGBPackage {
|
|||
SwitchBotDialog.setup(min.bot, min);
|
||||
DialogClass.setup(min.bot, min);
|
||||
BroadcastDialog.setup(min.bot, min);
|
||||
LanguageDialog.setup(min.bot, min);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -679,4 +679,36 @@ export class GBConversationalService {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static kmpSearch(pattern, text) {
|
||||
pattern = pattern.toLowerCase();
|
||||
text = text.toLowerCase();
|
||||
if (pattern.length == 0)
|
||||
return 0; // Immediate match
|
||||
|
||||
// Compute longest suffix-prefix table
|
||||
var lsp = [0]; // Base case
|
||||
for (var i = 1; i < pattern.length; i++) {
|
||||
var j = lsp[i - 1]; // Start by assuming we're extending the previous LSP
|
||||
while (j > 0 && pattern.charAt(i) != pattern.charAt(j))
|
||||
j = lsp[j - 1];
|
||||
if (pattern.charAt(i) == pattern.charAt(j))
|
||||
j++;
|
||||
lsp.push(j);
|
||||
}
|
||||
|
||||
// Walk through text string
|
||||
var j = 0; // Number of chars matched in pattern
|
||||
for (var i = 0; i < text.length; i++) {
|
||||
while (j > 0 && text.charAt(i) != pattern.charAt(j))
|
||||
j = lsp[j - 1]; // Fall back in the pattern
|
||||
if (text.charAt(i) == pattern.charAt(j)) {
|
||||
j++; // Next char matched, increment position
|
||||
if (j == pattern.length)
|
||||
return i - (j - 1);
|
||||
}
|
||||
}
|
||||
return -1; // Not found
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -764,8 +764,9 @@ export class GBMinService {
|
|||
await step.beginDialog('/publish', { confirm: true, firstTime: true });
|
||||
} else {
|
||||
let text = context.activity.text;
|
||||
const originalText = text;
|
||||
text = text.replace(/<([^>]+?)([^>]*?)>(.*?)<\/\1>/gi, '');
|
||||
|
||||
|
||||
// Spells check the input text before translating.
|
||||
|
||||
text = await min.conversationalService.spellCheck(min, text);
|
||||
|
@ -787,13 +788,14 @@ export class GBMinService {
|
|||
const systemUser = user.systemUser;
|
||||
if (systemUser.locale != locale) {
|
||||
let sec = new SecService();
|
||||
await sec.updateUserLocale(systemUser.userId, locale);
|
||||
user.systemUser = await sec.updateUserLocale(systemUser.userId, locale);
|
||||
await min.userProfile.set(step.context, user);
|
||||
}
|
||||
}
|
||||
|
||||
// Translates the input text if is turned on instance params.
|
||||
|
||||
const originalText = context.text;
|
||||
|
||||
let contentLocale = min.core.getParam<string>(
|
||||
min.instance,
|
||||
'Default Content Language',
|
||||
|
|
|
@ -10,8 +10,9 @@ export const Messages = {
|
|||
very_sorry_about_error: `I'm sorry to inform that there was an error which was recorded to be solved.`,
|
||||
canceled: 'Canceled. If I can be useful, let me know how',
|
||||
whats_email: "What's your E-mail address?",
|
||||
which_language: "Which language would you like to choose from?",
|
||||
which_language: "Please, type the language name you would like to talk through.",
|
||||
validation_enter_valid_email: "Please enter a valid e-mail." ,
|
||||
language_chosen: "Very good, so let's go..." ,
|
||||
affirmative_sentences: /^(sim|s|positivo|afirmativo|claro|evidente|sem dúvida|confirmo|confirmar|confirmado|uhum)/i,
|
||||
|
||||
},
|
||||
|
@ -24,8 +25,9 @@ export const Messages = {
|
|||
very_sorry_about_error: `Lamento, ocorreu um erro que já foi registrado para ser tratado.`,
|
||||
canceled: 'Cancelado, avise como posso ser útil novamente.',
|
||||
whats_email: "Qual seu e-mail?",
|
||||
which_language: "Qual idioma você gostaria de usar?",
|
||||
which_language: "Por favor, digite o idioma que você gostaria de usar para conversarmos.",
|
||||
validation_enter_valid_email: "Por favor digite um email válido.",
|
||||
language_chosen: "Muito bem, então vamos lá..." ,
|
||||
affirmative_sentences: /^(sim|s|positivo|afirmativo|claro|evidente|sem dúvida|confirmo|confirmar|confirmado|uhum)/i,
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue