This is a very basic chatbot example.
The business logic can be implemented in the reply private method
of the Chatbot REST API external object which is designed to be
called in POST with a JSON body.
To add the widget to the generic UI add this to the SCRIPT resource
of the responsive5 disposition:
(function() {
$(document).on('ui.ready', () => {
// Other UI ready stuff...
if ($grant.hasResponsibility("CHATBOT")) {
$ui.load([
{ name: 'CHATBOT_SCRIPT', type: 'JS' },
{ name: 'CHATBOT_STYLES', type: 'CSS' }
]).then(() => {
const chat = ChatWidget.init();
chat.onSend(msg => {
chat.setTyping(true);
fetch('/ui/ext/Chatbot', {
method: 'POST',
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: msg })
}).then(res => res.json()).then((data) => {
chat.setTyping(false);
chat.addMessage('agent', data.reply);
}).catch(err => {
chat.setTyping(false);
chat.addMessage('agent', $T('ERROR'));
console.error(err);
});
});
}).catch(err => {
console.error(err);
});
}
});
})();