Thursday, May 16, 2024
HomeGolangAssist me with a perform - Getting Assist

Assist me with a perform – Getting Assist


Good afternoon, I’ve the next perform:

func generateCarouselContent(attachments string, msg courier.Msg) map[string]interface{} {
carouselContent := make(map[string]interface{}, 0)

for _, attachment := vary attachments {
	_, mediaURL := handlers.SplitAttachment(attachment)
	carouselItem := map[string]interface{}{
		"textual content":  msg.Textual content(),
		"picture": mediaURL,
	}

	buttons := make([]map[string]interface{}, 0) // Slice para almacenar los botones

	for _, quickReplyCarousel := vary msg.QuickReplies() {
		// Verificamos si quickReply es una URL o texto
		if strings.HasPrefix(quickReplyCarousel, "http") {
			buttons = append(buttons, map[string]interface{}{
				"textual content":         "Open URL",
				"kind":         "web_url",
				"postbackData": "myCustomPostbackData",
				"information":         quickReplyCarousel,
			})
		}
	}

	if len(buttons) > 0 {
		carouselItem["buttons"] = buttons // Agregamos los botones al elemento del carrusel si hay alguno
	}

	carouselContent = append(carouselContent, carouselItem)
}

return carouselContent

}

As seen within the picture, it seems 2 instances within the textual content “Open URL” and I solely need it to seem as soon as.

Might you assist me or have any strategies?

Hello @Ivan_Garcia,

I’m not positive if I perceive what the code does. It could appear that buttons accumulates all of the buttons from each quickReplyCarousel. So for every ingredient in msg.QuickReplies(), one button will get added to buttons.

After the loop, the whole buttons map with all buttons in it’s assigned to the map carouselItem with the important thing "buttons".

If msg.QuickReplies() incorporates two components with prefix “http”, buttons will include two buttons named “Open URL”.

Possibly it’s extra of a knowledge downside than a code downside.

Hey Cristophberger, thanks in your assist.

This code is used for RCS messages and significantly I’m implementing it in a picture carousel with one hyperlink per picture, as I present within the screenshot, it’s printing 2 hyperlinks.

I hope you’ve made me perceive jejejeje

Regards.

So is my fast “evaluation” of the code appropriate?

In that case, the answer may be to examine what msg.QuickReplies() truly incorporates. If it incorporates two components with prefix http, then clearly, the information delivered to your perform already incorporates this duplicate.

Does this make sense?

To elaborate additional, after discovering the primary fast reply with prefix http, the for loop might break to keep away from amassing additional strings with the identical prefix.

This is able to not repair the duplicate information, however a minimum of forestall the duplicate from showing within the UI.

Thanks very a lot in your assist, what I’ll do is remove the whole lot repeated from the association to see if it really works for me.

I’ll inform evr the way it went.

Greetings.

If msg.QuickReplies() incorporates a couple of occasion with “http” prefix, then there shall be a couple of button in return. Imho it wants the replace of the filter, which creates buttons. Or add particular ingredient of the buttons:

if len(buttons) > 0 {
	carouselItem["buttons"] = buttons[0]
}

Hello @lemarkar Thanks in your feedback, it helped me loads, the difficulty of duplicity has now been resolved.

A brand new downside arose, when printing the buttons, the identical hyperlink seems for all of the buttons when totally different hyperlinks are configured, hehehe is left with solely the primary hyperlink and repeats it within the “n” buttons.

I share my perform with you, I thanks prematurely in your assist.

func generateCarouselContent(attachments []string, msg courier.Msg) []map[string]interface{} {
	carouselContent := make([]map[string]interface{}, 0)

	for _, attachment := vary attachments {
		_, mediaURL := handlers.SplitAttachment(attachment)
		carouselItem := map[string]interface{}{
			"textual content":  msg.Textual content(),
			"picture": mediaURL,
		}

		buttons := make([]map[string]interface{}, 0) // Almacenar los botones únicos

		// Verificar si se ha encontrado al menos una quickReply que sea una URL
		foundURLQuickReply := false
		quickReplyIndex := 0

		for foundURLQuickReply == false && quickReplyIndex < len(msg.QuickReplies()) {
			quickReplyCarousel := msg.QuickReplies()[quickReplyIndex]

			// Verificar si quickReply es una URL o texto
			if strings.HasPrefix(quickReplyCarousel, "http") {
				// Agregar botón solo si es una URL y aún no se ha encontrado ninguna
				foundURLQuickReply = true

				buttons = append(buttons, map[string]interface{}{
					"textual content":         "Open",
					"kind":         "web_url",
					"postbackData": "myCustomPostbackData",
					"information":         quickReplyCarousel,
				})
			}

			quickReplyIndex++
		}

		if len(buttons) > 0 {
			carouselItem["buttons"] = buttons
		}

		carouselContent = append(carouselContent, carouselItem)
	}

	return carouselContent
}

If I acquired your code proper, then the issue is in quickReplyIndex. You outline it on each attachment iteration and it turns into 0. I assume, it is best to strive it outdoors the outer loop in the beginning of the perform:

quickReplyIndex := 0

for _, attachment := vary attachments {
    // Your code right here...
}

However this creates a confusion and doable panic scenario. What if attachments are nonetheless there, however your quickReplyIndex is out of vary. Do I perceive it proper, that quantity of attachments equals the quantity of strings with “http” prefix in msg.QuickReplies()?

That’s proper, you understood it completely, I’ll strive your resolution and I’ll share with you the way it turned out, I actually admire your assist.

I’ve applied the change and it really works accurately!!! Thanks very a lot in your assist, in reality it was that, quickReplyIndex := 0 will need to have been outdoors the loop, because it all the time initialized it to 0 in every loop.

thanks a lot.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments