@peopl-health/nexus 1.1.0 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,75 +0,0 @@
1
- const axios = require('axios');
2
- const { v4: uuidv4 } = require('uuid');
3
-
4
- function convertTwilioToInternalFormat(twilioMessage) {
5
- const from = twilioMessage.From || '';
6
- const to = twilioMessage.To || '';
7
- const fromMe = to === from;
8
-
9
- return {
10
- key: {
11
- id: twilioMessage.MessageSid || uuidv4(),
12
- fromMe: fromMe,
13
- remoteJid: fromMe ? to : from
14
- },
15
- pushName: twilioMessage.ProfileName || '',
16
- message: {
17
- conversation: twilioMessage.Body || ''
18
- },
19
- messageTimestamp: Math.floor(Date.now() / 1000)
20
- };
21
- }
22
-
23
- async function downloadMediaFromTwilio(mediaUrl, credentials) {
24
- try {
25
- const response = await axios({
26
- method: 'GET',
27
- url: mediaUrl,
28
- responseType: 'arraybuffer',
29
- headers: {
30
- 'Authorization': `Basic ${Buffer.from(
31
- `${credentials.accountSid}:${credentials.authToken}`
32
- ).toString('base64')}`
33
- }
34
- });
35
-
36
- return Buffer.from(response.data);
37
- } catch (error) {
38
- console.error(`Failed to download media: ${error.message}`);
39
- throw error;
40
- }
41
- }
42
-
43
- function getMediaTypeFromContentType(contentType) {
44
- if (contentType.startsWith('image/')) return 'imageMessage';
45
- if (contentType.startsWith('audio/')) return 'audioMessage';
46
- if (contentType.startsWith('video/')) return 'videoMessage';
47
- if (contentType.startsWith('application/')) return 'documentMessage';
48
- return 'unknownMessage';
49
- }
50
-
51
- function extractTitle(message, mediaType) {
52
- if (mediaType === 'documentMessage' && message.MediaUrl0) {
53
- const urlParts = message.MediaUrl0.split('/');
54
- return urlParts[urlParts.length - 1] || null;
55
- }
56
- return null;
57
- }
58
-
59
- const ensureWhatsAppFormat = (phoneNumber) => {
60
- if (!phoneNumber) return null;
61
-
62
- if (phoneNumber.startsWith('whatsapp:')) {
63
- return phoneNumber;
64
- }
65
-
66
- return `whatsapp:${phoneNumber}`;
67
- };
68
-
69
- module.exports = {
70
- convertTwilioToInternalFormat,
71
- downloadMediaFromTwilio,
72
- getMediaTypeFromContentType,
73
- extractTitle,
74
- ensureWhatsAppFormat
75
- };
@@ -1,60 +0,0 @@
1
- const moment = require('moment-timezone');
2
-
3
- function delay(ms) {
4
- return new Promise(resolve => setTimeout(resolve, ms));
5
- }
6
-
7
- function formatCode(codeBase) {
8
- const [number, domain] = codeBase.split('@');
9
-
10
- if (!number || !domain) {
11
- throw new Error('Invalid code format: missing number or domain');
12
- }
13
-
14
- if (domain !== 's.whatsapp.net') {
15
- throw new Error('Invalid domain: must be s.whatsapp.net');
16
- }
17
-
18
- let formattedNumber = number;
19
-
20
- if (formattedNumber.endsWith('-2')) {
21
- formattedNumber = formattedNumber.slice(0, -2);
22
- }
23
-
24
- if (formattedNumber.length === 10) {
25
- formattedNumber = '52' + formattedNumber;
26
- }
27
-
28
- if (formattedNumber.startsWith('52') && formattedNumber.length === 12) {
29
- formattedNumber = formattedNumber.substring(0, 2) + '1' + formattedNumber.substring(2);
30
- }
31
- return `${formattedNumber}@s.whatsapp.net`;
32
- }
33
-
34
- function calculateDelay(sendTime, timeZone) {
35
- if (sendTime !== undefined && timeZone !== undefined) {
36
- const sendMoment = moment.tz(sendTime, timeZone);
37
-
38
- if (!sendMoment.isValid()) {
39
- return { error: 'Invalid time format' };
40
- }
41
-
42
- const now = moment().tz(timeZone);
43
- const randomDelay = Math.floor(Math.random() * 15001) + 15000;
44
- const delay = sendMoment.diff(now) + randomDelay;
45
-
46
- if (delay <= 0) {
47
- return 2500;
48
- }
49
-
50
- return delay;
51
- } else {
52
- return 2500;
53
- }
54
- }
55
-
56
- module.exports = {
57
- delay,
58
- formatCode,
59
- calculateDelay
60
- };