new(whatsapp.gblib): New WhatsApp provider: Meta.
This commit is contained in:
parent
37d9028f98
commit
bac90cbff8
3 changed files with 130 additions and 40 deletions
|
@ -63,6 +63,8 @@ import * as marked from 'marked';
|
|||
import Translate from '@google-cloud/translate';
|
||||
import { GBUtil } from '../../../src/util.js';
|
||||
import { GBLogEx } from './GBLogEx.js';
|
||||
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
|
||||
|
||||
|
||||
/**
|
||||
* Provides basic services for handling messages and dispatching to back-end
|
||||
|
@ -425,7 +427,7 @@ export class GBConversationalService {
|
|||
}
|
||||
}
|
||||
|
||||
public async sendToMobile(min: GBMinInstance, mobile: string, message: string, conversationId) {
|
||||
public async sendToMobile(min: GBMinInstance, mobile: string, message: any, conversationId) {
|
||||
GBLogEx.info(min, `Sending message ${message} to ${mobile}...`);
|
||||
return min.whatsAppDirectLine ? await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId) :
|
||||
await this.sendSms(min, mobile, message);
|
||||
|
@ -646,6 +648,47 @@ export class GBConversationalService {
|
|||
});
|
||||
}
|
||||
|
||||
public async fillAndBroadcastTemplate(min: GBMinInstance, step: GBDialogStep, mobile: string, text) {
|
||||
|
||||
let image = /(.*)\n/gmi.exec(text)[0].trim();
|
||||
|
||||
const gbaiName = DialogKeywords.getGBAIPath(min.botId);
|
||||
|
||||
const fileUrl = urlJoin(process.env.BOT_URL,'kb', gbaiName, `${min.botId}.gbkb`, 'images', image);
|
||||
|
||||
let urlImage = image.startsWith('http')
|
||||
? image
|
||||
: fileUrl;
|
||||
|
||||
text = text.substring(image.length).trim();
|
||||
let data = {
|
||||
name: 'broadcast', components: [
|
||||
{
|
||||
type: "header",
|
||||
parameters: [
|
||||
{
|
||||
type: "image",
|
||||
image: {
|
||||
link: urlImage,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "body",
|
||||
parameters: [
|
||||
{
|
||||
type: "text",
|
||||
text: text,
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
GBLogEx.info(min, `Sending answer file to mobile: ${mobile}.`);
|
||||
await this.sendToMobile(min, mobile, data, null);
|
||||
}
|
||||
// tslint:enable:no-unsafe-any
|
||||
|
||||
public async sendMarkdownToMobile(min: GBMinInstance, step: GBDialogStep, mobile: string, text: string) {
|
||||
|
|
|
@ -743,6 +743,26 @@ export class KBService implements IGBKBService {
|
|||
});
|
||||
}
|
||||
} else if (file !== null && file.name.endsWith('.docx')) {
|
||||
const path = DialogKeywords.getGBAIPath(instance.botId, `gbkb`);
|
||||
const localName = Path.join('work', path, 'articles', file.name);
|
||||
let loader = new DocxLoader(localName);
|
||||
let doc = await loader.load();
|
||||
|
||||
const answer = {
|
||||
instanceId: instance.instanceId,
|
||||
content: doc[0].pageContent,
|
||||
format: '.md',
|
||||
media: file.name,
|
||||
packageId: packageId,
|
||||
prevId: 0
|
||||
};
|
||||
|
||||
data.answers.push(answer);
|
||||
|
||||
|
||||
|
||||
|
||||
} else if (file !== null && file.name.endsWith('.toc.docx')) {
|
||||
const path = DialogKeywords.getGBAIPath(instance.botId, `gbkb`);
|
||||
const localName = Path.join('work', path, 'articles', file.name);
|
||||
const buffer = Fs.readFileSync(localName, { encoding: null });
|
||||
|
|
|
@ -142,8 +142,10 @@ export class WhatsappDirectLine extends GBService {
|
|||
const createClient = () => {
|
||||
const client = (this.customClient = new Client({
|
||||
puppeteer: GBSSR.preparePuppeteer(localName)
|
||||
, webVersionCache: { type: 'remote',
|
||||
remotePath: `https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${webVersion}.html` }
|
||||
, webVersionCache: {
|
||||
type: 'remote',
|
||||
remotePath: `https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${webVersion}.html`
|
||||
}
|
||||
}));
|
||||
client.on(
|
||||
'message',
|
||||
|
@ -179,7 +181,8 @@ export class WhatsappDirectLine extends GBService {
|
|||
}
|
||||
|
||||
if (adminEmail) {
|
||||
await s.sendEmail({pid, to: adminEmail, subject: `Check your WhatsApp for bot ${this.min.botId}`,
|
||||
await s.sendEmail({
|
||||
pid, to: adminEmail, subject: `Check your WhatsApp for bot ${this.min.botId}`,
|
||||
body: msg
|
||||
});
|
||||
}
|
||||
|
@ -262,8 +265,14 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
switch (provider) {
|
||||
case 'meta':
|
||||
if (req.body.entry[0].changes[0].value.messages[0].text) {
|
||||
text = req.body.entry[0].changes[0].value.messages[0].text.body;
|
||||
}
|
||||
else {
|
||||
text = req.body.entry[0].changes[0].value.messages[0].button.text;
|
||||
}
|
||||
|
||||
from = req.body.entry[0].changes[0].value.messages[0].from;
|
||||
text = req.body.entry[0].changes[0].value.messages[0].text.body
|
||||
to = this.min.core.getParam<string>(this.min.instance, 'Bot Number', null);
|
||||
fromName = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
||||
|
||||
|
@ -710,7 +719,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
await this.sendFileToDevice(to, url, 'Audio', msg, chatId);
|
||||
}
|
||||
|
||||
public async sendToDevice(to: string, msg: string, conversationId) {
|
||||
public async sendToDevice(to: any, msg: string, conversationId) {
|
||||
const cmd = '/audio ';
|
||||
let url;
|
||||
let chatId = WhatsappDirectLine.chatIds[conversationId];
|
||||
|
@ -725,14 +734,23 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
switch (this.provider) {
|
||||
case 'meta':
|
||||
const bot = createBot(this.min.instance.whatsappServiceNumber,
|
||||
const driver = createBot(this.min.instance.whatsappServiceNumber,
|
||||
this.min.instance.whatsappServiceKey);
|
||||
|
||||
if (msg['name']) {
|
||||
const res = await driver.sendTemplate(to, msg['name'], 'pt_br', msg['components']);
|
||||
console.log(JSON.stringify(res));
|
||||
}
|
||||
else {
|
||||
|
||||
messages = msg.match(/(.|[\r\n]){1,4096}/g)
|
||||
|
||||
await CollectionUtil.asyncForEach(messages, async msg => {
|
||||
await GBUtil.sleep(3000);
|
||||
await bot.sendText(to, msg);
|
||||
const res =await driver.sendText(to, msg);
|
||||
console.log(JSON.stringify(res));
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
case 'official':
|
||||
|
@ -806,14 +824,23 @@ export class WhatsappDirectLine extends GBService {
|
|||
switch (provider) {
|
||||
|
||||
case 'meta':
|
||||
|
||||
if (req.body.entry[0].changes[0].value.statuses) {
|
||||
GBLogEx.info(this.min, `WhatsApp: ${req.body.entry[0].changes[0].value.statuses[0].status}.`);
|
||||
GBLogEx.info(this.min, `WhatsApp:${id} ${senderName} ${req.body.entry[0].changes[0].value.statuses[0].status}.`);
|
||||
res.status(200);
|
||||
res.end();
|
||||
|
||||
return;
|
||||
}
|
||||
if (req.body.entry[0].changes[0].value.messages[0].text) {
|
||||
text = req.body.entry[0].changes[0].value.messages[0].text.body;
|
||||
}
|
||||
else {
|
||||
text = req.body.entry[0].changes[0].value.messages[0].button.text;
|
||||
}
|
||||
id = req.body.entry[0].changes[0].value.messages[0].from;
|
||||
text = req.body.entry[0].changes[0].value.messages[0].text.body
|
||||
senderName = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
||||
|
||||
botId = this.botId;
|
||||
|
||||
break;
|
||||
|
|
Loading…
Add table
Reference in a new issue