From ce84053d95b8ff69d0175a1da70ec89921493459 Mon Sep 17 00:00:00 2001 From: Alan Date: Thu, 9 Feb 2023 19:40:16 -0300 Subject: [PATCH] feat(whatsapp.gblib): add 'graphapi' option. --- packages/basic.gblib/index.ts | 9 +++- .../services/WhatsappDirectLine.ts | 41 +++++++++++++++---- src/app.ts | 10 ++++- swagger.json | 7 +--- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/packages/basic.gblib/index.ts b/packages/basic.gblib/index.ts index b78ba3562..0a4160b5a 100644 --- a/packages/basic.gblib/index.ts +++ b/packages/basic.gblib/index.ts @@ -60,7 +60,8 @@ export class GBBasicPackage implements IGBPackage { public async loadPackage (core: IGBCoreService, sequelize: Sequelize): Promise { core.sequelize.addModels([GuaribasSchedule]); - app.use(koaBody.koaBody({ multipart: true })); + app.use(koaBody.koaBody({ multipart: true, })); + app.listen(1111); } @@ -97,5 +98,11 @@ export class GBBasicPackage implements IGBPackage { app.use(waRouter.routes()); app.use(dbgRouter.routes()); app.use(imgRouter.routes()); + app.use(async (ctx, next) => { + if(ctx['status'] === 404){ + ctx.status = 404 + ctx.body = {msg:'emmmmmmm, seems 404'}; + } + }); } } diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index f207b26d5..99699627a 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -48,6 +48,7 @@ import qrcode from 'qrcode-terminal'; import express from 'express'; import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js'; import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js'; +import { method } from 'lodash'; /** * Support for Whatsapp. @@ -96,11 +97,13 @@ export class WhatsappDirectLine extends GBService { this.whatsappServiceKey = whatsappServiceKey; this.whatsappServiceNumber = whatsappServiceNumber; this.whatsappServiceUrl = whatsappServiceUrl; - this.provider = + this.provider = whatsappServiceKey === 'internal' ? 'GeneralBots' : whatsappServiceNumber.indexOf(';') > -1 ? 'maytapi' + : whatsappServiceKey !== 'internal' + ? 'graphapi' : 'chatapi'; this.groupId = groupId; } @@ -125,6 +128,7 @@ export class WhatsappDirectLine extends GBService { new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header') ); let options: any; + const phoneId = this.whatsappServiceNumber.split(';')[0]; switch (this.provider) { case 'GeneralBots': @@ -203,7 +207,7 @@ export class WhatsappDirectLine extends GBService { msg ); - s.sendEmail({pid: 0, to: adminEmail, subject: `Check your WhatsApp for bot ${this.botId}`, body: msg }); + s.sendEmail({ pid: 0, to: adminEmail, subject: `Check your WhatsApp for bot ${this.botId}`, body: msg }); }).bind(this) ); @@ -249,7 +253,6 @@ export class WhatsappDirectLine extends GBService { setUrl = false; break; - case 'chatapi': url = urlJoin(this.whatsappServiceUrl, 'webhook'); options = { @@ -268,7 +271,6 @@ export class WhatsappDirectLine extends GBService { break; case 'maytapi': - let phoneId = this.whatsappServiceNumber.split(';')[0]; let productId = this.whatsappServiceNumber.split(';')[1]; url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`; body = { @@ -288,7 +290,7 @@ export class WhatsappDirectLine extends GBService { json: true }; break; - } + } if (setUrl && options && this.whatsappServiceUrl) { GBServer.globals.server.use(`/audios`, express.static('work')); @@ -327,7 +329,7 @@ export class WhatsappDirectLine extends GBService { } public static providerFromRequest(req: any) { - return req.body.messages ? 'chatapi' : req.body.message ? 'maytapi' : 'GeneralBots'; + return req.body.messages ? 'chatapi' : req.body.message ? 'maytapi' : req.body.message ? 'graphapi' : 'GeneralBots'; } public async received(req, res) { @@ -379,6 +381,8 @@ export class WhatsappDirectLine extends GBService { } break; + case 'graphapi': + break; case 'maytapi': message = req.body.message; @@ -621,7 +625,7 @@ export class WhatsappDirectLine extends GBService { } } - private async endTransfer(id: any, locale: string, user: GuaribasUser, agent: GuaribasUser, sec: SecService) { + private async endTransfer(id: string, locale: string, user: GuaribasUser, agent: GuaribasUser, sec: SecService) { await this.sendToDeviceEx(id, Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null); if (user.agentSystemId.charAt(2) === ':') { @@ -643,7 +647,7 @@ export class WhatsappDirectLine extends GBService { await sec.updateHumanAgent(id, this.min.instance.instanceId, null); } - public inputMessage(client, conversationId, text, from, fromName, group, attachments) { + public inputMessage(client, conversationId: string, text: string, from, fromName: string, group, attachments: File) { return client.Conversations.Conversations_PostActivity({ conversationId: conversationId, activity: { @@ -794,7 +798,24 @@ export class WhatsappDirectLine extends GBService { }; break; + + case 'graphapi': + url = `https://graph.facebook.com/v15.0/${phoneId}/messages` + options = { + method:'POST', + timeout: 10000, + headers: { + token: `Bearer `, + 'Content-Type': 'application/json' + }, + body:{ + messaging_product: 'whatsapp', + recipient_type: 'individual', + to: phoneId, + } + } } + if (options) { try { // tslint:disable-next-line: await-promise @@ -845,7 +866,7 @@ export class WhatsappDirectLine extends GBService { } } - public async sendTextAsAudioToDevice(to, msg, chatId) { + public async sendTextAsAudioToDevice(to, msg: string, chatId) { const url = await GBConversationalService.getAudioBufferFromText(msg); await this.sendFileToDevice(to, url, 'Audio', msg, chatId); @@ -906,6 +927,8 @@ export class WhatsappDirectLine extends GBService { } }; break; + case 'graphapi': + } if (options) { diff --git a/src/app.ts b/src/app.ts index b0b9d41c1..eb4c41f8d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -96,7 +96,7 @@ export class GBServer { server.use(bodyParser.json()); server.use(bodyParser.urlencoded({ extended: true })); - process.on('unhandledRejection', (err, p) => { + process.on('unhandledRejection', (err, p) => { GBLog.error(`UNHANDLED_REJECTION(promises): ${p} ${err.toString()}`); }); @@ -126,7 +126,7 @@ export class GBServer { const azureDeployer: AzureDeployerService = await AzureDeployerService.createInstance(deployer); const adminService: GBAdminService = new GBAdminService(core); - if (process.env.NODE_ENV === 'development' ) { + if (process.env.NODE_ENV === 'development') { const proxy = GBConfigService.get('BOT_URL'); if (proxy !== undefined) { GBServer.globals.publicAddress = proxy; @@ -228,6 +228,12 @@ export class GBServer { winston.default(server, loggers[1]); } + server.get('*', function(req, res){ + GBLog.info(`HTTP 404: ${req.url}.`); + res.status(404); + res.end(); + }); + GBLog.info(`The Bot Server is in RUNNING mode...`); // Opens Navigator. diff --git a/swagger.json b/swagger.json index 0499bef4f..2f7b83206 100644 --- a/swagger.json +++ b/swagger.json @@ -13,7 +13,7 @@ "status": "Production" } }, - "host": "generalbots.ai", + "host": "", "basePath": "/", "schemes": [ "https" @@ -21,7 +21,7 @@ "consumes": [], "produces": [], "paths": { - "/api/talkTo": { + "/api/v2//dialog/talk": { "post": { "summary": "Talk to the user.", "description": "Talk to the user.", @@ -58,9 +58,6 @@ "properties": { "pid": { "type": "string" - }, - "mobile": { - "type": "string" }, "text": { "type": "string"