Extract from history
# 🤝help
l
[whatsapp] Received list reply from Whatsapp: { id: '4', title: 'Breakfast', description: 'Breakfast Healthy Meal' }
Using Raw Input, I do not get a choice to extract id, it by default extracts title
Field validation successful! Extracted: "breakfast". Input: 4
Any suggestions?
s
I use AI Task for strings that are not consistent (same) each time Task: Extract the last "id" value from the array. Save extracted "id" value to variable In the input field write explicitly that this is an array content: Array: Store result in variables: Or you can use regular expressions in Execude code: // The string containing the object-like data let dataString = "{ id: '4', title: 'Breakfast', description: 'Breakfast Healthy Meal' }"; // Regular expression to find the 'id' value let idRegex = /id:\s*'(\d+)'/; // Extract the 'id' using the regular expression let match = dataString.match(idRegex); // Check if a match was found and log the id if (match) { let id = match[1]; console.log(id); // Outputs: 4 } else { console.log("No ID found"); } Choose the one you like the best.
l
Thanks @square-toddler-30705. However, neither of the methods will work, since Botpress does not give direct access to the received data from WhatsApp. If there was a way to extract id, I could have easily solved this. This is where I find difficulty with Botpress. Another case. Received an array with id, title, and description. extracted value was something else
Copy code
[whatsapp] Received list reply from Whatsapp: {
  id: '1',
  title: 'Jumbo Meal',
  description: 'Jumbo Meal for Lunch and Dinner'
}
[bbf13878b8]    [dm]    :     Message of type text received from user_01HV6XVCSMZCZD0C7MRM5SYEZH on whatsapp
[bbf13878b8]    [agent-hook]    [KnowledgeAgent] [conversation_turn_started]:     Skipping KB: disabled for the current turn
[bbf13878b8]    [bot-hook]    [after_incoming_middleware] [Tokens]:     Executing hook
[bbf13878b8]    [dm]    :     [Capture] User provided a value:
[bbf13878b8]    [dm]    :     [Capture] Transformed the extracted value
[bbf13878b8]    [dm]    :     [Capture] Prompt success
[bbf13878b8]    [dm]    :     [Capture] Field validation successful! Extracted: ["lunch",1]. Input: 1
s
But you used the validation. I don't know how it works and how to control it properly (validation option)
If you use the code I sent you it will work - tested.
l
below is my Json
Copy code
const mealMenu = {
  purpose: 'sendmessage',
  message_data: {
    messaging_product: 'whatsapp',
    recipient_type: 'individual',
    to: user.phoneNumber,
    type: 'interactive',
    interactive: {
      type: 'list',
      header: {
        type: 'text',
        text: 'ATM Tiffins'
      },
      body: {
        text: 'Choose from Breakfast, Lunch or Dinner'
      },
      action: {
        button: 'Select Meal',
        sections: [
          {
            title: 'Choose',
            rows: [{
                       id: meal.id,
                       title: meal.name,
                       description: meal.description
              }
           ]
          }
        ]
      }
    }
  }
}
It gives users a Single Choice Card, from where the user selects an option However, since this is not a standard Botpress Single Choice Card used, it only shows the user selection in the logs. To handle it, I am using a raw input card retrieved from history to extract the received response and hence the whole problem https://cdn.discordapp.com/attachments/1229364809179467818/1229398986763538563/image.png?ex=662f8a07&is=661d1507&hm=79cfe25d2949f064438c3332bf584fa5480d946a9c9ae022a0ace1420e74ac10&
Each execute card is a single choice
I know how to solve it, but it will mean a lot more code and workflow handling, which I do not want write🥹
For all means @fresh-fireman-491 @famous-jewelry-85388 Raw Input, should be a Raw Input, why should it interpret and transform data
s
But you get this from the whatsapp, correct? Or I'm missing something?
l
Yes @square-toddler-30705 yes the response is from WhatsApp, but not from a Botpress-initiated single choice card
f
What data does it transform?
l
[whatsapp] Received list reply from Whatsapp: { id: '1', title: 'Jumbo Meal', description: 'Jumbo Meal for Lunch and Dinner' } to [Capture] Field validation successful! Extracted: ["lunch",1]. Input: 1
or [whatsapp] Received list reply from Whatsapp: { id: '4', title: 'Breakfast', description: 'Breakfast Healthy Meal' } to Field validation successful! Extracted: "breakfast". Input: 4
check these, in case one the extraction was a string, in case two it was an array
at first, I thought it was extracting title, but then I realized, it is not!
Is there an way raw input can extract values as is?
f
I am sorry, but I am a bit confused here. Isn't that a single choice, and not a raw input?
l
hahaha... it is a single choice, but not the single choice card, since the single choice card does not allow customization... as you see, instead of the standard "Choose..." text I am displaying "Select Meal" as text
Well, while the Raw input remains as is. I found if I extract 2 levels from history, I am getting the value I need, Although not totally in my control what is would present to me, in this case, it is working
Copy code
whatsapp] Handler received request from Whatsapp with payload: {"object":"whatsapp_business_account","entry":[{"id":"302242106299282","changes":[{"value":{"messaging_product":"whatsapp","metadata":{"display_phone_number":"918660829530","phone_number_id":"293125593881024"},"contacts":[{"profile":{"name":"Lijo Mathew"},"wa_id":"919741391700"}],"messages":[{"context":{"from":"918660829530","id":"wamid.HBgMOTE5NzQxMzkxNzAwFQIAERgSN0NGM0U1Q0E0ODYyMEM5MTE4AA=="},"from":"919741391700","id":"wamid.HBgMOTE5NzQxMzkxNzAwFQIAEhggQkUwNzc4NzI2QzdBQTk1MEIyQzFFRDNCRjQyRjBCQzYA","timestamp":"1713186940","type":"interactive","interactive":{"type":"list_reply","list_reply":{"id":"4","title":"Breakfast","description":"Breakfast Healthy Meal"}}}]},"field":"messages"}]}]}
[whatsapp] Received list reply from Whatsapp: { id: '4', title: 'Breakfast', description: 'Breakfast Healthy Meal' }
extract history 1 level gives me and array like [breakfast, 4], etc. However, extract history 2 levels, gives me just 4
Capture] Field validation successful! Extracted: "4". Input: 4
Very unpredictable, with every selection the response is vivid 😭 I need some consistency Please help
f
I can see the problem here. I am unfortunately in no power to do anything about it, but I am sure that Bassam can take a look, and let the Botpress team know more about it
l
@famous-jewelry-85388 Do you have any solution here?
s
I would suggest using a Single Choice card, then changing the type of the message and testing it because the Raw Input might have some issues (I reported some). I don't know why, but this configuration doesn't have errors. If I use the Raw Input card, then there is an error. You must test this idea - no guarantee it will work in your case - sorry 😉 https://cdn.discordapp.com/attachments/1229364809179467818/1232739648044929125/image.png?ex=662a8dc3&is=66293c43&hm=dfd6bad78f1d53ad93967ab518a2709c2f04c8662904b9e57fd3632d5e36cd8c&
l
Thanks @square-toddler-30705 - I am not using Single Choice at all, so this may not work
s
I mean, it's not a single choice - it's a single choice card but switched to Raw Input
For me it's a workaround
l
ok will try, I get what you were pointing to. Will keep you posted
s
Great, maybe it will work - strange but it solved my errors
f
Hi @limited-pencil-78283 Are you still facing this issue? Can you guide me a little bit please? I get lost easily in details 🙂 Can you sharre with me from business perspective what are you trying to achieve?
Why are we switching between choices and raw input?
l
Hi @famous-jewelry-85388 I am using WhatsApp Interactive message instead of Single Choice.
here is the code, and hence you see the switch between the execute card and raw input. The execute card is sending the single choice and then the next card is waiting for user input followed by a raw input that collects data from history
f
why don:t you use the wait card?
l
My observation is that the input received gets interpreted by AI and transforms the value
You can see that here
I have used a wait card, the issue is not with the card, the issue is the interpretation and transformation of the received input eg. Extracted: ["lunch",1]. Input: 1
What I have observed is that at times the button response is captured and presented as an array, in some cases it is presented as a string. For now, I have written a code to check if the output is an array or a string and managed it
f
hmmmm....
let me check and get back to you
cna you guve me a report id?
l
sure
report_01HWB1WX5X4QTYJS5DW0M3R53C
Field validation successful! Extracted: ["lunch",1]. Input: 1 => Array Field validation successful! Extracted: "breakfast". Input: 4 => String
f
Thank you sir
5 Views