2 min read 2 days ago

Client-Side API (JS)

You can interact with the chatbots and conversations directly through JS. Have a look at the MwaiAPI object that it is available directly in your JS console. If you only use one chatbot, you can get the instance by using MwaiAPI.getChatbot(). If you have many, you can mention its ID as the first argument. For example, you could do:

let chatbot = MwaiAPI.getChatbot(); // Get the chatbot; if there are more than one on the same page, you'll need to pass the ID of the chatbot in argument
chatbot.open(); // Opens the chatbot if it's minimized
chatbot.ask("How are you?", false); // Sends a request (if true, the request will be send immediately)
chatbot.clear(); // Clears the chat

JS Filters

Modify the reply: ai.reply

MwaiAPI.addFilter('ai.reply', function (reply, args) {
  const { botId, chatId } = args;
  return reply + ' For more information, visit [Meow Apps](https://meowapps.com).';
});

Examples

MwaiAPI.addFilter('ai.reply', function (reply, args) {
  return reply + ' For more info, visit [Meow Apps](https://meowapps.com).';
});


Auto-Engage If User is Inactive for 30 Seconds

let inactivityTimer;
function resetInactivityTimer() {
  clearTimeout(inactivityTimer);
  inactivityTimer = setTimeout(() => {
    const chatbot = MwaiAPI.getChatbot();
    chatbot.ask("Still there? Let me know if you need help!", true);
  }, 30000); // 30 seconds
}

document.addEventListener('mousemove', resetInactivityTimer);
document.addEventListener('keydown', resetInactivityTimer);
resetInactivityTimer();

Show a Shortcut after a “result”

If you don’t know what a Shortcut is, it is basically a button that displays inside of the chatbot, you can learn more about it here.

MwaiAPI.addFilter('ai.reply', function (reply, args) {
    const { botId, chatId } = args;
    // Check if the reply is from the default bot (assumed ID 'default') and contains 'result'
    if (botId === 'default' && reply.toLowerCase().includes('result')) {
        let chatbot = MwaiAPI.getChatbot(botId);
        chatbot.setShortcuts([
            {
                type: 'message',
                data: {
                    label: 'Thanks',
                    message: 'Thank you!',
                    variant: 'success',
                    icon: null
                }
            }
        ]);
    }
    return reply;
});