@@ -20,6 +20,7 @@ import ai.tock.bot.connector.ConnectorBase
20
20
import ai.tock.bot.connector.ConnectorCallback
21
21
import ai.tock.bot.connector.ConnectorData
22
22
import ai.tock.bot.connector.ConnectorMessage
23
+ import ai.tock.bot.connector.ConnectorQueue
23
24
import ai.tock.bot.connector.whatsapp.cloud.model.send.manageTemplate.WhatsAppCloudTemplate
24
25
import ai.tock.bot.connector.whatsapp.cloud.model.webhook.Change
25
26
import ai.tock.bot.connector.whatsapp.cloud.model.webhook.Entry
@@ -48,7 +49,6 @@ import ai.tock.shared.listProperty
48
49
import ai.tock.shared.security.RequestFilter
49
50
import com.fasterxml.jackson.module.kotlin.readValue
50
51
import com.github.salomonbrys.kodein.instance
51
- import java.time.Duration
52
52
import mu.KotlinLogging
53
53
54
54
class WhatsAppConnectorCloudConnector internal constructor(
@@ -70,6 +70,7 @@ class WhatsAppConnectorCloudConnector internal constructor(
70
70
71
71
private val whatsAppCloudApiService: WhatsAppCloudApiService = WhatsAppCloudApiService (client)
72
72
private val executor: Executor by injector.instance()
73
+ private val messageQueue = ConnectorQueue (executor)
73
74
74
75
private val restrictedPhoneNumbers =
75
76
listProperty(" tock_whatsapp_cloud_restricted_phone_numbers" , emptyList()).toSet().takeIf { it.isNotEmpty() }
@@ -180,15 +181,15 @@ class WhatsAppConnectorCloudConnector internal constructor(
180
181
181
182
override fun send (event : Event , callback : ConnectorCallback , delayInMs : Long ) {
182
183
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
+ )
191
189
}
190
+ }, send = {
191
+ whatsAppCloudApiService.sendMessage(phoneNumberId, token, it)
192
+ })
192
193
}
193
194
}
194
195
0 commit comments