From a03ce984f68d5cbfb95037f6fc2b8a94bbc24042 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Mon, 15 Jul 2024 12:43:20 -0300 Subject: [PATCH] chore: Update telemetry and add new integrations Refactored the telemetry guard to use a new sendTelemetry utility function, which allows for easier tracking of API routes. Also, added telemetry events for message sending in the Chatwoot and Typebot services. Additionally, updated the README.md to include new content creators and added new integrations with Typebot and Chatwoot services. Modified: - README.md - package.json - src/api/guards/telemetry.guard.ts - src/api/integrations/chatwoot/services/chatwoot.service.ts - src/api/integrations/typebot/services/typebot.service.ts Added: - src/utils/sendTelemetry.ts --- README.md | 5 +++- package.json | 2 +- src/api/guards/telemetry.guard.ts | 23 ++--------------- .../chatwoot/services/chatwoot.service.ts | 9 +++++++ .../typebot/services/typebot.service.ts | 17 +++++++++++++ src/utils/sendTelemetry.ts | 25 +++++++++++++++++++ 6 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 src/utils/sendTelemetry.ts diff --git a/README.md b/README.md index 495c779eb..5a85edb51 100644 --- a/README.md +++ b/README.md @@ -171,4 +171,7 @@ We are proud to collaborate with the following content creators who have contrib - [XPop Digital](https://www.youtube.com/@xpopdigital) - [Costar Wagner Dev](https://www.youtube.com/@costarwagnerdev) - [Dante Testa](https://youtube.com/@dantetesta_) -- [Rubén Salazar](https://youtube.com/channel/UCnYGZIE2riiLqaN9sI6riig) \ No newline at end of file +- [Rubén Salazar](https://youtube.com/channel/UCnYGZIE2riiLqaN9sI6riig) +- [OrionDesign](youtube.com/OrionDesign_Oficial) +- [IMPA 365](youtube.com/@impa365_ofc) +- [Comunidade Hub Connect](https://youtube.com/@comunidadehubconnect) \ No newline at end of file diff --git a/package.json b/package.json index 0d6f28ee2..4dbc93170 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "2.0.0", + "version": "2.0.0-beta", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { diff --git a/src/api/guards/telemetry.guard.ts b/src/api/guards/telemetry.guard.ts index af8645d27..c8599e394 100644 --- a/src/api/guards/telemetry.guard.ts +++ b/src/api/guards/telemetry.guard.ts @@ -1,29 +1,10 @@ -import axios from 'axios'; import { NextFunction, Request, Response } from 'express'; -import fs from 'fs'; -const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); - -interface TelemetryData { - route: string; - apiVersion: string; - timestamp: Date; -} +import { sendTelemetry } from '../../utils/sendTelemetry'; class Telemetry { public collectTelemetry(req: Request, res: Response, next: NextFunction): void { - const telemetry: TelemetryData = { - route: req.path, - apiVersion: `${packageJson.version}`, - timestamp: new Date(), - }; - - axios - .post('https://log.evolution-api.com/telemetry', telemetry) - .then(() => {}) - .catch((error) => { - console.error('Telemetry error', error); - }); + sendTelemetry(req.path); next(); } diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index cade750a5..07ae41bde 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -21,6 +21,7 @@ import { Readable } from 'stream'; import { Chatwoot, ConfigService, HttpServer } from '../../../../config/env.config'; import { Logger } from '../../../../config/logger.config'; import i18next from '../../../../utils/i18n'; +import { sendTelemetry } from '../../../../utils/sendTelemetry'; import { ICache } from '../../../abstract/abstract.cache'; import { InstanceDto } from '../../../dto/instance.dto'; import { Options, Quoted, SendAudioDto, SendMediaDto, SendTextDto } from '../../../dto/sendMessage.dto'; @@ -1034,6 +1035,8 @@ export class ChatwootService { quoted: options?.quoted, }; + sendTelemetry('/message/sendWhatsAppAudio'); + const messageSent = await waInstance?.audioWhatsapp(data, true); return messageSent; @@ -1052,6 +1055,8 @@ export class ChatwootService { quoted: options?.quoted, }; + sendTelemetry('/message/sendMedia'); + if (caption) { data.caption = caption; } @@ -1290,6 +1295,8 @@ export class ChatwootService { quoted: await this.getQuotedMessage(body, instance), }; + sendTelemetry('/message/sendText'); + let messageSent: any; try { messageSent = await waInstance?.textMessage(data, true); @@ -1380,6 +1387,8 @@ export class ChatwootService { delay: 1200, }; + sendTelemetry('/message/sendText'); + await waInstance?.textMessage(data); } diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index 694173444..4534c6562 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -3,6 +3,7 @@ import axios from 'axios'; import { ConfigService, S3, Typebot } from '../../../../config/env.config'; import { Logger } from '../../../../config/logger.config'; +import { sendTelemetry } from '../../../../utils/sendTelemetry'; import { InstanceDto } from '../../../dto/instance.dto'; import { PrismaRepository } from '../../../repository/repository.service'; import { WAMonitoringService } from '../../../services/monitor.service'; @@ -1091,6 +1092,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendText'); } if (message.type === 'image') { @@ -1103,6 +1106,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendMedia'); } if (message.type === 'video') { @@ -1115,6 +1120,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendMedia'); } if (message.type === 'audio') { @@ -1127,6 +1134,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendWhatsAppAudio'); } const wait = findItemAndGetSecondsToWait(clientSideActions, message.id); @@ -1156,6 +1165,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendText'); } await prismaRepository.typebotSession.update({ @@ -1588,6 +1599,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendText'); } return; } @@ -1700,6 +1713,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendText'); } return; } @@ -1779,6 +1794,8 @@ export class TypebotService { }, true, ); + + sendTelemetry('/message/sendText'); } return; } diff --git a/src/utils/sendTelemetry.ts b/src/utils/sendTelemetry.ts new file mode 100644 index 000000000..1d823a06f --- /dev/null +++ b/src/utils/sendTelemetry.ts @@ -0,0 +1,25 @@ +import axios from 'axios'; +import fs from 'fs'; + +const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); + +export interface TelemetryData { + route: string; + apiVersion: string; + timestamp: Date; +} + +export const sendTelemetry = async (route: string): Promise => { + const telemetry: TelemetryData = { + route, + apiVersion: `${packageJson.version}`, + timestamp: new Date(), + }; + + axios + .post('https://log.evolution-api.com/telemetry', telemetry) + .then(() => {}) + .catch((error) => { + console.error('Telemetry error', error); + }); +};