acceptable-kangaroo-64719
08/04/2023, 10:24 AM<script>
tags on your webpage:
### Show bot on page load:
js
window.addEventListener('load', function() {
window.botpressWebChat.sendEvent({ type: "show" })
}
### Show bot after 5 seconds:
js
setTimeout(() =>window.botpressWebChat.sendEvent({type:'show'}), 5000);
This will only show the bot, it will not make the bot say something! To do that, see Triggers below!
### Show the bot on button click:
js
document.getElementById('myButton').addEventListener('click', function() {
window.botpressWebChat.sendEvent({ type: "show" })
});
Make sure to change "myButton" to your button's ID!acceptable-kangaroo-64719
08/04/2023, 10:29 AMjs
window.addEventListener('load', function() {
window.botpressWebChat.sendPayload({ type: "text", text: "Hello Chatbot!" })
}
### Send message to bot after 5 seconds:
js
setTimeout(() =>window.botpressWebChat.sendPayload({ type: "text", text: "Hello Chatbot!" }), 5000);
### Send message to bot on button click:
js
document.getElementById('myButton').addEventListener('click', function() {
window.botpressWebChat.sendPayload({ type: "text", text: "Hello Chatbot!" })
});
Make sure to change "myButton" to your button's ID!acceptable-kangaroo-64719
08/04/2023, 10:36 AMjs
window.addEventListener('load', function() {
window.botpressWebChat.sendEvent({ type: "trigger"})
}
### Send trigger to bot after 5 seconds:
js
setTimeout(() =>window.botpressWebChat.sendEvent({ type: "trigger"}), 5000);
### Send trigger to bot on button click:
js
document.getElementById('myButton').addEventListener('click', function() {
window.botpressWebChat.sendEvent({ type: "trigger"}));
});
Then, in your bot:
1. Publish your bot if you haven't already
2. Add a trigger node by right clicking in the studio and selecting "trigger."
3. In the trigger's inspector panel (right side of screen) select "Custom Trigger Advanced" for trigger type
4. Connect your new trigger node to wherever you want the bot to start when it is triggered
5. Publish your bot (again)
Back in your webpage, you should now be able to trigger your bot with whatever event you chose!acceptable-kangaroo-64719
08/04/2023, 10:48 AMhtml
<label for="textInput" id="textInput'">Enter your name:</label>
<input type="text" id="name" name="nameInput" required>
<label for="colorSelect">Choose your favorite color:</label>
<select id="colorSelect" name="colorSelect">
<option value="red">🔴 Red</option>
<option value="blue">🔵 Blue</option>
<option value="green">🟢 Green</option>
<option value="yellow">🟡 Yellow</option>
</select>
<button id="submit">Submit</button>
Then, add an event listener for the submit button (put this in <script>
tags). When the event is fired, we want to do two things:
1. Pull the information from our inputs
2. Send this information to the bot
Let's start off with extracting the information
js
document.getElementById('submit').addEventListener('click', function() {
//Get the name from the text box
const inputValue = document.getElementById('name').value;
//Get the webpage's URL
const currentURL = window.location.href;
// Get the selected color from the dropdown
const dropdown = document.getElementById("colorSelect")
const color = dropdown.options[dropdown.selectedIndex].text;
Next, we send this information in a payload to the bot. Notice how we put what we want the user to see in text
and our data goes in payload
.
js
window.botpressWebChat.sendPayload({
type:'text',
text:"This is what the user will see in their chat",
payload:{
name:inputValue,
favColor: color,
url: currentURL
}
})
acceptable-kangaroo-64719
08/04/2023, 10:50 AMevent.payload.payload
2. Save data to workflow variables with this code:
js
let { name, color, url } = event.payload.payload
workflow.name = name
workflow.color = color
workflow.url = url
3. Tell it back to the user by putting a nice message in a text card.
md
Thanks for filling out the survey, {{workflow.name}}! Your favorite color is {{workflow.color}} and you're joining us from {{workflow.url}}.
4. (Optional) Save this data to a tablefresh-fireman-491
08/25/2023, 7:13 PMshy-room-21068
08/27/2023, 2:19 PMlemon-receptionist-60189
09/27/2023, 2:52 PMacceptable-kangaroo-64719
09/27/2023, 4:51 PMjs
window.botpressWebChat.sendEvent({ type: "show"})
and then use a "Conversation Start" trigger in the botlemon-receptionist-60189
09/27/2023, 4:53 PMacceptable-kangaroo-64719
09/27/2023, 4:54 PMlemon-receptionist-60189
09/27/2023, 4:55 PMlemon-receptionist-60189
09/27/2023, 5:22 PMmelodic-salesclerk-70876
12/12/2023, 7:10 AMmelodic-salesclerk-70876
12/12/2023, 7:10 AM