new(WhatsApp.gblib): Official Twilio driver.

This commit is contained in:
Rodrigo Rodriguez 2024-03-08 17:10:13 -03:00
parent 6217d3f8cd
commit cae8408b59
4 changed files with 40 additions and 3 deletions

View file

@ -199,6 +199,7 @@
"tabulator-tables": "5.4.2",
"tedious": "15.1.2",
"textract": "2.5.0",
"twilio": "^4.23.0",
"twitter-api-v2": "1.12.9",
"typescript": "4.9.5",
"url-join": "5.0.0",

View file

@ -446,6 +446,10 @@ export class GBMinService {
if (!res) {
return 'GeneralBots';
}
if (req.NumMedia)
{
return 'Official';
}
return req.body.phone_id ? 'maytapi' : 'chatapi';
}

View file

@ -165,7 +165,7 @@ export class ChatServices {
const chain = new LLMChain({
memory: windowMemory,
prompt: chatPrompt,
llm: llmWithTools as any,
llm: llm as any,
});
const contextVectorStore = min['vectorStore'];

View file

@ -52,6 +52,8 @@ import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import e from 'express';
import { GBUtil } from '../../../src/util.js';
const { WAState, List, Buttons, Client, MessageMedia } = pkg;
import twilio from 'twilio';
const { MessagingResponse } = twilio.twiml;
/**
* Support for Whatsapp.
@ -104,8 +106,8 @@ export class WhatsappDirectLine extends GBService {
this.provider =
whatsappServiceKey === 'internal'
? 'GeneralBots'
: whatsappServiceNumber.indexOf(';') > -1
? 'maytapi'
: whatsappServiceKey.indexOf('official') > -1
? 'official'
: whatsappServiceKey !== 'internal'
? 'graphapi'
: 'chatapi';
@ -132,6 +134,12 @@ export class WhatsappDirectLine extends GBService {
let options: any;
switch (this.provider) {
case 'Official':
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
this.customClient = twilio(accountSid, authToken);
break;
case 'GeneralBots':
const minBoot = GBServer.globals.minBoot;
// Initialize the browser using a local profile for each bot.
@ -872,6 +880,21 @@ export class WhatsappDirectLine extends GBService {
let options;
switch (this.provider) {
case 'Official':
const botNumber = this.min.core.getParam(this.min.instance, 'Bot Number', null);
if (to.charAt(0) !== '+') {
to = `+${to}`
}
await this.customClient.messages
.create({
body: msg,
from: `whatsapp:${botNumber}`,
to: `whatsapp:${to}`
// TODO: mediaUrl.
});
case 'GeneralBots':
to = to.replace('+', '');
if (to.indexOf('@') == -1) {
@ -953,6 +976,15 @@ export class WhatsappDirectLine extends GBService {
let text;
switch (provider) {
case 'Official':
const { body } = req;
let message;
break;
case 'GeneralBots':
// Ignore E2E messages and status updates.