From 52bea408467cd0dbc229c6d2b74cd9b9ac945461 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Thu, 15 Jun 2023 12:02:25 -0400 Subject: [PATCH] fix: ensure messages are sent sequentially and update in light of chunking --- src/services/twilio-conversation.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/services/twilio-conversation.ts b/src/services/twilio-conversation.ts index f6addd57..b03cf26d 100644 --- a/src/services/twilio-conversation.ts +++ b/src/services/twilio-conversation.ts @@ -249,9 +249,16 @@ export class TwilioConversationClient { maxChunkLength?: number }) { const chunks = chunkString(text, TWILIO_SMS_LENGTH_SOFT_LIMIT) - return Promise.all( - chunks.map((chunk) => this.sendMessage({ conversationSid, text: chunk })) - ) + const out: TwilioConversationMessage[] = [] + for (const chunk of chunks) { + const sent = await this.sendMessage({ + conversationSid, + text: chunk + }) + out.push(sent) + } + + return out } /** @@ -339,11 +346,11 @@ export class TwilioConversationClient { } const response = await this.fetchMessages(conversationSid) + const candidates = response.messages.filter( + (message) => message.author !== this.botName + ) - if (response.messages.length > 1) { - const candidates = response.messages.filter( - (message) => message.author !== this.botName - ) + if (candidates.length > 0) { const candidate = candidates[candidates.length - 1] if (validate(candidate)) {