botpress / v12

Botpress OSS – v12
https://v12.botpress.com
GNU Affero General Public License v3.0
68 stars 83 forks source link

[BUG] Proactive triggers on embedded webchat works intermittently #1426

Closed charlescatta closed 2 years ago

charlescatta commented 2 years ago

Describe the bug When trying to call a proactive trigger on slow connections on "webchatLoaded" the trigger sometimes does not get sent and errors occur, this also happens when trying to reset the conversation on "session_reset"

Related botpress/v12#1317

Error: image

To Reproduce Steps to reproduce the behavior: here is the example embedded webpage javascript

      window.addEventListener("message", (event) => {
        if (event.data && event.data.name === "webchatLoaded") {
          window.botpressWebChat.mergeConfig({
            layoutWidth: "100%",
          });

          window.botpressWebChat.sendEvent({ type: "show" });

          window.botpressWebChat.sendEvent({
            type: "proactive-trigger",
            channel: "web",
            payload: {
              text: "bonjour",
            },
          });
        }

        if (event.data && event.data.message_type === "session_reset") {
          window.webchat_store.clearMessages();
          window.botpressWebChat.sendEvent({
            type: "proactive-trigger",
            channel: "web",
            payload: {
              text: "bonjour",
            },
          });
        }
      });

full webpage: embed-clear-on-reset-html.txt

Expected behavior The proactive trigger should be sent once the webchat is ready

Environment:

laurentlp commented 2 years ago

@charlescatta The Pull Request I opened will now display a warning in the console if the user tries to send proactive messages on webchatLoaded. To make your example work, the proactive message should be sent on webchatReady.

Also, the session_reset event is never called. You would need to check the value from event.data.payload.type instead of event.data.message_type.

Here is a working version of embed-clear-on-reset-html.txt

Please let me know if the documentation says the contrary or if you have seen examples that contradict what I just detailed so I can adjust them.