Skip to content

Commit fae6670

Browse files
committed
fixes theopenconversationkit#1680 whatsapp_connector: add message queue to connector
1 parent 2a7ef50 commit fae6670

File tree

7 files changed

+250
-223
lines changed

7 files changed

+250
-223
lines changed

bot/connector-whatsapp-cloud/src/main/kotlin/WhatsAppConnectorCloudConnector.kt

+10-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import ai.tock.bot.connector.ConnectorBase
2020
import ai.tock.bot.connector.ConnectorCallback
2121
import ai.tock.bot.connector.ConnectorData
2222
import ai.tock.bot.connector.ConnectorMessage
23+
import ai.tock.bot.connector.ConnectorQueue
2324
import ai.tock.bot.connector.whatsapp.cloud.model.send.manageTemplate.WhatsAppCloudTemplate
2425
import ai.tock.bot.connector.whatsapp.cloud.model.webhook.Change
2526
import ai.tock.bot.connector.whatsapp.cloud.model.webhook.Entry
@@ -48,7 +49,6 @@ import ai.tock.shared.listProperty
4849
import ai.tock.shared.security.RequestFilter
4950
import com.fasterxml.jackson.module.kotlin.readValue
5051
import com.github.salomonbrys.kodein.instance
51-
import java.time.Duration
5252
import mu.KotlinLogging
5353

5454
class WhatsAppConnectorCloudConnector internal constructor(
@@ -70,6 +70,7 @@ class WhatsAppConnectorCloudConnector internal constructor(
7070

7171
private val whatsAppCloudApiService: WhatsAppCloudApiService = WhatsAppCloudApiService(client)
7272
private val executor: Executor by injector.instance()
73+
private val messageQueue = ConnectorQueue(executor)
7374

7475
private val restrictedPhoneNumbers =
7576
listProperty("tock_whatsapp_cloud_restricted_phone_numbers", emptyList()).toSet().takeIf { it.isNotEmpty() }
@@ -180,15 +181,15 @@ class WhatsAppConnectorCloudConnector internal constructor(
180181

181182
override fun send(event: Event, callback: ConnectorCallback, delayInMs: Long) {
182183
if (event is Action) {
183-
SendActionConverter.toBotMessage(event)
184-
?.also {
185-
val delay = Duration.ofMillis(delayInMs)
186-
executor.executeBlocking(delay) {
187-
whatsAppCloudApiService.sendMessage(
188-
phoneNumberId, token, it
189-
)
190-
}
184+
messageQueue.add(event, delayInMs, prepare = { action ->
185+
SendActionConverter.toBotMessage(action)?.let {
186+
whatsAppCloudApiService.prepareMessage(
187+
phoneNumberId, token, it
188+
)
191189
}
190+
}, send = {
191+
whatsAppCloudApiService.sendMessage(phoneNumberId, token, it)
192+
})
192193
}
193194
}
194195

0 commit comments

Comments
 (0)