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