@peopl-health/nexus 2.5.9 → 2.5.10
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.
- package/lib/adapters/TwilioProvider.js +90 -15
- package/package.json +1 -1
|
@@ -105,15 +105,52 @@ class TwilioProvider extends MessageProvider {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
// Validate message has content
|
|
109
108
|
if (!messageParams.body && !messageParams.mediaUrl && !messageParams.contentSid) {
|
|
110
109
|
throw new Error('Message must have body, media URL, or content SID');
|
|
111
110
|
}
|
|
112
111
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
let result;
|
|
113
|
+
const chunks = messageParams.body && messageParams.body.length > 1600 && !messageParams.mediaUrl && !messageParams.contentSid
|
|
114
|
+
? this.splitMessageAtWordBoundaries(messageParams.body)
|
|
115
|
+
: null;
|
|
116
|
+
|
|
117
|
+
if (chunks) {
|
|
118
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
119
|
+
const chunkParams = { ...messageParams, body: chunks[i] };
|
|
120
|
+
result = await this.twilioClient.messages.create(chunkParams);
|
|
121
|
+
if (this.messageStorage && typeof this.messageStorage.saveMessage === 'function') {
|
|
122
|
+
try {
|
|
123
|
+
await this.messageStorage.saveMessage({
|
|
124
|
+
...messageData,
|
|
125
|
+
body: chunks[i],
|
|
126
|
+
code: formattedCode,
|
|
127
|
+
from: formattedFrom,
|
|
128
|
+
messageId: result.sid,
|
|
129
|
+
provider: 'twilio',
|
|
130
|
+
timestamp: new Date(),
|
|
131
|
+
fromMe: true,
|
|
132
|
+
processed: messageData.processed !== undefined ? messageData.processed : false,
|
|
133
|
+
statusInfo: {
|
|
134
|
+
status: result.status ? result.status.toLowerCase() : null,
|
|
135
|
+
updatedAt: result.dateCreated || new Date()
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
logger.info('[TwilioProvider] Message chunk persisted', { messageId: result.sid, chunk: i + 1, total: chunks.length });
|
|
139
|
+
} catch (storageError) {
|
|
140
|
+
logger.error('TwilioProvider storage failed:', storageError);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (i < chunks.length - 1) {
|
|
144
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
logger.info('[TwilioProvider] Sending message', messageParams);
|
|
149
|
+
try {
|
|
150
|
+
result = await this.twilioClient.messages.create(messageParams);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
throw new Error(`Twilio send failed: ${error.message}`);
|
|
153
|
+
}
|
|
117
154
|
if (this.messageStorage && typeof this.messageStorage.saveMessage === 'function') {
|
|
118
155
|
try {
|
|
119
156
|
await this.messageStorage.saveMessage({
|
|
@@ -135,17 +172,15 @@ class TwilioProvider extends MessageProvider {
|
|
|
135
172
|
logger.error('TwilioProvider storage failed:', storageError);
|
|
136
173
|
}
|
|
137
174
|
}
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
success: true,
|
|
141
|
-
messageId: result.sid,
|
|
142
|
-
provider: 'twilio',
|
|
143
|
-
status: result.status,
|
|
144
|
-
result
|
|
145
|
-
};
|
|
146
|
-
} catch (error) {
|
|
147
|
-
throw new Error(`Twilio send failed: ${error.message}`);
|
|
148
175
|
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
success: true,
|
|
179
|
+
messageId: result.sid,
|
|
180
|
+
provider: 'twilio',
|
|
181
|
+
status: result.status,
|
|
182
|
+
result
|
|
183
|
+
};
|
|
149
184
|
}
|
|
150
185
|
|
|
151
186
|
async sendTypingIndicator(messageId) {
|
|
@@ -394,6 +429,46 @@ class TwilioProvider extends MessageProvider {
|
|
|
394
429
|
return Math.max(0, targetTime.getTime() - now.getTime());
|
|
395
430
|
}
|
|
396
431
|
|
|
432
|
+
/**
|
|
433
|
+
* Split a message into chunks at sentence boundaries, respecting Twilio's character limit
|
|
434
|
+
* @param {string} text - The message text to split
|
|
435
|
+
* @param {number} maxLength - Maximum length per chunk (default: 1600)
|
|
436
|
+
* @returns {Array<string>} Array of message chunks
|
|
437
|
+
*/
|
|
438
|
+
splitMessageAtWordBoundaries(text, maxLength = 1600) {
|
|
439
|
+
if (!text || text.length <= maxLength) return [text];
|
|
440
|
+
const chunks = [];
|
|
441
|
+
let remaining = text;
|
|
442
|
+
while (remaining.length > maxLength) {
|
|
443
|
+
let splitIndex = -1;
|
|
444
|
+
const searchStart = Math.max(0, maxLength - 500); // Look back up to 500 chars
|
|
445
|
+
const searchArea = remaining.substring(searchStart, maxLength + 1);
|
|
446
|
+
|
|
447
|
+
const regex = /[.!?]\s/g;
|
|
448
|
+
let match;
|
|
449
|
+
let lastMatchIndex = -1;
|
|
450
|
+
|
|
451
|
+
while ((match = regex.exec(searchArea)) !== null) {
|
|
452
|
+
const absoluteIndex = searchStart + match.index;
|
|
453
|
+
if (absoluteIndex <= maxLength) {
|
|
454
|
+
lastMatchIndex = absoluteIndex + match[0].length;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (lastMatchIndex > 0) {
|
|
459
|
+
splitIndex = lastMatchIndex;
|
|
460
|
+
} else {
|
|
461
|
+
splitIndex = remaining.lastIndexOf(' ', maxLength);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const chunk = remaining.substring(0, splitIndex === -1 ? maxLength : splitIndex).trim();
|
|
465
|
+
if (chunk) chunks.push(chunk);
|
|
466
|
+
remaining = remaining.substring(splitIndex === -1 ? maxLength : splitIndex).trim();
|
|
467
|
+
}
|
|
468
|
+
if (remaining) chunks.push(remaining);
|
|
469
|
+
return chunks;
|
|
470
|
+
}
|
|
471
|
+
|
|
397
472
|
/**
|
|
398
473
|
* List templates from Twilio Content API
|
|
399
474
|
* @param {Object} options - Query options
|