Chat JS workflows
Customer’s ID is prepopulated on the webchat by the embedding site. This ID is then used to assign the chat to a record. If no record is found, then a new one is created. Afterwards, the chat is moved to a queue.
function chatStarted(ctx) {
// Your code here, for example:
console.log("Chat started!");
vcc.setOnMessage(saveCustomerAndAssignChat);
}
// Function to save customer and assign the chat
function saveCustomerAndAssignChat(ctx) {
var clientid = vcc.getVariable('clientid');
// Search for an existing customer record by clientId
var records = vcc.searchCustomer("client_id", clientid);
if (records && records.rows.length > 0) {
// If the customer exists, update their record and assign the chat
vcc.assignCustomer(records.rows[0].id, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
// If customer doesn't exist, create a new customer
var newCustomerId = vcc.createCustomer([{
"form": {
"client_id": clientid
}
}]);
// If the new customer is created successfully, assign the chat
if (newCustomerId != 0) {
vcc.assignCustomer(newCustomerId, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
}
// Function to add to the queue if no response within timeout
function addToQueue(ctx) {
vcc.addToQueue({queueId: 1}); // Add the chat to queue with ID 3803
console.log("No response in time, added to queue.");
}
In this scenario, the system asks the customer to provide a Customer ID manually. This ID is then used to assign the chat to a record. If no record is found, then a new one is created. Afterwards, the chat is moved to a queue.
function chatStarted(ctx) {
// Your code here, for example:
console.log("Chat started!");
vcc.setOnMessage(askCustomerId);
}
function askCustomerId(ctx) {
// Asking for Customer ID
vcc.askVariable({
nextAction: saveCustomerAndAssignChat, // This function will be triggered after the question
variable: "customerid", // Variable name to store the response
question: "What is your Customer ID?", // The question to ask
// timeout: 30, // Wait for 30 seconds before timing out
// timeoutAction: addToQueue // Action to take if the timeout expires
});
}
// Function to save customer and assign the chat
function saveCustomerAndAssignChat(ctx) {
var customerid = vcc.getVariable('customerid');
// Search for an existing customer record by clientId
var records = vcc.searchCustomer("customerid", customerid);
if (records && records.rows.length > 0) {
// If the customer exists, update their record and assign the chat
vcc.assignCustomer(records.rows[0].id, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
// If customer doesn't exist, create a new customer
var newCustomerId = vcc.createCustomer([{
"form": {
"customerid": customerid
}
}]);
// If the new customer is created successfully, assign the chat
if (newCustomerId != 0) {
vcc.assignCustomer(newCustomerId, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
}
// Function to add to the queue if no response within timeout
function addToQueue(ctx) {
vcc.addToQueue({queueId: 1}); // Add the chat to queue with ID 3803
console.log("No response in time, added to queue.");
}
Comments
Can’t find what you need? Use the comment section below to connect with others, get answers from our experts, or share your ideas with us.
There are no comments yet.