0

On our website, we use Botpress' website embedding function

<script>
  window.botpressWebChat.init({
    host: OWN_URL:BOTPRESS_PORT,
    botId: "test12345"
  })
</script>

which displays the bot and allows interacting with it. Up to this point, everything works as expected.

However, in the next step, we intend to define a custom userId to store existing conversations and restore them, when logging back into the system. While Botpress allows defining a custom userId, it seems not to be used for storing and reloading the current conversation at a later point. Instead, even when initializing the chat/conversation with this userId:

<script>
  window.botpressWebChat.init({
    host: OWN_URL:BOTPRESS_PORT,
    botId: "test12345",
    userId: "example12345"
  })
</script>

then retrieving the conversationId from the Networks-Tab and trying to reload the conversation in another window using:

<script>
  window.botpressWebChat.init({
    host: OWN_URL:BOTPRESS_PORT,
    botId: "test12345",
    userId: "example12345",
    conversationId: ID_RETRIEVED_FROM_NETWORKS_TAB
  })
</script>

Botpress sends a "The conversation ID doesn't belong to that user" error message, which can be found within this file.

Debugging the point of error tells us that even though the conversation is found:

{
  id: 'd3178f66-3b4f-493e-aa39-ce8fcc2195a3',
  clientId: '076ea353-fcd7-4984-869e-713e12b0176f',
  userId: 'a317d806-a6ad-493f-b3b3-70c792f29445',
  createdOn: 2022-07-15T16:39:56.523Z
}

the userId differs (e.g., 705d31e8-c6e0-4e7b-a28f-f0da46390653) -> conversation.userId !== userId.

It seems like setting a userId when initializing the bot (or at a later point – we tried this as well) does not have an influence on the userId set in the conversation.

Now the question is as follows: How can you restore existing conversations of a custom userId in Botpress? Or is this a Pro-feature when referring to user authentication?

Our main idea is just about using an existing user (of our system), e.g., example12345 to have a conversation and when logging back into the system (from another computer, for instance) to restore this conversation.

Thanks for any hint!

Additional links:

  • Botpress inject-Script which is used for website embedding can be found here
  • Issue showing how to "test" with Botpress can be found here
Tommy
  • 2,355
  • 1
  • 19
  • 48

1 Answers1

2

TL;DR

Any alphanumeric string from 24 to 40 characters long will work for userId.

Research

According to the source code, a valid userId must be less than USER_ID_MAX_LENGTH caracters long which is 40 and match the /[a-z0-9-_]+/i pattern.

Although your example value (example12345) meets the criteria, it indeed doesn't work.

You seem to indicate that the framework generates UUIDs, so, after testing some, turns out UUIDs work.

nanoid uses the same pattern by default, so, after testing some, turns out any string works starting from 24 characters long.

Reproduction

The following code exposes nanoId and initializes the framework with a userId coming from localStorage :

<script src="http://localhost:3000/assets/modules/channel-web/inject.js"></script>
<script type="module">
    import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js';
    window.nanoid = nanoid;

    window.botpressWebChat.init({
        host: "http://localhost:3000",
        botId: "test",
        userId: window.localStorage.getItem('userId')
    });
    setTimeout(() => window.botpressWebChat.sendEvent({ type: 'show' }), 1000);
</script>

Step 1 : generate a first ID and say Hi Step 1 Step 2 : generate a second ID and say Hi Step 2 Step 3 : reload the first ID and retrieve my previous Hi (notice the timestamp checks out) Step 3

Additional info

When testing for the first time, I noticed I didn't need to specify userId to retrieve my conversation after reloading.

KaKi87
  • 915
  • 7
  • 15
  • It seems like that you saved our day or maybe the entire week. Please let me verify that your solution works in our production environment and I will accept it and award the bounty afterwards. Thank you so much! – Tommy Jul 18 '22 at 14:02
  • 1
    You're welcome ! If my research turns out to be conclusive, you may report the min length bug on the repo *(or I could, but it would be less appropriate as I'm not an actual user of the framework)*. Perhaps they should also know that their RegExp doesn't validate the string from start to end (`userId` = `###` is invalid, but `a##`, `#a#` and `##a` are). Surrounding the expression with `^` and `$` would solve this. – KaKi87 Jul 18 '22 at 14:37
  • As far as we could test in our production environment, everything works as expected, despite some console errors due to an undefined `currentConversation` within the `lite.bundle.js`. However, we guess that initializing the chat with the previous `conversationId` or updating the version of Botpress used helps at solving this issue, as well. Thanks again for helping us! And please feel free to report the bug you described above. I am pretty sure that the developers would be interested in it as well. – Tommy Jul 19 '22 at 13:29