Send a message to user chat from the Execution Cod...
# 🤝help
c
Hello everyone, I am trying to programmatically send a message to user chat from the Execution Code panel. Botpress AI is suggesting to use a HTTP call to the Botpress API, but that ain't working Any ideas how I can do that? Something like:
Copy code
const messages = [{ text: '12313132131' }]

for (const message of messages) {
  await client.createMessage({
    payload: { type: 'text', text: message.text },
    'x-user-key': event.userId,
    conversationId: event.conversationId
  })
}
https://cdn.discordapp.com/attachments/1245095412897943562/1245095413019840577/Captura_de_Tela_2024-05-28_as_16.03.29.png?ex=665780f7&is=66562f77&hm=17613ff8d8d654a109df54c4cf1eef22022c5ba40cf3ef382cb7a5b7dca11dc7&
a
Hey @cuddly-elephant-53641 , could you please elaborate on what you want to happen? You could also send me a report id so I can take a look at your bot @square-energy-41150
s
# Sending a Problem Report Here's how to send the Botpress Team a problem report: 1. In the Botpress Cloud Studio, locate the "Help" icon in the bottom left corner of the screen. 2. Click "Report a problem" and follow the steps on the screen. 3. Copy the Report ID to your clipboard and provide it to a member of the Botpress Team on Discord.
message has been deleted
c
Hey @aloof-traffic-22451 , so basically I have an HTTP call that happens inside the Execution Code step, and it returns a list of delivery types and prices the user may choose from Something like this: Fast - 100$ Normal - 95$ Slow - 90$ PickUp - 80$ And I want to present to the user a dynamic list of options based on the response of the HTTP call. So if the response returns 5 options, I want to show all 5 to user, and if it returns only 2, I want to show only those. Since I don't know how to do this, I was trying to do it using javascript inside the Execution Code step. Is it clear now?
a
Yep. Have you considered storing the fetched data in a variable and displaying to user via text card? I would advise against sending a message to user via http as it may disrupt the flow of the boat due to the asynchronous nature
c
Yes I have considered that, however, since I don't know how many items the list may have, I am not sure how to do it
If it was a fixed list of 3 for example, it would be easy, I could just use 3 texts, one for each item of the list
Is this possible?
This would be extremely easy if I could write messages in the chat from the Execution Code panel, I could just use
bot.sendMessage(option)
in a for loop and it would solve the problem
or even if I could do something like
bot.sendSingleChoice(choices)
a
Does http request return string of this format but with different number of options?
Copy code
Fast - 100$
Normal - 95$
Slow - 90$
PickUp - 80$
It would be easier to parse the string into desired format
c
That's just an example, the list is based on the users locations, so in a given city I may have all 4 options available for the user to choose from, but in another area there might be only one delivery type available. The HTTP call actually return a list of objects, each object containing the name of the delivery type available and the price:
Copy code
[
    {
        "id": 1,
        "description": "Fast: Up to 30 min,
        "unit_price": 112,
        "formatted_unit_price": "$ 112,00"
    },
    {
        "id": 2,
        "description": "Normal: up to 60 min",
        "unit_price": 109,
        "formatted_unit_price": "$ 109,00"
    },
    {
        "id": 3,
        "description": "Scheduled: choose a time of the time",
        "unit_price": 106,
        "formatted_unit_price": "$ 106,00"
    {
        "id": 4,
        "description": "Pick up: Choose a location to pick up your item",
        "unit_price": 85,
        "formatted_unit_price": "$ 85,00"
    }
]
In this example, there are 4 available options, but for a given location, there might be just one or two
Botpress Studio is incridibly powerful in what it can do, but if it also had the option to interact with the chat from the Execution Code step, everything would be possible hahaha
c
Interesting, can that also be done using Text?
a
Sure. You can make a function that takes those options as a parameter and generates a string
c
But it would a single string, right? Not a list of texts
a
What do you meany by list of texts?
c
I mean, instead of being 4 separate texts sent to the user, it would be a single text containing all info
a
Exactly. If your goal is to display shipping options to a user, you can also look into AI task
c
Can you send me a link to the documentation explaning how to do it using AI task?
a
546 Views