fix(all): fetch calls replaces request packages.
This commit is contained in:
parent
e4fc246b25
commit
ea6c721cb5
11 changed files with 1668 additions and 162 deletions
1547
package-lock.json
generated
1547
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -14,7 +14,7 @@
|
|||
"Dário Vieira <dario.junior3@gmail.com>"
|
||||
],
|
||||
"engines": {
|
||||
"node": "=19.1.0"
|
||||
"node": "=19.2.0"
|
||||
},
|
||||
"license": "AGPL-3.0",
|
||||
"preferGlobal": true,
|
||||
|
@ -143,6 +143,7 @@
|
|||
"swagger-client": "^3.18.5",
|
||||
"tabulator-tables": "5.4.2",
|
||||
"tedious": "15.1.2",
|
||||
"textract": "^2.5.0",
|
||||
"twitter-api-v2": "1.12.9",
|
||||
"typescript": "4.9.3",
|
||||
"typescript-rest-rpc": "^1.0.7",
|
||||
|
|
|
@ -94,7 +94,7 @@ export class GuaribasConversation extends Model<GuaribasConversation> {
|
|||
|
||||
@CreatedAt
|
||||
@Column(DataType.DATE)
|
||||
createdAt: Date;
|
||||
declare createdAt: Date;
|
||||
|
||||
@Column(DataType.STRING(255))
|
||||
text: string;
|
||||
|
@ -126,11 +126,11 @@ export class GuaribasConversationMessage extends Model<GuaribasConversationMessa
|
|||
|
||||
@Column(DataType.DATE)
|
||||
@CreatedAt
|
||||
createdAt: Date;
|
||||
declare createdAt: Date;
|
||||
|
||||
@Column(DataType.DATE)
|
||||
@UpdatedAt
|
||||
updatedAt: Date;
|
||||
declare updatedAt: Date;
|
||||
|
||||
//tslint:disable-next-line:no-use-before-declare
|
||||
@ForeignKey(() => GuaribasConversation)
|
||||
|
|
|
@ -69,9 +69,9 @@ export class GuaribasSchedule extends Model<GuaribasSchedule> {
|
|||
|
||||
@Column(DataType.DATE)
|
||||
@CreatedAt
|
||||
createdAt: Date;
|
||||
declare createdAt: Date;
|
||||
|
||||
@Column(DataType.DATE)
|
||||
@UpdatedAt
|
||||
updatedAt: Date;
|
||||
declare updatedAt: Date;
|
||||
}
|
||||
|
|
|
@ -383,11 +383,8 @@ export class SystemKeywords {
|
|||
* Retrives stock inforation for a given symbol.
|
||||
*/
|
||||
public async getStock ({ symbol }) {
|
||||
var options = {
|
||||
uri: `http://live-nse.herokuapp.com/?symbol=${symbol}`
|
||||
};
|
||||
|
||||
let data = await request.get(options);
|
||||
const url = `http://live-nse.herokuapp.com/?symbol=${symbol}`;
|
||||
let data = await fetch(url);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -716,8 +713,8 @@ export class SystemKeywords {
|
|||
const gbaiName = `${this.min.botId}.gbai`;
|
||||
const localName = Path.join('work', gbaiName, 'cache', `csv${GBAdminService.getRndReadableIdentifier()}.csv`);
|
||||
const url = file['@microsoft.graph.downloadUrl'];
|
||||
const response = await request({ uri: url, encoding: null });
|
||||
Fs.writeFileSync(localName, response, { encoding: null });
|
||||
const response = await fetch(url);
|
||||
Fs.writeFileSync(localName, Buffer.from(await response.arrayBuffer()), { encoding: null });
|
||||
|
||||
var workbook = new Excel.Workbook();
|
||||
const worksheet = await workbook.csv.readFile(localName);
|
||||
|
@ -1282,7 +1279,7 @@ export class SystemKeywords {
|
|||
*
|
||||
*/
|
||||
public async getByHttp ({ url, headers, username, ps, qs, streaming }) {
|
||||
let options = { url: url };
|
||||
let options = { };
|
||||
if (headers) {
|
||||
options['headers'] = headers;
|
||||
}
|
||||
|
@ -1295,14 +1292,14 @@ export class SystemKeywords {
|
|||
if (qs) {
|
||||
options['qs'] = qs;
|
||||
}
|
||||
if (streaming) {
|
||||
if (streaming) { // TODO: Do it with fetch.
|
||||
options['responseType'] = 'stream';
|
||||
options['encoding'] = null;
|
||||
}
|
||||
let result = await request.get(options);
|
||||
let result = await fetch(url, options);
|
||||
|
||||
try {
|
||||
return JSON.parse(result);
|
||||
return JSON.parse(await result.text());
|
||||
} catch (error) {
|
||||
GBLog.info(`[GET]: OK.`);
|
||||
|
||||
|
@ -1321,12 +1318,11 @@ export class SystemKeywords {
|
|||
*/
|
||||
public async putByHttp ({ url, data, headers }) {
|
||||
const options = {
|
||||
uri: url,
|
||||
json: data,
|
||||
headers: headers
|
||||
};
|
||||
|
||||
let result = await request.put(options);
|
||||
let result = await fetch(url, options);
|
||||
GBLog.info(`[PUT]: ${url} (${data}): ${result}`);
|
||||
return typeof result === 'object' ? result : JSON.parse(result);
|
||||
}
|
||||
|
@ -1342,12 +1338,11 @@ export class SystemKeywords {
|
|||
*/
|
||||
public async postByHttp ({ url, data, headers }) {
|
||||
const options = {
|
||||
uri: url,
|
||||
json: data,
|
||||
headers: headers
|
||||
};
|
||||
|
||||
let result = await request.post(options);
|
||||
let result = await fetch(url, options);
|
||||
GBLog.info(`[POST]: ${url} (${data}): ${result}`);
|
||||
|
||||
return result ? (typeof result === 'object' ? result : JSON.parse(result)) : true;
|
||||
|
@ -1375,8 +1370,8 @@ export class SystemKeywords {
|
|||
let template = await this.internalGetDocument(client, baseUrl, path, templateName);
|
||||
const url = template['@microsoft.graph.downloadUrl'];
|
||||
const localName = Path.join('work', gbaiName, 'cache', ``);
|
||||
const response = await request({ uri: url, encoding: null });
|
||||
Fs.writeFileSync(localName, response, { encoding: null });
|
||||
const response = await fetch( url);
|
||||
Fs.writeFileSync(localName, Buffer.from(await response.arrayBuffer()), { encoding: null });
|
||||
|
||||
// Loads the file as binary content.
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ import { join } from 'path';
|
|||
import shell from 'any-shell-escape';
|
||||
import { exec } from 'child_process';
|
||||
import prism from 'prism-media';
|
||||
import request from 'request-promise-native';
|
||||
|
||||
import SpeechToTextV1 from 'ibm-watson/speech-to-text/v1.js';
|
||||
import TextToSpeechV1 from 'ibm-watson/text-to-speech/v1.js';
|
||||
import { IamAuthenticator } from 'ibm-watson/auth/index.js';
|
||||
|
@ -355,27 +355,26 @@ export class GBConversationalService {
|
|||
GBLog.info(`Sending SMS to ${mobile} with text: '${text}'.`);
|
||||
|
||||
if (!min.instance.smsKey && min.instance.smsSecret) {
|
||||
const url = 'http://sms-api.megaconecta.com.br/mt';
|
||||
let options = {
|
||||
method: 'POST',
|
||||
url: 'http://sms-api.megaconecta.com.br/mt',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
authorization: `Bearer ${min.instance.smsSecret}`
|
||||
},
|
||||
body: [
|
||||
{
|
||||
body:
|
||||
JSON.stringify({
|
||||
numero: `${mobile}`,
|
||||
servico: 'short',
|
||||
mensagem: text,
|
||||
parceiro_id: '',
|
||||
codificacao: '0'
|
||||
}
|
||||
],
|
||||
json: true
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
try {
|
||||
const results = await request(options);
|
||||
const results = await fetch(url, options);
|
||||
|
||||
return results;
|
||||
} catch (error) {
|
||||
|
@ -961,30 +960,24 @@ export class GBConversationalService {
|
|||
return Promise.reject(new Error(msg));
|
||||
}
|
||||
} else {
|
||||
const url = urlJoin(endPoint, 'translate', new URLSearchParams({
|
||||
'api-version': '3.0',
|
||||
to: language
|
||||
}).toString());
|
||||
let options = {
|
||||
method: 'POST',
|
||||
baseUrl: endPoint,
|
||||
url: 'translate',
|
||||
qs: {
|
||||
'api-version': '3.0',
|
||||
to: [language]
|
||||
},
|
||||
headers: {
|
||||
'Ocp-Apim-Subscription-Key': key,
|
||||
'Ocp-Apim-Subscription-Region': 'westeurope',
|
||||
'Content-type': 'application/json',
|
||||
'X-ClientTraceId': GBAdminService.generateUuid()
|
||||
},
|
||||
body: [
|
||||
{
|
||||
text: text
|
||||
}
|
||||
],
|
||||
body:text,
|
||||
json: true
|
||||
};
|
||||
|
||||
try {
|
||||
const results = await request(options);
|
||||
const results = await fetch(url, options);
|
||||
|
||||
return results[0].translations[0].text;
|
||||
} catch (error) {
|
||||
|
|
|
@ -40,7 +40,7 @@ import Path from 'path';
|
|||
import express from 'express';
|
||||
import child_process from 'child_process';
|
||||
import rimraf from 'rimraf';
|
||||
import request from 'request-promise-native';
|
||||
|
||||
import vhost from 'vhost';
|
||||
import urlJoin from 'url-join';
|
||||
import Fs from 'fs';
|
||||
|
@ -522,8 +522,8 @@ export class GBDeployer implements IGBDeployer {
|
|||
GBLog.info(`Downloading ${itemPath}...`);
|
||||
const url = item['@microsoft.graph.downloadUrl'];
|
||||
|
||||
const response = await request({ uri: url, encoding: null });
|
||||
Fs.writeFileSync(itemPath, response, { encoding: null });
|
||||
const response = await fetch(url);
|
||||
Fs.writeFileSync(itemPath, Buffer.from(await response.arrayBuffer()), { encoding: null });
|
||||
Fs.utimesSync(itemPath, new Date(), new Date(item.lastModifiedDateTime));
|
||||
} else {
|
||||
GBLog.info(`Local is up to date: ${itemPath}...`);
|
||||
|
|
|
@ -39,9 +39,9 @@ import cliProgress from 'cli-progress';
|
|||
import { DialogSet, TextPrompt } from 'botbuilder-dialogs';
|
||||
import express from 'express';
|
||||
import Swagger from 'swagger-client';
|
||||
import request from 'request-promise-native';
|
||||
|
||||
import removeRoute from 'express-remove-route';
|
||||
import AuthenticationContext from '@azure/msal-node';
|
||||
import AuthenticationContext from 'adal-node';
|
||||
import wash from 'washyourmouthoutwithsoap';
|
||||
import { FacebookAdapter } from 'botbuilder-adapter-facebook';
|
||||
import path from 'path';
|
||||
|
@ -574,8 +574,8 @@ export class GBMinService {
|
|||
* Gets Webchat token from Bot Service.
|
||||
*/
|
||||
private async getWebchatToken (instance: any) {
|
||||
const url = 'https://directline.botframework.com/v3/directline/tokens/generate';
|
||||
const options = {
|
||||
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${instance.webchatKey}`
|
||||
|
@ -583,9 +583,9 @@ export class GBMinService {
|
|||
};
|
||||
|
||||
try {
|
||||
const json = await request(options);
|
||||
const res = await fetch(url, options);
|
||||
|
||||
return JSON.parse(json);
|
||||
return await res.json();
|
||||
} catch (error) {
|
||||
const msg = `[botId:${instance.botId}] Error calling Direct Line to generate a token for Web control: ${error}.`;
|
||||
|
||||
|
@ -598,7 +598,6 @@ export class GBMinService {
|
|||
*/
|
||||
private async getSTSToken (instance: any) {
|
||||
const options = {
|
||||
url: instance.speechEndpoint,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Ocp-Apim-Subscription-Key': instance.speechKey
|
||||
|
@ -606,7 +605,7 @@ export class GBMinService {
|
|||
};
|
||||
|
||||
try {
|
||||
return await request(options);
|
||||
return await fetch(instance.speechEndpoint, options);
|
||||
} catch (error) {
|
||||
const msg = `Error calling Speech to Text client. Error is: ${error}.`;
|
||||
|
||||
|
|
|
@ -153,11 +153,11 @@ export class GuaribasQuestion extends Model<GuaribasQuestion> {
|
|||
|
||||
@Column(DataType.DATE)
|
||||
@CreatedAt
|
||||
createdAt: Date;
|
||||
declare createdAt: Date;
|
||||
|
||||
@Column(DataType.DATE)
|
||||
@UpdatedAt
|
||||
updatedAt: Date;
|
||||
declare updatedAt: Date;
|
||||
|
||||
//tslint:disable-next-line:no-use-before-declare
|
||||
@ForeignKey(() => GuaribasAnswer)
|
||||
|
|
|
@ -62,7 +62,6 @@ import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
|
|||
import { CSService } from '../../customer-satisfaction.gbapp/services/CSService.js';
|
||||
import { GuaribasAnswer, GuaribasQuestion, GuaribasSubject } from '../models/index.js';
|
||||
import { GBConfigService } from './../../core.gbapp/services/GBConfigService.js';
|
||||
import request from 'request-promise-native';
|
||||
import textract from 'textract';
|
||||
import pdf from 'pdf-extraction';
|
||||
|
||||
|
@ -847,15 +846,16 @@ export class KBService implements IGBKBService {
|
|||
}
|
||||
|
||||
public async readComprehension(instanceId: number, doc: string, question: string) {
|
||||
const url =
|
||||
`http://${process.env.GBMODELS_SERVER}/reading-comprehension` +
|
||||
new URLSearchParams({ question: question, key: process.env.GBMODELS_KEY });
|
||||
const form = new FormData();
|
||||
form.append('content', doc);
|
||||
const options = {
|
||||
timeout: 60000 * 5,
|
||||
uri: `http://${process.env.GBMODELS_SERVER}/reading-comprehension`,
|
||||
form: { content: doc },
|
||||
qs: { question: question, key: process.env.GBMODELS_KEY }
|
||||
body: form
|
||||
};
|
||||
|
||||
GBLog.info(`[General Bots Models]: ReadComprehension for ${question}.`);
|
||||
return await request.post(options);
|
||||
return await fetch(url, options);
|
||||
}
|
||||
|
||||
private async getTextFromFile(filename: string) {
|
||||
|
|
|
@ -192,17 +192,22 @@ export class WhatsappDirectLine extends GBService {
|
|||
const s = new DialogKeywords(this.min, null, null);
|
||||
const qrBuf = await s.getQRCode(qr);
|
||||
const gbaiName = `${this.min.botId}.gbai`;
|
||||
const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`);
|
||||
Fs.writeFileSync(localName, qrBuf);
|
||||
const url = urlJoin(
|
||||
GBServer.globals.publicAddress,
|
||||
this.min.botId,
|
||||
const localName = Path.join(
|
||||
'work',
|
||||
gbaiName,
|
||||
'cache',
|
||||
Path.basename(localName)
|
||||
`qr${GBAdminService.getRndReadableIdentifier()}.png`
|
||||
);
|
||||
Fs.writeFileSync(localName, qrBuf);
|
||||
const url = urlJoin(GBServer.globals.publicAddress, this.min.botId, 'cache', Path.basename(localName));
|
||||
GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(
|
||||
adminNumber,
|
||||
url,
|
||||
Path.basename(localName),
|
||||
msg
|
||||
);
|
||||
GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(adminNumber, url, Path.basename(localName), msg);
|
||||
|
||||
s.sendEmail({to:adminEmail, subject:`Check your WhatsApp for bot ${this.botId}`, body:msg);
|
||||
s.sendEmail({ to: adminEmail, subject: `Check your WhatsApp for bot ${this.botId}`, body: msg });
|
||||
}).bind(this)
|
||||
);
|
||||
|
||||
|
@ -250,7 +255,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
break;
|
||||
|
||||
case 'chatapi':
|
||||
url = urlJoin(this.whatsappServiceUrl, 'webhook')
|
||||
url = urlJoin(this.whatsappServiceUrl, 'webhook');
|
||||
options = {
|
||||
method: 'POST',
|
||||
url: url,
|
||||
|
@ -294,9 +299,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
if (options) {
|
||||
try {
|
||||
|
||||
const response: Response = await fetch(url, options);
|
||||
|
||||
} catch (error) {
|
||||
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
||||
}
|
||||
|
@ -314,15 +317,15 @@ export class WhatsappDirectLine extends GBService {
|
|||
return true;
|
||||
default:
|
||||
GBLog.verbose(`GBWhatsapp: Checking server...`);
|
||||
let url = urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`;
|
||||
const options = {
|
||||
url: urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`,
|
||||
url: url,
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
const res = await request(options);
|
||||
const json = JSON.parse(res);
|
||||
|
||||
return json.accountStatus === 'authenticated';
|
||||
const res = await fetch(url, options);
|
||||
const json = (await res.json());
|
||||
return json ['accountStatus'] === 'authenticated';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -484,15 +487,16 @@ export class WhatsappDirectLine extends GBService {
|
|||
}
|
||||
|
||||
if (message.type === 'ptt') {
|
||||
let url = provider ? message.body : message.text;
|
||||
if (process.env.AUDIO_DISABLED !== 'true') {
|
||||
const options = {
|
||||
url: provider ? message.body : message.text,
|
||||
url: url,
|
||||
method: 'GET',
|
||||
encoding: 'binary'
|
||||
};
|
||||
|
||||
const res = await request(options);
|
||||
const buf = Buffer.from(res, 'binary');
|
||||
const res = await fetch(url, options);
|
||||
const buf = Buffer.from(await res.arrayBuffer());
|
||||
text = await GBConversationalService.getTextFromAudioBuffer(
|
||||
this.min.instance.speechKey,
|
||||
this.min.instance.cloudLocation,
|
||||
|
@ -797,7 +801,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
if (options) {
|
||||
try {
|
||||
// tslint:disable-next-line: await-promise
|
||||
const result = await request.post(options);
|
||||
const result = await fetch(url, options);
|
||||
GBLog.info(`File ${url} sent to ${to}: ${result}`);
|
||||
} catch (error) {
|
||||
GBLog.error(`Error sending file to Whatsapp provider ${error.message}`);
|
||||
|
@ -838,7 +842,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
if (options) {
|
||||
try {
|
||||
const result = await request.post(options);
|
||||
const result = await fetch(url, options);
|
||||
GBLog.info(`Audio ${url} sent to ${to}: ${result}`);
|
||||
} catch (error) {
|
||||
GBLog.error(`Error sending audio message to Whatsapp provider ${error.message}`);
|
||||
|
@ -854,7 +858,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
public async sendToDevice(to: string, msg: string, conversationId) {
|
||||
const cmd = '/audio ';
|
||||
|
||||
let url;
|
||||
let chatId = WhatsappDirectLine.chatIds[conversationId];
|
||||
|
||||
if (typeof msg !== 'object' && msg.startsWith(cmd)) {
|
||||
|
@ -895,10 +899,9 @@ export class WhatsappDirectLine extends GBService {
|
|||
case 'maytapi':
|
||||
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
||||
let productId = this.whatsappServiceNumber.split(';')[1];
|
||||
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
|
||||
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
|
||||
|
||||
options = {
|
||||
url: url,
|
||||
method: 'post',
|
||||
json: true,
|
||||
body: { type: 'text', message: msg, to_number: to },
|
||||
|
@ -913,7 +916,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
if (options) {
|
||||
try {
|
||||
GBLog.info(`Message [${msg}] is being sent to ${to}...`);
|
||||
await request.post(options);
|
||||
await fetch(url, options);
|
||||
} catch (error) {
|
||||
GBLog.error(`Error sending message to Whatsapp provider ${error.message}`);
|
||||
|
||||
|
@ -1090,9 +1093,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
} else {
|
||||
await (activeMin as any).whatsAppDirectLine.sendToDevice(
|
||||
id,
|
||||
`Olá! Seja bem-vinda(o)!\nMe chamo ${
|
||||
activeMin.instance.title
|
||||
}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.`,
|
||||
`Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.`,
|
||||
null
|
||||
);
|
||||
if (res) {
|
||||
|
@ -1138,9 +1139,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
activeMin = GBServer.globals.minBoot;
|
||||
await (activeMin as any).whatsAppDirectLine.sendToDevice(
|
||||
id,
|
||||
`O outro Bot que você estava falando(${
|
||||
user.instanceId
|
||||
}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...`
|
||||
`O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...`
|
||||
);
|
||||
}
|
||||
await (activeMin as any).whatsAppDirectLine.received(req, res);
|
||||
|
|
Loading…
Add table
Reference in a new issue