Search code examples
javascriptnode.jsbotpress

Restore existing conversation of custom userId in Botpress


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

Solution

  • 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.