From cae8408b5936899e9f5d1e526c8c5e75735725af Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 8 Mar 2024 17:10:13 -0300 Subject: [PATCH] new(WhatsApp.gblib): Official Twilio driver. --- package.json | 1 + packages/core.gbapp/services/GBMinService.ts | 4 +++ packages/gpt.gblib/services/ChatServices.ts | 2 +- .../services/WhatsappDirectLine.ts | 36 +++++++++++++++++-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6c1d02ba..dfbd27db 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 3a168a8c..6894fb94 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -446,6 +446,10 @@ export class GBMinService { if (!res) { return 'GeneralBots'; } + if (req.NumMedia) + { + return 'Official'; + } return req.body.phone_id ? 'maytapi' : 'chatapi'; } diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index c42e65d3..77f7c425 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -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']; diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 8676596c..ca96c218 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -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.