Here's a Deluge code example for integrating Zoho Sales IQ with Zoho Creator to automate form entries via chat/voice inputs. This includes context management, mandatory field validation, and record creation.
1. Prerequisites
- Zoho Sales IQ Setup:
- Create a bot in Sales IQ and connect it to your website/app.
- Configure a Webhook in Sales IQ to send chat/voice inputs to Zoho Creator (use the Creator API endpoint).
- Zoho Creator Form:
- Create a form (e.g.,
Chat_Sessions) to track conversation context (fields: session_id, context (JSON), last_updated).
- Create your target form (e.g.,
Sales_Orders) with fields like customer_name, product, quantity, address.
2. Deluge Code (Triggered by Sales IQ Webhook)
// Deluge Script: Process Chat/Voice Input from Zoho Sales IQ
// Trigger: When a new record is added to a "Chat_Input" form (fields: session_id, message, is_voice)
// Define mandatory fields for the target form (e.g., Sales_Orders)
mandatoryFields = Map();
mandatoryFields.put("customer_name","");
mandatoryFields.put("product","");
mandatoryFields.put("quantity","");
mandatoryFields.put("address","");
// Get incoming data from Sales IQ
incomingData = input; // Input from webhook (e.g., {session_id: "123", message: "I want 5 widgets", is_voice: false})
// Fetch or initialize conversation context
sessionId = incomingData.get("session_id");
context = getOrCreateContext(sessionId);
// Process the message (text or voice)
userMessage = incomingData.get("message");
if(incomingData.get("is_voice") == true)
{
// If voice, Sales IQ sends transcribed text; no extra processing needed here
}
// Extract intent and entities from message
parsedData = parseMessage(userMessage, context);
// Update context with new data
context = updateContext(context, parsedData);
// Check if all mandatory fields are filled
missingFields = getMissingFields(context, mandatoryFields);
// Respond to user or create record
if(missingFields.size() > 0)
{
// Ask for missing fields
responseMessage = "I need more info: " + missingFields.toString().replace("[","").replace("]","");
sendResponseToSalesIQ(sessionId, responseMessage);
}
else
{
// All fields present: create record
createSalesOrder(context);
responseMessage = "Order created! Order ID: " + zoho.creator.getLastInsertID("Sales_Orders");
sendResponseToSalesIQ(sessionId, responseMessage);
}
// Function to get or create conversation context
getOrCreateContext(sessionId)
{
// Check if context exists in the Chat_Sessions form
existingContext = zoho.creator.getRecord("your_account", "your_app", "Chat_Sessions", sessionId);
if(existingContext.isEmpty())
{
// Create new context
newContext = Map();
newContext.put("session_id",sessionId);
newContext.put("context",Map()); // Empty initial context
newContext.put("last_updated",zoho.currenttime);
zoho.creator.createRecord("your_account", "your_app", "Chat_Sessions", newContext);
return Map();
}
else
{
return existingContext.get("context"); // Return stored context
}
}
// Function to parse message and extract data
parseMessage(message, context)
{
parsed = Map();
// Example: Extract customer name (simple keyword matching)
if(message.containsIgnoreCase("my name is") || message.containsIgnoreCase("I'm"))
{
nameMatch = message.getMatch("my name is (.+?)( |$)");
if(nameMatch.size() > 0)
{
parsed.put("customer_name",nameMatch[1]);
}
}
// Extract product and quantity (e.g., "I want 5 widgets")
productMatch = message.getMatch("I want (\d+) (.+?)( |$)");
if(productMatch.size() > 0)
{
parsed.put("quantity",productMatch[1]);
parsed.put("product",productMatch[2]);
}
// Extract address (e.g., "My address is 123 Main St")
addressMatch = message.getMatch("address is (.+)");
if(addressMatch.size() > 0)
{
parsed.put("address",addressMatch[1]);
}
// Add more patterns as needed
return parsed;
}
// Function to update context with parsed data
updateContext(oldContext, newData)
{
updatedContext = oldContext.clone(); // Clone to avoid modifying original
foreach key in newData.keySet()
{
updatedContext.put(key, newData.get(key));
}
// Save updated context to Chat_Sessions form
contextRecord = Map();
contextRecord.put("session_id",sessionId);
contextRecord.put("context",updatedContext);
contextRecord.put("last_updated",zoho.currenttime);
// Update or create record
existing = zoho.creator.getRecord("your_account", "your_app", "Chat_Sessions", sessionId);
if(existing.isEmpty())
{
zoho.creator.createRecord("your_account", "your_app", "Chat_Sessions", contextRecord);
}
else
{
zoho.creator.updateRecord("your_account", "your_app", "Chat_Sessions", sessionId, contextRecord);
}
return updatedContext;
}
// Function to check missing mandatory fields
getMissingFields(context, mandatoryFields)
{
missing = List();
foreach field in mandatoryFields.keySet()
{
if(context.get(field) == null || context.get(field) == "")
{
missing.add(field);
}
}
return missing;
}
// Function to send response back to Sales IQ
sendResponseToSalesIQ(sessionId, message)
{
// Use Zoho Creator's HTTP client to call Sales IQ's API
// Replace with your Sales IQ webhook URL and auth details
salesIQWebhook = "https://salesiq.zoho.com/api/v1/bot/your_bot_id/sendMessage";
payload = Map();
payload.put("session_id",sessionId);
payload.put("message",message);
headers = Map();
headers.put("Authorization","Zoho-oauthtoken your_auth_token");
headers.put("Content-Type","application/json");
// Send POST request
response = zoho.creator.httpPost(salesIQWebhook, payload.toString(), headers);
}
// Function to create Sales Order in Creator
createSalesOrder(context)
{
orderData = Map();
orderData.put("customer_name",context.get("customer_name"));
orderData.put("product",context.get("product"));
orderData.put("quantity",context.get("quantity").toNumber()); // Convert to number
orderData.put("address",context.get("address"));
// Create record in Sales_Orders form
result = zoho.creator.createRecord("your_account", "your_app", "Sales_Orders", orderData);
// Handle success/error
if(result.get("code") == 3000)
{
info "Order created: " + result.get("ID");
}
else
{
error "Failed to create order: " + result.get("message");
}
}
3. Key Features
-
Context Management:
- Stores conversation state in a
Chat_Sessions form to remember user inputs across messages.
- Updates context incrementally as the user provides more info.
-
Mandatory Field Validation:
- Checks against a
mandatoryFields map to ensure all required data is collected.
-
Voice/Chat Support:
- Treats voice inputs (transcribed by Sales IQ) the same as chat text.
-
Sales IQ Integration:
- Uses
sendResponseToSalesIQ to send prompts back to the user via Sales IQ's API.
4. Setup Steps
-
In Zoho Sales IQ:
- Create a bot and connect it to your website/app.
- Configure a Webhook to send chat/voice data to your Creator form (e.g.,
Chat_Input).
-
In Zoho Creator:
- Create forms:
Chat_Input (to receive webhook data) and Chat_Sessions (to store context).
- Create your target form (e.g.,
Sales_Orders).
- Add the Deluge code to a workflow triggered when a new
Chat_Input record is created.
-
Configure API Access:
- Get your Sales IQ API token and replace
your_auth_token in sendResponseToSalesIQ.
- Update
your_account, your_app, and form names in the code.
5. Example Conversation Flow
- User (via Sales IQ): "I want 5 widgets"
- Bot (via Sales IQ): "I need more info: [customer_name, address]"
- User: "My name is John"
- Bot: "I need more info: [address]"
- User: "Address is 123 Main St"
- Bot: "Order created! Order ID: SO-001"
6. Enhancements
- Advanced NLP: Integrate with Zoho's Zia AI or external NLP services (e.g., Dialogflow) for better intent/entity extraction.
- Error Handling: Add try-catch blocks to handle API failures.
- Field Validation: Add regex checks (e.g., validate email/phone format).
- Voice Optimization: Use Sales IQ's voice features to enable hands-free interaction.
Let me know if you need help with specific parts!