https://discord.gg/botpress logo
Restore user data
# đŸ€help
c
I’m using a webchat that sets a custom userKey (from GHL CRM) to identify users across devices. What’s the correct way to make a returning user (on a new device or session) automatically load their previous data or profile when the chat starts? Is there an official way or endpoint to restore user data by userKey? Thanks! 🙏
w
you can save the userKey as a primary key in a database inside your botpress workflow
then query it on the conversation start to get the user's information (if there's any)
w
c
When a returning user connects with the same userKey, where does Botpress actually get their data from? Does it automatically restore the user variables from their previous conversation, or do I need to store those variables myself (e.g. in my CRM or a Data Table)?
I couldn't find any information about this.
w
Botpress creates a user ID and conversation ID that are saved in the local storage of the browser and in the internal memory of Botpress. These IDs are passed to the webchat, and you can access them in your bot using
{{ event.userId }}
and
{{ event.conversationId }}
If you need to pass other data from your CRM or page, read the [Send User Data documentation.](https://www.botpress.com/docs/webchat/interact/send-user-data)
1. You can save your data in the [User Scope](https://www.botpress.com/docs/learn/reference/variables/overview) If you want a structured and typed type use a [Schema](https://www.botpress.com/docs/learn/reference/schemas) 2. Use a Botpress [Table](https://www.botpress.com/docs/learn/reference/tables) as suggested by Shen 3. Call a CRM API/Webhook to store in your own table using [Execute Card](https://www.botpress.com/docs/learn/reference/cards/execute-code) and [axios](https://www.botpress.com/docs/learn/guides/advanced/exporting-data/exporting-raw-conversations-with-the-botpress-api) or using the integration HTTP Requests / API calls
c
Sorry, I’ve reviewed all the pages you linked, but I still couldn’t find an answer to this specific question: When I identify a returning user using their userKey, does Botpress automatically restore the previous user variables from their last conversation? Is there working code or built-in support for this — does the system actually do it, or not? Or do I need to store and reload those variables myself (for example in my CRM or a Data Table)? Can I please get a concrete answer to this question? 🙏
w
I apologize if my previous reply was unclear. Here are the steps you need to follow: 1. Send Custom Data: From your page, send the necessary custom data to the webchat. 2. Store User Data: Save the fields from {{ workflow.userData.userData }} into the User scope, either as a simple variable or by using a schema. 3. Persist Data: Store your data along with the userId and conversationId in one of the following options: - A Botpress table - Your own custom table - An external service For more information, please refer to the documentation on Botpress features that I posted earlier
c
I understand what you’re saying, but it still doesn’t answer my question. Let me try to explain more clearly... There is a user who previously interacted with the chatbot and provided their information, which we use to guide them through the workflow. This user received a userKey, which we saved. Later, if the same user comes back to my website from a different browser — and we pass the same userKey (and userId if needed) to the chatbot — then when the user triggers the workflow via a custom event, the userKey and userId are both available in the workflow. At that point, can Botpress restore the user’s previous data (from their earlier conversation and user variables) — either automatically or through a code card — or not?
w
@creamy-teacher-84823
c
So if I run this now — a test user just provided their information in the bot, and their userId was created. I copied that userId into the Get User Data card and set it to save the result into the workflow.userData variable. If I run this in the emulator, I should get that user’s data back, right? But this doesn’t return any result. https://cdn.discordapp.com/attachments/1435600782329253929/1436060254956683406/image.png?ex=690e3ad5&is=690ce955&hm=9d7c596a18225f8c89bb72804b45fd7a5f35687510c3bf1d134db175399715ba& https://cdn.discordapp.com/attachments/1435600782329253929/1436060255329980426/image.png?ex=690e3ad5&is=690ce955&hm=f21044730daeb438d2cd2d9f46bbf50334d73bdee3a2d2fe9d306de792a75b03&
w
you won't get anything back because you're in the emulator
and nothing there actually exists
c
Okay, I tried running the same thing live (not in the emulator), and it still doesn’t work. It only shows errors in the log — it doesn’t return any user data. Are you saying that if I add the Get User Data card to the workflow, and include a previous user’s
userId
in it, Botpress will restore that user’s data into the new conversation? https://cdn.discordapp.com/attachments/1435600782329253929/1436259334769213480/image.png?ex=690ef43d&is=690da2bd&hm=b9606d93ddf137e4080ff22d07ba363be26790bba54879bbd0f23644e7f781c7&
w
that appears to be a completely different error i think
either the conversation_id doesn't exist or you hardcoded it somewhere (the problem with this specific approach is that user and conversation ids in the emulator are different, they're something like "`convo_1`" whereas real user ids are something like "`conv_DU01JFH9SK1K9F9HSH19FHJ9S`)
c
Yes, I hardcoded a real
userId
of a user who previously submitted their data through the webchat. And I tried to fetch it from a completely new, real user. So this won’t work the way I’ve set it up in the attached screenshot? What’s the actual solution in this case? https://cdn.discordapp.com/attachments/1435600782329253929/1436425707495362721/image.png?ex=690f8f30&is=690e3db0&hm=4baae1a6ffb0cea43abb447ebec149e8653324fd5192c1a33bd33c1ba4f3c09c&
w
does that user have a conversation?
the previous error says the conversation doesn't exist
but you're only setting the user ID
w
I understand your use case, but there are a couple of things to clarify: - A user chatting from a different browser is the same as chatting from a different WhatsApp or Telegram account. - Therefore, a few aspects can change: - Message history is saved in the original account or browser. - The user ID and conversation ID should not be the same. - The userKey is the only common point.
c
So can I use the
userKey
to retrieve the data from the previous conversation? How?
w
You need to decide the store flow and: 1. If you have saved the userKey along with all user and conversation variables in a Botpress table, you need to receive the userKey with Get User Data to read values from the table and set your user and conversation variables. 2. If you have saved the data in your table, you have two options: - Read the values on your site and pass them all with Get User Data to set your user and conversation variables. - Pass the userKey to the chatbot, read the user data, and then use the userKey to call your API to retrieve the data from your table.
c
Sorry, that’s exactly what I was asking at the beginning, but I didn’t get a clear answer — or I got a different one. So, Botpress actually cannot return the user data from a previous conversation. I have to save and retrieve those myself — either in a Botpress Table or through an external CRM. Is that correct?
w
Exactly. - If you access from a different browser, Botpress cannot retrieve data because the user and conversation IDs are different. - However, if you do so in the same browser (and not in a private or anonymous session), Botpress can retrieve data. It saves the conversation ID and user ID in local storage if you have set Across browser tab cross in the web chat settings. https://cdn.discordapp.com/attachments/1435600782329253929/1436471673560760440/immagine.png?ex=690fb9ff&is=690e687f&hm=cdbe13292d8ebcc319ba775dff76b41252dcf08d7d590ea95129ac1d9f9b2895&
c
Thanks a lot! I see, great that we finally figured this out! 🙂 But what is the
userKey
actually useful for?
w
It's your CRM key and the field used to search for stored user and conversation variables
c
You’re saying “search for stored user and conversation variables,” but that’s exactly what I’ve been asking all along — how to do that. Last time you said it wasn’t possible, and now you’re saying it is. So could you please explain: HOW?