Messages sending twice
# 🤝help
s
Ive searched through discord however havent found any solution. Please let me know if I'm missing something obvious as to why it sends twice. I have setup triggers on open and it is sending twice on occasion. Also, if theres code that will stop it from sending double if you message it before the trigger activates that would be handy too, however thats a separate issue Trigger Code: window.botpressWebChat.onEvent( function() { window.botpressWebChat.sendEvent({ type: "loadConversation" }); window.botpressWebChat.onEvent( function(event) { console.log('RECEIVED FROM STUDIO ==>', event); // if (event.value.type === 'navigate') { window.location.href = event.value.url } }, ["TRIGGER"] ); }, ["LIFECYCLE.LOADED"] ); window.addEventListener( "message", (event) => { console.log("message from iframe", event); }, ); function sendPayload() { var textInput = document.getElementById('textInput'); var text = textInput.value; window.botpressWebChat.sendPayload({ type: "trigger", payload: { text: text, currentUrl: window.location.href } }); textInput.value = ''; } Thank you!

https://cdn.discordapp.com/attachments/1135785341178085426/1135785341593325598/Screen_Shot_2023-08-01_at_1.56.19_pm.png

https://cdn.discordapp.com/attachments/1135785341178085426/1135785341970817054/Screen_Shot_2023-08-01_at_2.02.53_pm.png

h
I am not sure why it's doing that but here is what I use
Copy code
js
window.addEventListener("load", function () {
// Sends the pay load 2 secs later
  setTimeout(function () {
              window.botpressWebChat.sendPayload({
                type: "trigger",
                payload: {
                  text: "startConversation",
                  currentUrl: window.location.href
                }
              });
            }, 2000);
// Opens the chat window
  window.botpressWebChat.sendEvent({ type: "show" });
  });
a
Hey @helpful-vr-6699 check out this triggers demo https://jsfiddle.net/GordyNumberOne/pbhf3L0d/71/ a couple notes on your code: * Triggers are separate from payloads.
sendEvent()
sends a trigger, which is caught by the trigger nodes.
sendPayload()
takes the same route as if the user sent a message, and that hits the start node not the trigger. * You could be hitting both start and trigger nodes, depending on how you're catching the "trigger" payload in your bot. This might be the source fo your double messaging
h
@acceptable-kangaroo-64719 this is for @salmon-midnight-96260, the code I have is from one of Botpress support staff. But I will have a look to make changes required. The trigger is at the start node so I guess that is why it works but this is great information to have, thank you
@acceptable-kangaroo-64719 Updated to sendEvent instead of sendPayload and I send the trigger and show the chat with a delay and seems to be working pretty good, thanks!
s
Would his solution apply to mine too @acceptable-kangaroo-64719
h
@salmon-midnight-96260 Can you tell me what you are trying to achieve exactly?
s
Open the chat button and it messages without the user having to message. The functionality works in that regard however in the second pic u can see it will send the message twice occasionally