new(whatsapp.gblib): New provider.

This commit is contained in:
Rodrigo Rodriguez 2022-06-10 14:43:13 -03:00
parent 0c967a9f71
commit 49056e9f76

View file

@ -139,7 +139,7 @@ export class WhatsappDirectLine extends GBService {
options = { options = {
url: url, url: url,
method: 'POST', method: 'POST',
body: { webhook: webhook, }, body: { webhook: `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}` },
headers: { headers: {
'x-maytapi-key': this.whatsappServiceKey, 'x-maytapi-key': this.whatsappServiceKey,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -196,7 +196,7 @@ export class WhatsappDirectLine extends GBService {
let answerText = null; let answerText = null;
let text = this.chatapi? message.body : message.text; let text = this.chatapi ? message.body : message.text;
text = text.replace(/\@\d+ /gi, ''); text = text.replace(/\@\d+ /gi, '');
const from = this.chatapi ? req.body.messages[0].author.split('@')[0] : req.body.user.phone; const from = this.chatapi ? req.body.messages[0].author.split('@')[0] : req.body.user.phone;
@ -299,7 +299,7 @@ export class WhatsappDirectLine extends GBService {
if (process.env.AUDIO_DISABLED !== 'true') { if (process.env.AUDIO_DISABLED !== 'true') {
const options = { const options = {
url: this.chatapi?message.body : message.text, url: this.chatapi ? message.body : message.text,
method: 'GET', method: 'GET',
encoding: 'binary' encoding: 'binary'
}; };
@ -316,7 +316,7 @@ export class WhatsappDirectLine extends GBService {
`No momento estou apenas conseguindo ler mensagens de texto.`, null); `No momento estou apenas conseguindo ler mensagens de texto.`, null);
} }
} }
const botId = this.min.instance.botId; const botId = this.min.instance.botId;
const conversationId = WhatsappDirectLine.conversationIds[botId + from + group]; const conversationId = WhatsappDirectLine.conversationIds[botId + from + group];
const client = await this.directLineClient; const client = await this.directLineClient;
@ -541,39 +541,39 @@ export class WhatsappDirectLine extends GBService {
} }
else { else {
// TODO: Attach.
let contents = 0; let contents = 0;
let body = { let body = {
type: 'image', to_number: to,
text: 'Base64 Image Response', type: "media",
message: `data:image/jpeg;base64,${contents}`, message: url,
}; text: caption
};
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`;
options = { options = {
method: 'post', url: `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`,
json: true, method: 'post',
body, json: true,
headers: { body,
'Content-Type': 'application/json', headers: {
'x-maytapi-key': this.whatsappServiceKey, 'Content-Type': 'application/json',
}, 'x-maytapi-key': this.whatsappServiceKey,
}; },
};
} }
try { try {
// tslint:disable-next-line: await-promise // tslint:disable-next-line: await-promise
const result = await request.post(options); const result = await request.post(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}`);
}
} }
}
public async sendAudioToDevice(to, url, chatId) { public async sendAudioToDevice(to, url, chatId) {
const options = { const options = {