-
Notifications
You must be signed in to change notification settings - Fork 124
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
Add a solution to the WhatsApp Cloud payload character limitation #1605
Conversation
327ed0d
to
f8e6cb0
Compare
|
||
object PayloadWhatsAppCloudMongoDAO: PayloadWhatsAppCloudDAO { | ||
|
||
private const val COLLECTION_NAME = "sncv_whatsapp_payload" |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use val
bot/connector-whatsapp-cloud/pom.xml
Outdated
<dependency> | ||
<groupId>org.litote.kmongo</groupId> | ||
<artifactId>kmongo-async</artifactId> | ||
</dependency> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
f8e6cb0
to
15ea187
Compare
There was a problem hiding this 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?
.capped(true) | ||
.sizeInBytes(100000000) | ||
.maxDocuments(50000) |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this still required?
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
f7e0f2c
to
d065487
Compare
import com.github.salomonbrys.kodein.bind | ||
import com.github.salomonbrys.kodein.singleton | ||
|
||
val whastappCloudConnectorModule = Kodein.Module { |
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
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
d065487
to
d1e894e
Compare
resolve #1604