Shiks254
03/20/2025, 2:00 PMShubham Bajaj
03/22/2025, 6:38 AMjavascript
const IDLE_MESSAGES_MULTILINGUAL = {
"en": [
"Are you still there?",
"Is there anything else you need help with?",
"Feel free to ask me any questions."
],
"es": [
"¿Sigues ahí?",
"¿Hay algo más en lo que pueda ayudarte?",
"No dudes en hacerme cualquier pregunta."
],
"fr": [
"Êtes-vous toujours là?",
"Y a-t-il autre chose dont vous avez besoin?",
"N'hésitez pas à me poser des questions."
],
// Add more languages as needed
};
2. API Batch Request Implementation
Here's how you can implement a webhook or API endpoint that responds to assistant requests and sets the appropriate idle messages based on language:
javascript
// Example implementation for a webhook handling assistant-request
app.post('/webhook/assistant-request', async (req, res) => {
// Extract data from the incoming request
const { call, customer, assistant } = req.body;
// Determine language from call context or customer preference
// This could come from customer data, call metadata, or other sources
const language = customer?.preferredLanguage || call?.detectedLanguage || 'en';
// Get idle messages for the determined language, fallback to English if not available
const idleMessages = IDLE_MESSAGES_MULTILINGUAL[language] || IDLE_MESSAGES_MULTILINGUAL['en'];
// Create assistant overrides or a new assistant configuration
const response = {
// You can either reference an existing assistant and override its messagePlan
assistantId: assistant?.id,
assistantOverrides: {
messagePlan: {
idleMessages: idleMessages,
// Keep other messagePlan settings like timeout the same or override them
idleTimeoutSeconds: 10,
idleMessageMaxSpokenCount: 3
}
},
// Alternatively, you can create a completely new assistant on the fly
/*
assistant: {
name: `Multilingual Assistant (${language})`,
messagePlan: {
idleMessages: idleMessages,
idleTimeoutSeconds: 10,
idleMessageMaxSpokenCount: 3
},
// Include other required assistant configuration
model: {...},
voice: {...},
...
},
*/
};
// Send back the response
res.json(response);
});Shubham Bajaj
03/22/2025, 6:38 AMassistant-request to your webhook, and you can respond with the appropriate language-specific idle messages.
Example configuration:
json
{
"webhooks": {
"assistantRequest": "https://your-api.com/webhook/assistant-request"
}
}
#### Option 2: Using the API Directly
Create multiple assistant variants for different languages and use the appropriate one based on the customer's language preference:
javascript
// Using the VAPI API to create a new assistant with language-specific idle messages
const createMultilingualAssistant = async (language) => {
const idleMessages = IDLE_MESSAGES_MULTILINGUAL[language] || IDLE_MESSAGES_MULTILINGUAL['en'];
const assistantConfig = {
name: `Support Assistant (${language.toUpperCase()})`,
messagePlan: {
idleMessages: idleMessages,
idleTimeoutSeconds: 10,
idleMessageMaxSpokenCount: 3
},
// Other assistant configuration
model: {...},
voice: {...},
// Language-specific system prompt if needed
firstMessage: `Hello! I'm your assistant. How can I help you today?`
};
const response = await fetch("https://api.vapi.ai/assistant", {
method: "POST",
headers: {
"Authorization": `Bearer ${YOUR_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify(assistantConfig)
});
return response.json();
};
// Create assistants for multiple languages
const createAllLanguageAssistants = async () => {
const languages = ["en", "es", "fr", "de", "ja"];
const assistants = {};
for (const lang of languages) {
assistants[lang] = await createMultilingualAssistant(lang);
}
return assistants;
};
### Implementation Example: Dynamic Language Selection in Call Routing
You can also implement a system that determines the language dynamically during the call and switches assistants if needed:
javascript
app.post('/webhook/assistant-request', async (req, res) => {
const { call } = req.body;
// Initial response with default language (English)
if (!call.detectedLanguage) {
return res.json({
assistantId: "your-default-assistant-id",
// Any other configuration
});
}
// If language is detected during the call, select the appropriate assistant
const languageToAssistantMap = {
"en": "english-assistant-id",
"es": "spanish-assistant-id",
"fr": "french-assistant-id",
// Add more mappings
};
// Get the appropriate assistant ID or use default
const assistantId = languageToAssistantMap[call.detectedLanguage] ||
languageToAssistantMap["en"];
return res.json({
assistantId: assistantId
});
});
This implementation allows you to provide a fully multi-lingual experience for your VAPI assistants, including dynamic idle messages that match the user's language preferences.Shiks254
04/07/2025, 8:15 AMAditya
04/07/2025, 3:53 PM