https://discord.gg/botpress logo
Whatsapp returning user
# 🤝help
f
Hello! I'm building an assistant, the objetive and problem are: When a WhatsApp user messages the bot, I save their phone number, then ask info about them. On future conversations or after a timeout, I want the bot to read the phone number from the incoming event, find the existing record in my UserTable, retrieve the saved fields, and greet them by name etc... If there is no record, capture the data I've managed to save the whatsapp number on the table using event.tags.conversation['whatsapp:userPhone']; The user name and other info all that is beign saved with no issue. My main issue is I can't find a way that if the user returns after timeout I can use the whatsapp number, check if it exists, if yes use the records already saved for that user, using the phone number as identifier Is there a way to do that? I tried searching the docs and using chatgpt but i feel i'm going in circles 😢 Thanks for the helpp!! 🙂
a
Hey, I can help you sort that out in Botpress. You can run a check at the start of each conversation using the phone number from event.tags.conversation['whatsapp:userPhone']. If the number exists in your UserTable, load the saved data and greet the user by name; if not, start the info collection flow again. Are you currently using a custom action or a hook to save and fetch the user data? @fierce-daybreak-40159
w
Hi, - save the last conversation ID in the table or in the user scope. - in the main workflow, after the Start node, check if the saved conversation ID and the current one are equal to display the correct message.
f
I managed that if it matches it goes to if_matching, but I can't find a way to extract the data (When it goes into that flow) I cant find the way that it just says the first name for example: So the Check_user execute code I'm using
Copy code
workflow.phone = event.tags.conversation['whatsapp:userPhone'];
Then the condition / expression is
Copy code
(
  Array.isArray(workflow.lookup)
    ? workflow.lookup.length
    : (
        workflow.lookup && workflow.lookup.items
          ? workflow.lookup.items.length
          : 0
      )
) > 0
Then inside the if_exists the code is
Copy code
// Use the row we already found
const r = workflow.lookup
const row = Array.isArray(r) ? r[0] : r?.items?.[0]

if (row) {
  workflow.userRow = row
  workflow.userId = row.id
  workflow.userName = row.fields?.Name || null
  workflow.NombreEmpresa = row.fields?.NombreEmpresa || null
  workflow.TiempoEmpresa = row.fields?.TiempoEmpresa || null

  // Cache to user scope for future conversations
  if (workflow.userName) user.name = workflow.userName
  if (workflow.NombreEmpresa) user.company = workflow.NombreEmpresa
  if (workflow.TiempoEmpresa) user.yearsAtCompany = workflow.TiempoEmpresa
}
Sorry I'm new into this and coding is not my best https://cdn.discordapp.com/attachments/1426359711216697465/1426560529077637150/image.png?ex=68ebab86&is=68ea5a06&hm=25fc35d933ccbc9d4b2518846f73d529cdbc17ac37fedc77b47711688fcb2056& https://cdn.discordapp.com/attachments/1426359711216697465/1426560529379491941/Screenshot_2025-10-11_at_2.41.41_p.m..png?ex=68ebab86&is=68ea5a06&hm=9a1c90c4e16f24aeff0bc0e707f8961d35d7b2a6fcf00bc7ce2b8e0c20f8f5e7& https://cdn.discordapp.com/attachments/1426359711216697465/1426560529643737170/image.png?ex=68ebab86&is=68ea5a06&hm=9f57c6a09837a6a3a5b020d681bd3c8d55ce95a3b8bb615926eb5141dc324571&
w
Hi, - I would store the WhatsApp phone number in the user scope:
user.phone = event.tags.conversation['whatsapp:userPhone']
. - I would simplify the transition expression to:
workflow.lookup && workflow.lookup.length > 0
. - I would store values only in user scope variables, as the user scope is visible throughout all the bot workflows.
Copy code
ts
// Use the row we already found
const row = workflow.lookup[0]

user.userRow = row
user.userId = row.id
user.name = row.Name
user.company = row.NombreEmpresa
user.yearsAtCompany = row.TiempoEmpresa
- in text and in the single choice
Copy code
Hola! {{user.name}} Bienvenido de vuelta!

Bienvenido {{user.name}} a Bistro 702,