new(security.gblib): SMS Auth.

This commit is contained in:
Rodrigo Rodriguez 2024-03-11 15:03:53 -03:00
parent 27cdb25322
commit 6112ce9a1c
4 changed files with 853 additions and 832 deletions

View file

@ -135,7 +135,6 @@
"ffmpeg-static": "5.1.0",
"google-libphonenumber": "3.2.31",
"googleapis": "126.0.1",
"ibm-watson": "7.1.2",
"iso-639-1": "3.1.1",
"join-images-updated": "1.1.4",

View file

@ -49,7 +49,7 @@ import { GuaribasUser } from '../../security.gbapp/models/index.js';
import { GBMinService } from './GBMinService.js';
import urlJoin from 'url-join';
import Fs from 'fs';
import PasswordGenerator from 'strict-password-generator';
import twilio from 'twilio';
import Nexmo from 'nexmo';
import { join } from 'path';
import shell from 'any-shell-escape';
@ -62,6 +62,7 @@ import { IamAuthenticator } from 'ibm-watson/auth/index.js';
import * as marked from 'marked';
import Translate from '@google-cloud/translate';
import { GBUtil } from '../../../src/util.js';
import { GBLogEx } from './GBLogEx.js';
/**
* Provides basic services for handling messages and dispatching to back-end
@ -69,7 +70,7 @@ import { GBUtil } from '../../../src/util.js';
*/
export class GBConversationalService {
public async getNewMobileCode(){
public async getNewMobileCode() {
throw new Error('Method removed.');
}
@ -354,8 +355,27 @@ export class GBConversationalService {
// tslint:disable:no-unsafe-any due to Nexmo.
public async sendSms(min: GBMinInstance, mobile: string, text: string): Promise<any> {
GBLog.info(`Sending SMS to ${mobile} with text: '${text}'.`);
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(null, authToken, { accountSid: accountSid });
if (!min.instance.smsKey && min.instance.smsSecret) {
let botNumber = min.core.getParam<string>(min.instance, 'Bot Number', null);
if (!botNumber) {
botNumber = process.env.BOT_NUMBER;
}
const msg = await client.messages
.create({
body: text,
from: '+' + botNumber,
to: '+' + mobile
})
GBLogEx.info(min, `SMS sent, return: ${msg.sid}.`);
}
return;
if(!min.instance.smsKey && min.instance.smsSecret) {
const url = 'http://sms-api.megaconecta.com.br/mt';
let options = {
method: 'POST',
@ -381,7 +401,7 @@ export class GBConversationalService {
return Promise.reject(new Error(msg));
}
} else {
} else {
if (min.instance.smsKey && min.instance.smsSecret) {
return new Promise((resolve: any, reject: any): any => {
const nexmo = new Nexmo({
@ -400,15 +420,15 @@ export class GBConversationalService {
});
});
}
}
}
}
public async sendToMobile(min: GBMinInstance, mobile: string, message: string, conversationId) {
GBLog.info(`Sending message ${message} to ${mobile}...`);
await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId);
}
}
public static async getAudioBufferFromText(text): Promise<string> {
public static async getAudioBufferFromText(text): Promise < string > {
return new Promise<string>(async (resolve, reject) => {
const name = GBAdminService.getRndReadableIdentifier();
@ -446,9 +466,9 @@ export class GBConversationalService {
reject(error);
}
});
}
}
public static async getTextFromAudioBuffer(speechKey, cloudRegion, buffer, locale): Promise<string> {
public static async getTextFromAudioBuffer(speechKey, cloudRegion, buffer, locale): Promise < string > {
return new Promise<string>(async (resolve, reject) => {
try {
const oggFile = new Readable();
@ -516,7 +536,7 @@ export class GBConversationalService {
return Promise.reject(error);
}
});
}
}
public static getIBMAudioModelNameFromLocale = locale => {
const locales = {
"ar": "ar-MS_BroadbandModel",
@ -534,7 +554,7 @@ export class GBConversationalService {
const languageCode = locale.substring(0, 2);
return locales[languageCode] || "en-US_BroadbandModel";
};
};
public async playMarkdown(min: GBMinInstance, answer: string, channel: string,
@ -606,7 +626,7 @@ export class GBConversationalService {
const html = marked(text);
await min.conversationalService.sendText(min, step, html);
}
}
}
private async sendHTMLToWeb(min, step: GBDialogStep, html: string, answer: string) {
const locale = step.context.activity.locale;
@ -621,7 +641,7 @@ export class GBConversationalService {
nextId: 0
}
});
}
}
// tslint:enable:no-unsafe-any
@ -788,7 +808,7 @@ export class GBConversationalService {
await this.sendToMobile(min, mobile, currentText, null);
}
}
}
}
public async routeNLP(step: GBDialogStep, min: GBMinInstance, text: string) {
if (min.instance.nlpAppId === null || min.instance.nlpAppId === undefined) {
@ -892,11 +912,11 @@ export class GBConversationalService {
GBLog.info(`NLP NOT called: score: ${score} > required (nlpScore): ${instanceScore}`);
return false;
}
}
public async getLanguage(min: GBMinInstance, text: string): Promise<string> {
public async getLanguage(min: GBMinInstance, text: string): Promise < string > {
const key = min.core.getParam<string>(min.instance, 'textAnalyticsKey', null);
if (!key) {
if(!key) {
return process.env.DEFAULT_USER_LANGUAGE;
}
let language = await AzureText.getLocale(
@ -906,12 +926,12 @@ export class GBConversationalService {
);
return language === '(Unknown)' ? 'en' : language;
}
}
public async spellCheck(min: GBMinInstance, text: string): Promise<string> {
public async spellCheck(min: GBMinInstance, text: string): Promise < string > {
const key = min.core.getParam<string>(min.instance, 'spellcheckerKey', null);
if (key) {
if(key) {
text = text.charAt(0).toUpperCase() + text.slice(1);
const data = await AzureText.getSpelledText(key, text);
if (data !== text) {
@ -921,9 +941,9 @@ export class GBConversationalService {
}
return text;
}
}
public async translate(min: GBMinInstance, text: string, language: string): Promise<string> {
public async translate(min: GBMinInstance, text: string, language: string): Promise < string > {
const translatorEnabled = () => {
if (min.instance.params) {
@ -935,23 +955,23 @@ export class GBConversationalService {
const endPoint = min.core.getParam<string>(min.instance, 'translatorEndpoint', null);
const key = min.core.getParam<string>(min.instance, 'translatorKey', null);
if (
if(
(endPoint === null && !min.instance.googleProjectId) ||
!translatorEnabled() ||
process.env.TRANSLATOR_DISABLED === 'true'
) {
return text;
}
}
if (text.length > 5000) {
if (text.length > 5000) {
text = text.substr(0, 4999);
GBLog.warn(`Text that bot will translate will be truncated due to MSFT service limitations.`);
} else if (text.length == 2) {
} else if (text.length == 2) {
return text;
}
text = text.replace('¿', '');
}
text = text.replace('¿', '');
if (min.instance.googleProjectId) {
if (min.instance.googleProjectId) {
// Instantiates a client
const translate = new Translate.v2.Translate({
@ -971,7 +991,7 @@ export class GBConversationalService {
return Promise.reject(new Error(msg));
}
} else {
} else {
let url = urlJoin(
endPoint,
'translate');
@ -1008,7 +1028,7 @@ export class GBConversationalService {
return Promise.reject(new Error(msg));
}
}
}
}
public static async handleText(min, user, step, text: string) {
@ -1127,7 +1147,7 @@ export class GBConversationalService {
GBLog.info(`After (processMessageActivity): ${text}.`);
return text;
}
}
public async prompt(min: GBMinInstance, step: GBDialogStep, text: string) {
let sec = new SecService();
@ -1148,17 +1168,17 @@ export class GBConversationalService {
await this.sendText(min, step, text);
return await step.prompt('textPrompt', {});
}
}
}
public async sendText(min: GBMinInstance, step, text) {
await this['sendTextWithOptions'](min, step, text, true, null);
}
}
public async sendTextWithOptions(min: GBMinInstance, step, text, translate, keepTextList) {
let sec = new SecService();
let user = await sec.getUserFromSystemId(step.context.activity.from.id);
await this['sendTextWithOptionsAndUser'](min, user, step, text, true, null);
}
}
public async sendTextWithOptionsAndUser(min: GBMinInstance, user, step, text, translate, keepTextList) {
const member = step ? step.context.activity.from : null;
@ -1210,7 +1230,7 @@ export class GBConversationalService {
} else {
await step.context.sendActivity(text);
}
}
}
public async broadcast(min: GBMinInstance, message: string) {
GBLog.info(`Sending broadcast notifications...`);
@ -1223,7 +1243,7 @@ export class GBConversationalService {
GBLog.info(`User: ${user.Id} with no conversation ID while broadcasting.`);
}
});
}
}
/**
*
@ -1248,7 +1268,7 @@ export class GBConversationalService {
console.log(error);
}
}
}
}
public static kmpSearch(pattern, text) {
pattern = pattern.toLowerCase();
@ -1274,5 +1294,5 @@ export class GBConversationalService {
}
}
return -1; // Not found
}
}
}

View file

@ -74,6 +74,8 @@ export class SMSAuthDialog extends IGBDialog {
mobile,
Messages[locale].please_use_code(code)
);
await min.conversationalService.sendSms(min, mobile, Messages[locale].please_use_code(code));
return await min.conversationalService.prompt(min, step, Messages[locale].confirm_mobile);
},
async (step) => {

View file

@ -53,7 +53,7 @@ import e from 'express';
import { GBUtil } from '../../../src/util.js';
const { WAState, List, Buttons, Client, MessageMedia } = pkg;
import twilio from 'twilio';
const { MessagingResponse } = twilio.twiml;
/**
* Support for Whatsapp.