Skip to content
Asterlio

Examples

Recipes for common integrations.

Open the chat from your own button

JavaScript
document.querySelector('#open-help').addEventListener('click', () => {
  window.ASTERLIO_API.expand()
})

Ask about a product with a prefilled message

Before the first chat there is no composer, so queue the draft: the visitor completes the form and the message is waiting for them.

JavaScript
document.querySelector('#ask-about-product').addEventListener('click', async () => {
  const { queued } = await window.ASTERLIO_API.openChat({
    message: `Question about ${document.title}`,
    queue: true,
  })
  if (queued) console.log('The draft appears once the visitor starts the chat')
})

Attach a file from your page

JavaScript
const input = document.querySelector('#receipt')
input.addEventListener('change', async () => {
  try {
    await window.ASTERLIO_API.openChat({ files: input.files })
  } catch (error) {
    if (error.code === 'invalid_file' || error.code === 'too_many_files') alert(error.message)
    if (error.code === 'first_chat_required') alert('Start a chat first, then attach your receipt.')
  }
})

Identify a signed-in visitor

JavaScript
await window.ASTERLIO_API.ready()
if (window.ASTERLIO_API.getCapabilities().needsFirstChat) {
  await window.ASTERLIO_API.setUser(
    { email: currentUser.email, phone: currentUser.phone, ticket_category: 'Support' },
    { mode: 'silent' },
  )
}

Resume a conversation after sign-in, end it on sign-out

JavaScript
async function onSignIn(account) {
  if (account.chatSessionId) {
    await window.ASTERLIO_API.restoreSession(account.chatSessionId).catch(() => {})
  }
  window.ASTERLIO_API.on('session:changed', ({ sessionId }) => saveChatSession(account, sessionId))
}

async function onSignOut() {
  await window.ASTERLIO_API.clearSession()
}

Hide the widget on some pages

JavaScript
const hideOn = ['/checkout', '/payment']
if (hideOn.some((path) => location.pathname.startsWith(path))) {
  window.ASTERLIO_API.hide()
}

Place the widget on the left

persist: false moves the widget on this page without overwriting the corner remembered for the visitor.

JavaScript
window.ASTERLIO_API.setPosition('bottom-left', { persist: false })

Enable your own 'Message us' button only when it will work

JavaScript
const button = document.querySelector('#message-us')
const sync = (caps) => {
  button.disabled = !(caps.canSendMessage || caps.canStartChat)
  button.title = caps.blockedReason ?? ''
}
window.ASTERLIO_API.on('capabilities:changed', sync)
window.ASTERLIO_API.ready().then(() => sync(window.ASTERLIO_API.getCapabilities()))

Mirror the unread badge

JavaScript
const badge = document.querySelector('#unread-badge')
window.ASTERLIO_API.on('unread:changed', ({ count }) => {
  badge.textContent = String(count)
  badge.style.display = count > 0 ? 'inline-flex' : 'none'
})

Custom error toast

JavaScript
window.ASTERLIO_API.on('error', ({ code, message }) => {
  showToast(message || 'An error occurred') // your UI library
})

Was this page helpful?