Last active
April 26, 2024 10:51
-
-
Save edonosotti/de376e6a510e09c891489f11b5e4f9b3 to your computer and use it in GitHub Desktop.
How to integrate chatbase in a chatbot project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Integrating Chatbase in your chatbot - Node.JS version | |
# | |
# The purpose of this code is to show how to integrate the Chatbase service in any chatbot. | |
# | |
# See my article on Chatbots Magazine for details: | |
# https://chatbotsmagazine.com/advanced-chatbot-analytics-and-sentiment-analysis-part-1-17a8e674d7e1 | |
# | |
# License: MIT (https://opensource.org/licenses/MIT) (C) Edoardo Nosotti, 2017 | |
const chatbase = require('@google/chatbase'); | |
var intent = "ASK-CURRENT-TIME"; // This should be actually decoded from the user message! | |
// Create a Message Set | |
// See: https://github.com/google/chatbase-node | |
var messageSet = chatbase.newMessageSet() | |
.setApiKey("APIKEY") // Chatbase API key | |
.setPlatform("facebook-or-whatever"); // Chat platform name | |
// Track the message from the user | |
const userMessage = messageSet.newMessage() // Create a new instance of Message | |
.setAsTypeUser() // Mark it as a message coming from the human | |
.setUserId("facebook-user-XYZ") // User ID on the chat platform, or custom ID | |
.setTimestamp(Date.now().toString()) // Mandatory | |
.setIntent(intent) // The intent decoded from the user message, if applicable | |
.setMessage("Do you know the time, please?"); // User message | |
// Was the intent successfully decoded? | |
if (intent == "UNKNOWN") { | |
userMessage.setAsNotHandled(); // Tell Chatbase to mark this user request as "not handled" | |
} else { | |
userMessage.setAsHandled(); // Mark this request as successfully handled ;) | |
} | |
// Track the response message from the bot | |
const botMessage = messageSet.newMessage() // See above | |
.setAsTypeAgent() // This message is the bot response | |
.setUserId("facebook-user-XYZ") // Same as above | |
.setTimestamp(Date.now().toString()) // Mandatory | |
.setMessage("It's 12 o'clock!"); // Bot response message | |
// Send all messages to Chatbase | |
return messageSet.sendMessageSet() | |
.then(response => { | |
var createResponse = response.getCreateResponse(); | |
return createResponse.all_succeeded; // "true" if all messages were correctly formatted and have been successfully forwarded | |
}) | |
.catch(error => { | |
console.error(error); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TY