fix(whatsapp.gblib): #288 fix list and buttons.
This commit is contained in:
parent
b96a78e810
commit
6f62453f4e
4 changed files with 149 additions and 155 deletions
|
@ -172,10 +172,10 @@
|
||||||
"vm2-process": "2.1.1",
|
"vm2-process": "2.1.1",
|
||||||
"walk-promise": "0.2.0",
|
"walk-promise": "0.2.0",
|
||||||
"washyourmouthoutwithsoap": "1.0.2",
|
"washyourmouthoutwithsoap": "1.0.2",
|
||||||
"whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#fix-buttons-list",
|
"whatsapp-web.js": "github:meetscrm/whatsapp-web.js#8a46b65e2284ae2b031b5a9217e33ec2bab2e579",
|
||||||
"winston": "3.8.2",
|
"winston": "3.8.2",
|
||||||
"winston-logs-display": "1.0.0",
|
"winston-logs-display": "1.0.0",
|
||||||
"ws": "^8.12.1",
|
"ws": "8.12.1",
|
||||||
"yarn": "1.22.19"
|
"yarn": "1.22.19"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -46,20 +46,19 @@ import { Messages } from '../strings.js';
|
||||||
import * as Fs from 'fs';
|
import * as Fs from 'fs';
|
||||||
import { CollectionUtil } from 'pragmatismo-io-framework';
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
||||||
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService.js';
|
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService.js';
|
||||||
import { GuaribasUser } from '../../security.gbapp/models/index.js';
|
|
||||||
import phoneUtil from 'google-libphonenumber';
|
import phoneUtil from 'google-libphonenumber';
|
||||||
import phone from 'phone';
|
import phone from 'phone';
|
||||||
import DateDiff from 'date-diff';
|
import DateDiff from 'date-diff';
|
||||||
import { Buttons, List } from 'whatsapp-web.js';
|
|
||||||
import tesseract from 'node-tesseract-ocr';
|
import tesseract from 'node-tesseract-ocr';
|
||||||
import Path from 'path';
|
import Path from 'path';
|
||||||
import sgMail from '@sendgrid/mail';
|
import sgMail from '@sendgrid/mail';
|
||||||
import mammoth from 'mammoth';
|
import mammoth from 'mammoth';
|
||||||
import qrcode from 'qrcode';
|
import qrcode from 'qrcode';
|
||||||
import { json } from 'body-parser';
|
|
||||||
import { WebAutomationServices } from './WebAutomationServices.js';
|
import { WebAutomationServices } from './WebAutomationServices.js';
|
||||||
import urljoin from 'url-join';
|
import urljoin from 'url-join';
|
||||||
import QrScanner from 'qr-scanner';
|
import QrScanner from 'qr-scanner';
|
||||||
|
import pkg from 'whatsapp-web.js';
|
||||||
|
const { List, Buttons } = pkg;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default check interval for user replay
|
* Default check interval for user replay
|
||||||
|
@ -70,7 +69,6 @@ const DEFAULT_HEAR_POLL_INTERVAL = 500;
|
||||||
* Base services of conversation to be called by BASIC.
|
* Base services of conversation to be called by BASIC.
|
||||||
*/
|
*/
|
||||||
export class DialogKeywords {
|
export class DialogKeywords {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Data = [10,20,30]
|
* Data = [10,20,30]
|
||||||
|
@ -84,7 +82,7 @@ export class DialogKeywords {
|
||||||
* @param legends
|
* @param legends
|
||||||
* @see https://www.npmjs.com/package/plot
|
* @see https://www.npmjs.com/package/plot
|
||||||
*/
|
*/
|
||||||
public async chart({pid, type, data, legends, transpose }) {
|
public async chart({ pid, type, data, legends, transpose }) {
|
||||||
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
||||||
let table = [[]];
|
let table = [[]];
|
||||||
|
|
||||||
|
@ -169,7 +167,7 @@ export class DialogKeywords {
|
||||||
*
|
*
|
||||||
* @example x = TODAY
|
* @example x = TODAY
|
||||||
*/
|
*/
|
||||||
public async getToday({pid}) {
|
public async getToday({ pid }) {
|
||||||
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
||||||
let d = new Date(),
|
let d = new Date(),
|
||||||
month = '' + (d.getMonth() + 1),
|
month = '' + (d.getMonth() + 1),
|
||||||
|
@ -206,28 +204,28 @@ export class DialogKeywords {
|
||||||
*
|
*
|
||||||
* @example EXIT
|
* @example EXIT
|
||||||
*/
|
*/
|
||||||
public async exit({ }) { }
|
public async exit({}) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get active tasks.
|
* Get active tasks.
|
||||||
*
|
*
|
||||||
* @example list = ACTIVE TASKS
|
* @example list = ACTIVE TASKS
|
||||||
*/
|
*/
|
||||||
public async getActiveTasks({ pid }) { }
|
public async getActiveTasks({ pid }) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new deal.
|
* Creates a new deal.
|
||||||
*
|
*
|
||||||
* @example CREATE DEAL dealname,contato,empresa,amount
|
* @example CREATE DEAL dealname,contato,empresa,amount
|
||||||
*/
|
*/
|
||||||
public async createDeal({ pid, dealName, contact, company, amount }) { }
|
public async createDeal({ pid, dealName, contact, company, amount }) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds contacts in XRM.
|
* Finds contacts in XRM.
|
||||||
*
|
*
|
||||||
* @example list = FIND CONTACT "Sandra"
|
* @example list = FIND CONTACT "Sandra"
|
||||||
*/
|
*/
|
||||||
public async fndContact({ pid, name }) { }
|
public async fndContact({ pid, name }) {}
|
||||||
|
|
||||||
public getContentLocaleWithCulture(contentLocale) {
|
public getContentLocaleWithCulture(contentLocale) {
|
||||||
switch (contentLocale) {
|
switch (contentLocale) {
|
||||||
|
@ -258,7 +256,7 @@ export class DialogKeywords {
|
||||||
* @example day = WEEKDAY (date)
|
* @example day = WEEKDAY (date)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async getWeekFromDate({pid, date}) {
|
public async getWeekFromDate({ pid, date }) {
|
||||||
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
||||||
const contentLocale = min.core.getParam(
|
const contentLocale = min.core.getParam(
|
||||||
min.instance,
|
min.instance,
|
||||||
|
@ -431,7 +429,7 @@ export class DialogKeywords {
|
||||||
* @example SAVE "contacts.xlsx", name, email, NOW
|
* @example SAVE "contacts.xlsx", name, email, NOW
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async getNow({pid}) {
|
public async getNow({ pid }) {
|
||||||
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
||||||
const contentLocale = min.core.getParam(
|
const contentLocale = min.core.getParam(
|
||||||
min.instance,
|
min.instance,
|
||||||
|
@ -623,16 +621,16 @@ export class DialogKeywords {
|
||||||
await this.setOption({ pid, name: 'wholeWord', value: value });
|
await this.setOption({ pid, name: 'wholeWord', value: value });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the FIND behaviour to consider whole words while searching.
|
* Defines the FIND behaviour to consider whole words while searching.
|
||||||
*
|
*
|
||||||
* @example SET FILTER TYPE date, string
|
* @example SET FILTER TYPE date, string
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async setFilterTypes({ pid, types }) {
|
public async setFilterTypes({ pid, types }) {
|
||||||
const value = types;
|
const value = types;
|
||||||
await this.setOption({ pid, name: 'filterTypes', value: value });
|
await this.setOption({ pid, name: 'filterTypes', value: value });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the theme for assets generation.
|
* Defines the theme for assets generation.
|
||||||
|
@ -678,7 +676,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
* @example MENU
|
* @example MENU
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async showMenu({ }) {
|
public async showMenu({}) {
|
||||||
// https://github.com/GeneralBots/BotServer/issues/237
|
// https://github.com/GeneralBots/BotServer/issues/237
|
||||||
// return await beginDialog('/menu');
|
// return await beginDialog('/menu');
|
||||||
}
|
}
|
||||||
|
@ -704,15 +702,16 @@ public async setFilterTypes({ pid, types }) {
|
||||||
* @example HEAR name
|
* @example HEAR name
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async hear({ pid, kind, arg }) {
|
public async hear({ pid, kind, args }) {
|
||||||
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
||||||
|
|
||||||
// Handles first arg as an array of args.
|
// Handles first arg as an array of args.
|
||||||
|
|
||||||
let args = [];
|
let args1 = [];
|
||||||
if (arg && arg.length) {
|
if (args && args.length) {
|
||||||
args = arg;
|
args1 = args;
|
||||||
}
|
}
|
||||||
|
args = args1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isIntentYes = (locale, utterance) => {
|
const isIntentYes = (locale, utterance) => {
|
||||||
|
@ -736,28 +735,25 @@ public async setFilterTypes({ pid, types }) {
|
||||||
// https://github.com/GeneralBots/BotServer/issues/266
|
// https://github.com/GeneralBots/BotServer/issues/266
|
||||||
|
|
||||||
if (args && args.length > 1) {
|
if (args && args.length > 1) {
|
||||||
// https://github.com/pedroslopez/whatsapp-web.js/issues/1811
|
let i = 0;
|
||||||
//
|
|
||||||
// const list = new List(
|
|
||||||
// 'Escolha um dos itens',
|
|
||||||
// 'Itens1',
|
|
||||||
// [
|
|
||||||
// {
|
|
||||||
// title: 'Itens2',
|
|
||||||
// rows: []
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// 'Please select a product'
|
|
||||||
// );
|
|
||||||
// let i = 0;
|
|
||||||
// await CollectionUtil.asyncForEach(args, async arg => {
|
|
||||||
// i++;
|
|
||||||
// list.sections[0].rows.push({ title: arg, id: `button${i}` });
|
|
||||||
// await this.getTalk(arg);
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const button = new wpp.Buttons(Messages[locale].choices, choices, ' ', ' ');
|
if (args.length > 3) {
|
||||||
// await this.getTalk(button);
|
let section = { title: '', rows: [] };
|
||||||
|
await CollectionUtil.asyncForEach(args, async arg => {
|
||||||
|
i++;
|
||||||
|
section.rows.push({ title: arg, id: `button${i}` });
|
||||||
|
});
|
||||||
|
const list = new List('Select:', '', [section], '', '');
|
||||||
|
await this.talk({ pid: pid, text: list });
|
||||||
|
} else {
|
||||||
|
let buttons = [];
|
||||||
|
await CollectionUtil.asyncForEach(args, async arg => {
|
||||||
|
i++;
|
||||||
|
buttons.push({ body: arg, id: `button${i}` });
|
||||||
|
});
|
||||||
|
let button = new Buttons('Select:', buttons, '', 'General Bots');
|
||||||
|
await this.talk({ pid: pid, text: button });
|
||||||
|
}
|
||||||
|
|
||||||
GBLog.info(`BASIC: HEAR with [${args.toString()}] (Asking for input).`);
|
GBLog.info(`BASIC: HEAR with [${args.toString()}] (Asking for input).`);
|
||||||
} else {
|
} else {
|
||||||
|
@ -781,7 +777,6 @@ public async setFilterTypes({ pid, types }) {
|
||||||
const answer = min.cbMap[userId].promise;
|
const answer = min.cbMap[userId].promise;
|
||||||
|
|
||||||
if (kind === 'sheet') {
|
if (kind === 'sheet') {
|
||||||
|
|
||||||
// Retrieves the .xlsx file associated with the HEAR var AS file.xlsx.
|
// Retrieves the .xlsx file associated with the HEAR var AS file.xlsx.
|
||||||
|
|
||||||
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
||||||
|
@ -795,10 +790,10 @@ public async setFilterTypes({ pid, types }) {
|
||||||
// Finds .xlsx specified by arg.
|
// Finds .xlsx specified by arg.
|
||||||
|
|
||||||
const document = res.value.filter(m => {
|
const document = res.value.filter(m => {
|
||||||
return m.name === arg;
|
return m.name === args;
|
||||||
});
|
});
|
||||||
if (document === undefined || document.length === 0) {
|
if (document === undefined || document.length === 0) {
|
||||||
GBLog.info(`${arg} not found on .gbdata folder, check the package.`);
|
GBLog.info(`${args} not found on .gbdata folder, check the package.`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -818,8 +813,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
for (; index < results.text.length; index++) {
|
for (; index < results.text.length; index++) {
|
||||||
if (results.text[index][0] !== '') {
|
if (results.text[index][0] !== '') {
|
||||||
list.push(results.text[index][0]);
|
list.push(results.text[index][0]);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -837,9 +831,8 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (result === null) {
|
if (result === null) {
|
||||||
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (kind === 'file') {
|
} else if (kind === 'file') {
|
||||||
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
|
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
|
||||||
const handle = WebAutomationServices.cyrb53(min.botId + answer.filename);
|
const handle = WebAutomationServices.cyrb53(min.botId + answer.filename);
|
||||||
|
@ -860,7 +853,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null) {
|
if (value === null) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um e-mail válido.' });
|
await this.talk({ pid, text: 'Por favor, digite um e-mail válido.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value;
|
result = value;
|
||||||
|
@ -873,7 +866,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null || value.length != 1) {
|
if (value === null || value.length != 1) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um nome válido.' });
|
await this.talk({ pid, text: 'Por favor, digite um nome válido.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value;
|
result = value;
|
||||||
|
@ -886,7 +879,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null || value.length != 1) {
|
if (value === null || value.length != 1) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um número válido.' });
|
await this.talk({ pid, text: 'Por favor, digite um número válido.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value;
|
result = value;
|
||||||
|
@ -901,7 +894,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null || value.length != 1) {
|
if (value === null || value.length != 1) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite uma data no formato 12/12/2020.' });
|
await this.talk({ pid, text: 'Por favor, digite uma data no formato 12/12/2020.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value;
|
result = value;
|
||||||
|
@ -914,7 +907,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null || value.length != 1) {
|
if (value === null || value.length != 1) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um horário no formato hh:ss.' });
|
await this.talk({ pid, text: 'Por favor, digite um horário no formato hh:ss.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value;
|
result = value;
|
||||||
|
@ -933,7 +926,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null || value.length != 1) {
|
if (value === null || value.length != 1) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um valor monetário.' });
|
await this.talk({ pid, text: 'Por favor, digite um valor monetário.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value;
|
result = value;
|
||||||
|
@ -946,21 +939,22 @@ public async setFilterTypes({ pid, types }) {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await this.talk({ pid, text: Messages[locale].validation_enter_valid_mobile });
|
await this.talk({ pid, text: Messages[locale].validation_enter_valid_mobile });
|
||||||
|
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
if (!phoneUtil.isPossibleNumber(phoneNumber)) {
|
if (!phoneUtil.isPossibleNumber(phoneNumber)) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um número de telefone válido.' });
|
await this.talk({ pid, text: 'Por favor, digite um número de telefone válido.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = phoneNumber;
|
result = phoneNumber;
|
||||||
} else if (kind === 'qr-scanner'){
|
} else if (kind === 'qr-scanner') {
|
||||||
//https://github.com/GeneralBots/BotServer/issues/171
|
//https://github.com/GeneralBots/BotServer/issues/171
|
||||||
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
|
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
|
||||||
const handle = WebAutomationServices.cyrb53(min.botId + answer.filename);
|
const handle = WebAutomationServices.cyrb53(min.botId + answer.filename);
|
||||||
GBServer.globals.files[handle] = answer;
|
GBServer.globals.files[handle] = answer;
|
||||||
QrScanner.scanImage(GBServer.globals.files[handle]).then(result => console.log(result)).catch(error => console.log(error || 'no QR code found.'));
|
QrScanner.scanImage(GBServer.globals.files[handle])
|
||||||
|
.then(result => console.log(result))
|
||||||
|
.catch(error => console.log(error || 'no QR code found.'));
|
||||||
} else if (kind === 'zipcode') {
|
} else if (kind === 'zipcode') {
|
||||||
const extractEntity = (text: string) => {
|
const extractEntity = (text: string) => {
|
||||||
text = text.replace(/\-/gi, '');
|
text = text.replace(/\-/gi, '');
|
||||||
|
@ -977,7 +971,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (value === null || value.length != 1) {
|
if (value === null || value.length != 1) {
|
||||||
await this.talk({ pid, text: 'Por favor, digite um CEP válido.' });
|
await this.talk({ pid, text: 'Por favor, digite um CEP válido.' });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
|
|
||||||
result = value[0];
|
result = value[0];
|
||||||
|
@ -992,7 +986,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (result === null) {
|
if (result === null) {
|
||||||
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
} else if (kind === 'language') {
|
} else if (kind === 'language') {
|
||||||
result = null;
|
result = null;
|
||||||
|
@ -1024,7 +1018,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
|
|
||||||
if (result === null) {
|
if (result === null) {
|
||||||
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
||||||
return await this.hear({ pid, kind, arg });
|
return await this.hear({ pid, kind, args });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -1146,7 +1140,7 @@ public async setFilterTypes({ pid, types }) {
|
||||||
await min.conversationalService.sendFile(min, null, mobile, url, caption);
|
await min.conversationalService.sendFile(min, null, mobile, url, caption);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Generates a new QRCode.
|
* Generates a new QRCode.
|
||||||
*
|
*
|
||||||
* file = QRCODE "data"
|
* file = QRCODE "data"
|
||||||
|
@ -1163,6 +1157,6 @@ public async setFilterTypes({ pid, types }) {
|
||||||
Fs.writeFileSync(localName, buf, { encoding: null });
|
Fs.writeFileSync(localName, buf, { encoding: null });
|
||||||
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));
|
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));
|
||||||
|
|
||||||
return {data: data, localName: localName, url: url};
|
return { data: data, localName: localName, url: url };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,6 +63,7 @@ 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';
|
||||||
import * as marked from 'marked';
|
import * as marked from 'marked';
|
||||||
import Translate from '@google-cloud/translate';
|
import Translate from '@google-cloud/translate';
|
||||||
|
import { List } from 'whatsapp-web.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides basic services for handling messages and dispatching to back-end
|
* Provides basic services for handling messages and dispatching to back-end
|
||||||
|
@ -1087,8 +1088,10 @@ export class GBConversationalService {
|
||||||
*
|
*
|
||||||
* Sends a message in a user with an already started conversation (got ConversationReference set)
|
* Sends a message in a user with an already started conversation (got ConversationReference set)
|
||||||
*/
|
*/
|
||||||
public async sendOnConversation(min: GBMinInstance, user: GuaribasUser, message: string) {
|
public async sendOnConversation(min: GBMinInstance, user: GuaribasUser, message: any) {
|
||||||
if (user.conversationReference.startsWith('spaces')) {
|
if (message['buttons'] || message['sections']) {
|
||||||
|
await min['whatsAppDirectLine'].sendToDevice(user.userSystemId, message, user.conversationReference );
|
||||||
|
} else if (user.conversationReference.startsWith('spaces')) {
|
||||||
await min['googleDirectLine'].sendToDevice(user.userSystemId, null, user.conversationReference, message);
|
await min['googleDirectLine'].sendToDevice(user.userSystemId, null, user.conversationReference, message);
|
||||||
} else {
|
} else {
|
||||||
const ref = JSON.parse(user.conversationReference);
|
const ref = JSON.parse(user.conversationReference);
|
||||||
|
|
|
@ -45,12 +45,9 @@ import { GBMinService } from '../../core.gbapp/services/GBMinService.js';
|
||||||
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
|
||||||
import qrcode from 'qrcode-terminal';
|
import qrcode from 'qrcode-terminal';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
|
|
||||||
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
|
|
||||||
import { method } from 'lodash';
|
|
||||||
import pkg from 'whatsapp-web.js';
|
|
||||||
import { GBSSR } from '../../core.gbapp/services/GBSSR.js';
|
import { GBSSR } from '../../core.gbapp/services/GBSSR.js';
|
||||||
const { Buttons, Client, MessageMedia } = pkg;
|
import pkg from 'whatsapp-web.js';
|
||||||
|
const { List, Buttons, Client, MessageMedia } = pkg;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Support for Whatsapp.
|
* Support for Whatsapp.
|
||||||
|
@ -116,13 +113,17 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendButton() {
|
public async sendButton(number) {
|
||||||
let url = '';
|
let url = 'https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31';
|
||||||
const media = await MessageMedia.fromUrl(url);
|
const media = await MessageMedia.fromUrl(url, {unsafeMime: true});
|
||||||
media.mimetype = 'image/png';
|
media.mimetype = 'image/png';
|
||||||
media.filename = 'hello.png';
|
media.filename = 'hello.png';
|
||||||
let btnClickableMenu = new Buttons(media as any, [{ id: 'customId', body: 'button1' }, { body: 'button2' }]);
|
let btnClickableMenu = new Buttons(media as any, [
|
||||||
await this.sendToDevice('', btnClickableMenu as any, null);
|
{ id: 'customId', body: 'button1' }, { body: 'button2' },
|
||||||
|
{ body: 'button3' }, { body: 'button4' },
|
||||||
|
{ body: 'button4' }, { body: 'button6' },
|
||||||
|
]);
|
||||||
|
await this.sendToDevice(number, btnClickableMenu as any, null);
|
||||||
}
|
}
|
||||||
public async setup(setUrl: boolean) {
|
public async setup(setUrl: boolean) {
|
||||||
const client = await new SwaggerClient({
|
const client = await new SwaggerClient({
|
||||||
|
@ -140,10 +141,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
const minBoot = GBServer.globals.minBoot;
|
const minBoot = GBServer.globals.minBoot;
|
||||||
// TODO: REMOVE THIS.
|
|
||||||
if (!setUrl) {
|
|
||||||
this.customClient = minBoot.whatsAppDirectLine.customClient;
|
|
||||||
} else {
|
|
||||||
// Initialize the browser using a local profile for each bot.
|
// Initialize the browser using a local profile for each bot.
|
||||||
const gbaiName = `${this.min.botId}.gbai`;
|
const gbaiName = `${this.min.botId}.gbai`;
|
||||||
const localName = Path.join('work', gbaiName, 'profile');
|
const localName = Path.join('work', gbaiName, 'profile');
|
||||||
|
@ -183,6 +180,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}).bind(this)
|
}).bind(this)
|
||||||
);
|
);
|
||||||
client.on('authenticated', async () => {
|
client.on('authenticated', async () => {
|
||||||
|
|
||||||
GBLog.verbose(`GBWhatsApp: QR Code authenticated for ${this.botId}.`);
|
GBLog.verbose(`GBWhatsApp: QR Code authenticated for ${this.botId}.`);
|
||||||
});
|
});
|
||||||
client.on('ready', async () => {
|
client.on('ready', async () => {
|
||||||
|
@ -199,9 +197,9 @@ export class WhatsappDirectLine extends GBService {
|
||||||
const wait = Math.floor(Math.random() * 5000) + 1000;
|
const wait = Math.floor(Math.random() * 5000) + 1000;
|
||||||
await sleep(wait);
|
await sleep(wait);
|
||||||
if (chat.isGroup) {
|
if (chat.isGroup) {
|
||||||
await chat.clearMessages();
|
// await chat.clearMessages();
|
||||||
} else if (!chat.pinned) {
|
} else if (!chat.pinned) {
|
||||||
await chat.delete();
|
// await chat.delete();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -209,7 +207,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
};
|
};
|
||||||
createClient.bind(this)();
|
createClient.bind(this)();
|
||||||
setUrl = false;
|
setUrl = false;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'chatapi':
|
case 'chatapi':
|
||||||
options = {
|
options = {
|
||||||
|
@ -226,7 +223,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case 'chatapi':
|
case 'official':
|
||||||
url = urlJoin(this.whatsappServiceUrl, 'webhook');
|
url = urlJoin(this.whatsappServiceUrl, 'webhook');
|
||||||
options = {
|
options = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
Loading…
Add table
Reference in a new issue