I have my chatbot now integrated on a website. I a...
# 💻developers
b
I have my chatbot now integrated on a website. I also want an additional button that opens the chatbot, how would the javascript code look like to open the chat from a button?
a
I'll be putting out a video on this later this week, but here's a sneak peek of the code to open the bot when you click a button:
Copy code
js
var detail;
var botLoaded = false;
var botReady = false;
var submitted = false;

function showBot(){
    console.log("Showing bot....")
  window.botpressWebChat.sendEvent({type:'show'})
}

function sendFormInfo(info){
  console.log("Triggering bot with payload...")
  window.botpressWebChat.sendPayload({ type: 'trigger', payload:info })
}
document.getElementById('myButton').addEventListener('click', function(event) {
  event.preventDefault(); 
  // collect details from the webpage if you want to do this
  detail ={
    name: event.target.name.value,
    email: event.target.email.value,
    linkedin: event.target.catster.value,
    message: event.target.message.value,
  }
  detail['position'] = 'Cat Wrangler'
  showBot()
  console.log("Submitting....")
  submitted=true;
});

window.botpressWebChat.onEvent( function (event) {
    console.log(event)
  if(event.type === 'LIFECYCLE.LOADED'){
         botLoaded = true;
      }
  else if(event.type === 'LIFECYCLE.READY'){
         botReady = true;
    if(botLoaded && botReady && submitted){
      sendFormInfo(detail)
      }
      }
},['LIFECYCLE.LOADED', 'LIFECYCLE.READY']
)
b
Thank you very much, will this video be on the botpress youtube channel?
a
yep