Skip to content

Commit

Permalink
send message type image (#1690)
Browse files Browse the repository at this point in the history
* send message type image

* send message type image

---------

Co-authored-by: BENKACI Amira <[email protected]>
  • Loading branch information
SNCFamiraBenkaci and BENKACI Amira authored Aug 20, 2024
1 parent 1f3bd4f commit e5f360c
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ fun BotBus.whatsAppCloudText(
userId = userId.id,
)

fun BotBus.whatsAppCloudImage(
id: String,
link: String? = null,
caption: CharSequence? = null,
): WhatsAppCloudBotImageMessage =
WhatsAppCloudBotImageMessage(
messagingProduct = "whatsapp",
image = WhatsAppCloudBotImage(
id = id,
link = link,
caption = translate(caption).toString()
),
recipientType = WhatsAppCloudBotRecipientType.individual,
userId = userId.id,
)

fun I18nTranslator.whatsAppCloudReplyButtonMessage(
text: CharSequence,
vararg replies: QuickReply,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import ai.tock.bot.connector.ConnectorMessage
import ai.tock.bot.connector.ConnectorType
import ai.tock.bot.connector.whatsapp.cloud.UserHashedIdCache
import ai.tock.bot.connector.whatsapp.cloud.WhatsAppCloudConnectorMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.WhatsAppCloudBotInteractiveMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.WhatsAppCloudBotLocationMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.WhatsAppCloudBotTemplateMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.WhatsAppCloudBotTextMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.*
import ai.tock.bot.connector.whatsapp.cloud.whatsAppCloudConnectorType
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
Expand All @@ -41,9 +38,10 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo
JsonSubTypes.Type(value = WhatsAppCloudBotInteractiveMessage::class, name = "interactive"),
JsonSubTypes.Type(value = WhatsAppCloudBotLocationMessage::class, name = "location"),
JsonSubTypes.Type(value = WhatsAppCloudBotTemplateMessage::class, name = "template"),
)
JsonSubTypes.Type(value = WhatsAppCloudBotImageMessage::class, name = "image")
)

abstract class WhatsAppCloudBotMessage ( val type: WhatsAppCoudBotMessageType, @JsonIgnore internal open val userId: String?) :
abstract class WhatsAppCloudBotMessage (val type: WhatsAppCloudBotMessageType, @JsonIgnore internal open val userId: String?) :
ConnectorMessage, WhatsAppCloudConnectorMessage() {

@get:JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

package ai.tock.bot.connector.whatsapp.cloud.model.send.message

enum class WhatsAppCoudBotMessageType {
enum class WhatsAppCloudBotMessageType {
text, reaction, image, audio , video, location, contacts, interactive, template
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
* limitations under the License.
*/

package ai.tock.bot.connector.whatsapp.cloud.model.send.media
package ai.tock.bot.connector.whatsapp.cloud.model.send.message

import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotRecipientType
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCoudBotMessageType
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.WhatsAppCloudBotImage

data class WhatsAppCloudSendBotImageMessage(
override val messagingProduct: String,
override val to: String?,
override val recipientType: WhatsAppCloudBotRecipientType?
) : WhatsAppCloudSendBotMessage(WhatsAppCoudBotMessageType.image) {

}
override val messagingProduct: String,
val image: WhatsAppCloudBotImage,
override val recipientType: WhatsAppCloudBotRecipientType,
override val to: String,
) : WhatsAppCloudSendBotMessage(WhatsAppCloudBotMessageType.image)
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ data class WhatsAppCloudSendBotInteractiveMessage(
val interactive: WhatsAppCloudBotInteractive,
override val recipientType: WhatsAppCloudBotRecipientType,
override val to: String,
) : WhatsAppCloudSendBotMessage(WhatsAppCoudBotMessageType.interactive)
) : WhatsAppCloudSendBotMessage(WhatsAppCloudBotMessageType.interactive)
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ data class WhatsAppCloudSendBotLocationMessage(
val location: WhatsAppCloudBotLocation,
override val recipientType: WhatsAppCloudBotRecipientType,
override val to: String,
) : WhatsAppCloudSendBotMessage(WhatsAppCoudBotMessageType.location)
) : WhatsAppCloudSendBotMessage(WhatsAppCloudBotMessageType.location)
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo
JsonSubTypes.Type(value = WhatsAppCloudSendBotInteractiveMessage::class, name = "interactive"),
JsonSubTypes.Type(value = WhatsAppCloudSendBotLocationMessage::class, name = "location"),
JsonSubTypes.Type(value = WhatsAppCloudSendBotTemplateMessage::class, name = "template"),
)
JsonSubTypes.Type(value = WhatsAppCloudSendBotImageMessage::class, name = "image"),
)

