JavaScript API
Control the widget from your own page: open it from your buttons, ask a question for the visitor, say who is logged in, and react when someone leaves their details.
Set up
Add one line before the widget snippet. Calls made before the widget has loaded are kept and run once it has.
index.html
<script>
window.replybash = window.replybash || function () { (replybash.q = replybash.q || []).push(arguments) }
</script>
<script src="https://replybash.com/embed.js" data-agent-id="acme_7Hq2k" defer></script>Commands
| Call | What it does |
|---|---|
| replybash("open") | Opens the chat. |
| replybash("open", { message: "Do you ship to Canada?" }) | Opens the chat and asks that question as the visitor. |
| replybash("close") | Closes the chat. |
| replybash("toggle") | Opens it if closed, closes it if open. |
| replybash("hide") | Hides the round button or the ask bar (the chat still opens from code, e.g. your own Help button). |
| replybash("show") | Shows the round button or the ask bar again. |
| replybash("identify", { name, email }) | Tells the agent who is logged in: their name and email are kept on the chat, so you don't have to ask for them. |
| replybash("reset") | Call on logout: forgets the visitor and the open chat, so the next person starts fresh. |
| replybash("setSuggestedMessages", ["Where is my order?"]) | For this page view: replaces the starter questions (up to 5, 80 characters each) in the chat, and on the ask bar or under the popup where you have starters switched on. null goes back to yours. |
| replybash("setInitialMessages", ["Hi from pricing!", "Ask me about plans."]) | For this page view: replaces the greeting; each text is its own bubble (up to 3, 500 characters each). null goes back to yours. |
| replybash("on", event, fn) | Listens to an event. replybash("off", event, fn) stops. |
Events
Events are only sent to your own page (its origin), never to other sites. To hear about chats and form answers on your server instead, use webhooks.
| Event | When, and what your function gets |
|---|---|
| widget-opened, widget-closed | The chat was opened or closed. |
| user-message | The visitor sent a message: { text, files } (files: the name and kind of any attached). |
| assistant-message | The agent's reply finished: { text }. |
| conversation-started | A new conversation began on this page view (after its first reply, or when a voice call starts it): { conversationId }. |
| handoff-requested | The agent asked a teammate to join the chat: { conversationId }. |
| form-submitted | A form was sent: { form: { key, title }, answers } (answers by field id). |
| cart-added | Shopify stores: the visitor added a product to the cart from the chat: { variantId, quantity }. Refresh your theme's cart drawer here. |
Examples
Count a lead in Google Analytics when a form is sent, and open the chat from your own Help button:
app.js
replybash("on", "form-submitted", ({ form }) => {
if (form.key === "contact") gtag("event", "generate_lead", { method: "chat" })
})
document.querySelector("#help").addEventListener("click", () => replybash("open"))Tell the agent who is logged in, and forget them on logout:
your page
replybash("identify", { name: "Maya Chen", email: "maya@northwind.io" })
// On logout, so the next person on this browser starts fresh:
replybash("reset")