Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a solution to the WhatsApp Cloud payload character limitation #1605

Conversation

SNCFamiraBenkaci
Copy link
Contributor

@SNCFamiraBenkaci SNCFamiraBenkaci commented May 13, 2024

resolve #1604


object PayloadWhatsAppCloudMongoDAO: PayloadWhatsAppCloudDAO {

private const val COLLECTION_NAME = "sncv_whatsapp_payload"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to write

private val collectionName = property("tock_whatsapp_payload", "whatsapp_payload")

@@ -94,7 +94,7 @@ data class WhatsAppCloudBotActionButton(

data class WhatsAppCloudBotActionButtonReply(
val title: String,
val id: String,
var id: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use val and copy() when you need to change it

@@ -62,7 +62,7 @@ sealed class Component {


data class TextParameter(val type: ParameterType, val text: String)
data class PayloadParameter(val type: ParameterType, val payload: String?, val text: String)
data class PayloadParameter(val type: ParameterType, var payload: String?, val text: String)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use val

@@ -17,6 +17,6 @@
package ai.tock.bot.connector.whatsapp.cloud.model.webhook.message.content

data class ButtonContent(
val payload: String,
var payload: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use val

@@ -27,7 +27,7 @@ data class InteractiveContent(
)

data class ButtonReply(
val id: String,
var id: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use val

<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-async</artifactId>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove kmongo-async - it should work with kmongo only

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, what's the reason ?

@SNCFamiraBenkaci SNCFamiraBenkaci force-pushed the 16604_add_solution_limitation_payload_button branch from f8e6cb0 to 15ea187 Compare May 15, 2024 14:42
@SNCFamiraBenkaci
Copy link
Contributor Author

#1604

Copy link
Member

@Fabilin Fabilin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, could you maybe describe the issue a bit more in-depth?

Comment on lines 56 to 58
.capped(true)
.sizeInBytes(100000000)
.maxDocuments(50000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe a capped collection may not be the most appropriate here - maybe just use a regular collection with a TTL index?


object PayloadWhatsAppCloudMongoDAO: PayloadWhatsAppCloudDAO {

private val COLLECTION_NAME = property("tock_whatsapp_payload", "whatsapp_payload")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to document this property in the module's README?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a const so private val collectionName =

}

override fun getPayloadById(id: String): String? {
return collection.findOneById(id)?.payload
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe first check that the id is a valid UUID before looking up the database?

}

private fun updateButton(button: WhatsAppCloudBotActionButton): WhatsAppCloudBotActionButton =
if (button.reply.id.length > 256) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>= 256, no?


private fun updateTemplateButton(button: Component.Button): Component.Button =
button.copy(parameters = button.parameters.mapNotNull { parameters ->
parameters.payload?.takeIf { it.length > 128 }?.let {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it.length >= 128 maybe?

}
}

fun sendMessageNOTRef(phoneNumberId: String, token: String, messageRequest: WhatsAppCloudSendBotMessage) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still required?

Comment on lines 342 to 273
messageRequest.template.components
.filterIsInstance<Component.Carousel>()
.flatMap { it.cards }
.flatMap { it.components }
.filterIsInstance<Component.Header>()
.flatMap { it.parameters }
.filterIsInstance<HeaderParameter.Image>()
.toList()
.forEach { imageHeader ->
imageHeader.image.id?.let { imageId ->
val newImageId = sendMedia(client, phoneNumberId, token, imageId, FileType.PNG.type).id
imageHeader.image.id = newImageId
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
messageRequest.template.components
.filterIsInstance<Component.Carousel>()
.flatMap { it.cards }
.flatMap { it.components }
.filterIsInstance<Component.Header>()
.flatMap { it.parameters }
.filterIsInstance<HeaderParameter.Image>()
.toList()
.forEach { imageHeader ->
imageHeader.image.id?.let { imageId ->
val newImageId = sendMedia(client, phoneNumberId, token, imageId, FileType.PNG.type).id
imageHeader.image.id = newImageId
}
}
messageRequest.template.components
.asSequence()
.filterIsInstance<Component.Carousel>()
.flatMap { it.cards }
.flatMap { it.components }
.filterIsInstance<Component.Header>()
.flatMap { it.parameters }
.filterIsInstance<HeaderParameter.Image>()
.forEach { imageHeader ->
imageHeader.image.id?.let { imageId ->
val newImageId = sendMedia(client, phoneNumberId, token, imageId, FileType.PNG.type).id
imageHeader.image.id = newImageId
}
}

avoiding unnecessary list creations and iterations - although ideally it should perform an object copy instead of mutating the message

@SNCFamiraBenkaci SNCFamiraBenkaci force-pushed the 16604_add_solution_limitation_payload_button branch 2 times, most recently from f7e0f2c to d065487 Compare May 16, 2024 14:40
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.singleton

val whastappCloudConnectorModule = Kodein.Module {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo whatsAppCloudConnectorModule

}

fun isUUID(uuid: String): Boolean {
val uuidRegex = Regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a val at class level in order to avoir calculate the regexp each time

@SNCFamiraBenkaci SNCFamiraBenkaci force-pushed the 16604_add_solution_limitation_payload_button branch from d065487 to d1e894e Compare May 17, 2024 09:00
@vsct-jburet vsct-jburet added this to the 24.3.0 milestone May 17, 2024
@vsct-jburet vsct-jburet merged commit ed9af4d into theopenconversationkit:master May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[WhatsApp Cloud] Add a solution to the WhatsApp Cloud payload character limitation
3 participants