abstract class WhatsAppCloudSendBotMessage(val type: WhatsAppCoudBotMessageType) {
abstract class WhatsAppCloudSendBotMessage(val type: WhatsAppCloudBotMessageType) {

@get:JsonProperty("messaging_product")
abstract val messagingProduct:String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ data class WhatsAppCloudSendBotTemplateMessage(
val template: WhatsAppCloudBotTemplate,
override val recipientType: WhatsAppCloudBotRecipientType,
override val to: String,
) : WhatsAppCloudSendBotMessage(WhatsAppCoudBotMessageType.template)
) : WhatsAppCloudSendBotMessage(WhatsAppCloudBotMessageType.template)
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ data class WhatsAppCloudSendBotTextMessage (
override val to: String,
@get:JsonProperty("preview_url")
val previewUrl: Boolean = false,
) : WhatsAppCloudSendBotMessage(WhatsAppCoudBotMessageType.text)
) : WhatsAppCloudSendBotMessage(WhatsAppCloudBotMessageType.text)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2017/2021 e-voyageurs technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.tock.bot.connector.whatsapp.cloud.model.send.message.content

data class WhatsAppCloudBotImage(
var id: String,
val link: String?,
val caption: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2017/2021 e-voyageurs technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.tock.bot.connector.whatsapp.cloud.model.send.message.content

import ai.tock.bot.connector.whatsapp.cloud.model.send.message.*
import ai.tock.bot.engine.config.UploadedFilesService.attachmentType
import ai.tock.bot.engine.message.Attachment
import ai.tock.bot.engine.message.GenericMessage

data class WhatsAppCloudBotImageMessage (
override val messagingProduct: String,
val image: WhatsAppCloudBotImage,
override val recipientType: WhatsAppCloudBotRecipientType,
override val userId: String? = null,
) : WhatsAppCloudBotMessage(WhatsAppCloudBotMessageType.image, userId) {
override fun toSendBotMessage(recipientId: String): WhatsAppCloudSendBotMessage =
WhatsAppCloudSendBotImageMessage(
messagingProduct,
image,
recipientType,
recipientId
)

override fun toGenericMessage(): GenericMessage =
GenericMessage(
texts = mapOf(GenericMessage.TEXT_PARAM to "image"),
attachments = listOf(Attachment(image.id, attachmentType(image.id)))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class WhatsAppCloudBotInteractiveMessage (
val interactive: WhatsAppCloudBotInteractive,
override val recipientType: WhatsAppCloudBotRecipientType,
override val userId: String? = null,
): WhatsAppCloudBotMessage(WhatsAppCoudBotMessageType.interactive, userId) {
): WhatsAppCloudBotMessage(WhatsAppCloudBotMessageType.interactive, userId) {
override fun toGenericMessage(): GenericMessage? {
val texts = mapOf(GenericMessage.TEXT_PARAM to (interactive.body?.text ?: ""))
return GenericMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotM
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotRecipientType
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotLocationMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCoudBotMessageType
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotMessageType
import ai.tock.bot.engine.message.GenericMessage

data class WhatsAppCloudBotLocationMessage(
override val messagingProduct: String,
val location: WhatsAppCloudBotLocation,
override val recipientType: WhatsAppCloudBotRecipientType,
override val userId: String? = null,
) : WhatsAppCloudBotMessage(WhatsAppCoudBotMessageType.location, userId) {
) : WhatsAppCloudBotMessage(WhatsAppCloudBotMessageType.location, userId) {
override fun toSendBotMessage(recipientId: String): WhatsAppCloudSendBotMessage =
WhatsAppCloudSendBotLocationMessage(
messagingProduct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotM
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotRecipientType
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotTemplateMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCoudBotMessageType
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudBotMessageType
import ai.tock.bot.engine.message.GenericMessage

data class WhatsAppCloudBotTemplateMessage(
override val messagingProduct: String,
val template: WhatsAppCloudBotTemplate,
override val recipientType: WhatsAppCloudBotRecipientType,
override val userId: String? = null,
) : WhatsAppCloudBotMessage(WhatsAppCoudBotMessageType.template, userId) {
) : WhatsAppCloudBotMessage(WhatsAppCloudBotMessageType.template, userId) {
override fun toSendBotMessage(recipientId: String): WhatsAppCloudSendBotMessage =
WhatsAppCloudSendBotTemplateMessage(
messagingProduct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ data class WhatsAppCloudBotTextMessage (
val text: TextContent,
override val recipientType: WhatsAppCloudBotRecipientType,
override val userId: String? = null,
) : WhatsAppCloudBotMessage(WhatsAppCoudBotMessageType.text, userId) {
) : WhatsAppCloudBotMessage(WhatsAppCloudBotMessageType.text, userId) {
override fun toSendBotMessage(recipientId: String): WhatsAppCloudSendBotMessage =
WhatsAppCloudSendBotTextMessage(
messagingProduct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import ai.tock.bot.connector.whatsapp.cloud.model.send.manageTemplate.ResponseCr
import ai.tock.bot.connector.whatsapp.cloud.model.send.manageTemplate.WhatsAppCloudTemplate
import ai.tock.bot.connector.whatsapp.cloud.model.send.media.FileType
import ai.tock.bot.connector.whatsapp.cloud.model.send.media.MediaResponse
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotTemplateMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotInteractiveMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotImageMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotLocationMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotTemplateMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.WhatsAppCloudSendBotTextMessage
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.Component
import ai.tock.bot.connector.whatsapp.cloud.model.send.message.content.HeaderParameter
Expand Down Expand Up @@ -68,8 +69,11 @@ class WhatsAppCloudApiService(private val apiClient: WhatsAppCloudApiClient) {
try {
when (messageRequest) {
is WhatsAppCloudSendBotTextMessage,

is WhatsAppCloudSendBotLocationMessage -> handleSimpleMessage(phoneNumberId, token, messageRequest)

is WhatsAppCloudSendBotImageMessage -> handleImageMessage(phoneNumberId, token, messageRequest)

is WhatsAppCloudSendBotInteractiveMessage -> handleInteractiveMessage(
phoneNumberId,
token,
Expand All @@ -83,6 +87,13 @@ class WhatsAppCloudApiService(private val apiClient: WhatsAppCloudApiClient) {
}
}

private fun handleImageMessage(phoneNumberId: String, token: String, messageRequest: WhatsAppCloudSendBotImageMessage) {
replaceWithRealMessageImageId(messageRequest, phoneNumberId, token)
send(messageRequest) {
apiClient.graphApi.sendMessage(phoneNumberId, token, messageRequest).execute()
}
}

private fun handleSimpleMessage(phoneNumberId: String, token: String, messageRequest: WhatsAppCloudSendBotMessage) {
send(messageRequest) {
apiClient.graphApi.sendMessage(phoneNumberId, token, messageRequest).execute()
Expand Down Expand Up @@ -302,6 +313,27 @@ class WhatsAppCloudApiService(private val apiClient: WhatsAppCloudApiClient) {
}
}

private fun replaceWithRealMessageImageId(
messageRequest: WhatsAppCloudSendBotImageMessage,
phoneNumberId: String,
token: String
) {
val client = OkHttpClient.Builder().apply(TockProxyAuthenticator::install).build()

var res = sendMedia(
client,
phoneNumberId,
token,
messageRequest.image.id,
FileType.PNG.type
)

val image = messageRequest.image
val newImageId = res.id
image.id = newImageId
}


private fun replaceWithRealImageId(
messageRequest: WhatsAppCloudSendBotTemplateMessage,
phoneNumberId: String,
Expand Down

0 comments on commit e5f360c

Please sign in to comment.