Skip to content

Commit

Permalink
chore: Update telemetry and add new integrations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dgcode-tec committed Jul 15, 2024
1 parent 22a24b1 commit a03ce98
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [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)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
23 changes: 2 additions & 21 deletions src/api/guards/telemetry.guard.ts
Original file line number Diff line number Diff line change
@@ -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();
}
Expand Down
9 changes: 9 additions & 0 deletions src/api/integrations/chatwoot/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1034,6 +1035,8 @@ export class ChatwootService {
quoted: options?.quoted,
};

sendTelemetry('/message/sendWhatsAppAudio');

const messageSent = await waInstance?.audioWhatsapp(data, true);

return messageSent;
Expand All @@ -1052,6 +1055,8 @@ export class ChatwootService {
quoted: options?.quoted,
};

sendTelemetry('/message/sendMedia');

if (caption) {
data.caption = caption;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1380,6 +1387,8 @@ export class ChatwootService {
delay: 1200,
};

sendTelemetry('/message/sendText');

await waInstance?.textMessage(data);
}

Expand Down
17 changes: 17 additions & 0 deletions src/api/integrations/typebot/services/typebot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1091,6 +1092,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendText');
}

if (message.type === 'image') {
Expand All @@ -1103,6 +1106,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendMedia');
}

if (message.type === 'video') {
Expand All @@ -1115,6 +1120,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendMedia');
}

if (message.type === 'audio') {
Expand All @@ -1127,6 +1134,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendWhatsAppAudio');
}

const wait = findItemAndGetSecondsToWait(clientSideActions, message.id);
Expand Down Expand Up @@ -1156,6 +1165,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendText');
}

await prismaRepository.typebotSession.update({
Expand Down Expand Up @@ -1588,6 +1599,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendText');
}
return;
}
Expand Down Expand Up @@ -1700,6 +1713,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendText');
}
return;
}
Expand Down Expand Up @@ -1779,6 +1794,8 @@ export class TypebotService {
},
true,
);

sendTelemetry('/message/sendText');
}
return;
}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/sendTelemetry.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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);
});
};

0 comments on commit a03ce98

Please sign in to comment.