The Message Types module in the Kanvas Core SDK provides functionality for managing and querying different types of messages within the Kanvas ecosystem. This module allows for the creation, updating, and retrieval of message types, enabling a more structured and categorized approach to messaging.
- Create new message types
- Update existing message types
- Retrieve message types with filtering capabilities
- Support for multilingual message templates
Access the Message Types module through the messagesTypes
property on your KanvasCore instance:
const kanvas = new KanvasCore({...});
const messageTypes = kanvas.messagesTypes;
Creates a new message type.
const newMessageType = await messageTypes.createMessageType({
languages_id: 1,
name: 'System Notification',
verb: 'notify',
template: 'System: {{message}}',
templates_plura: 'System: {{message}} ({{count}} notifications)'
});
console.log(newMessageType.id);
Parameters:
input
: MessageTypeInputInterface object containing message type details
Returns a MessageTypeInterface
object representing the created message type.
Updates an existing message type.
const updatedMessageType = await messageTypes.updateMessageType(123, {
name: 'Updated System Notification',
template: 'System Update: {{message}}'
});
console.log(updatedMessageType.name);
Parameters:
id
: Message type IDinput
: MessageTypeInputInterface object with updated details
Returns the updated MessageTypeInterface
object.
Retrieves message types based on specified criteria.
const systemMessageTypes = await messageTypes.getMessageTypes({
column: 'NAME',
operator: 'LIKE',
value: '%System%'
});
console.log(systemMessageTypes);
Parameters:
where
: Optional query conditions for filtering message types
Returns an array of MessageTypeInterface
objects.
interface MessageTypeInputInterface {
languages_id: number;
name: string;
verb: string;
template: string;
templates_plura: string;
}
interface MessageTypeInterface {
id: number;
languages_id: number;
apps_id: number;
uuid: string;
name: string;
verb: string;
description: string;
templates_plura: string;
}
- Use descriptive names and verbs for message types to ensure clarity.
- Implement a consistent naming convention for message types across your application.
- Utilize templates effectively to create dynamic and context-aware messages.
- Consider internationalization needs when creating message types and templates.
- Regularly audit and update message types to ensure they remain relevant and effective.
- Use message types consistently across your application to maintain a unified messaging experience.
- Duplicate Message Types: Ensure unique combinations of name and verb to avoid conflicts.
- Template Rendering Issues: Verify that all placeholders in templates are properly defined and used.
- Localization Problems: Check that language IDs are correct and templates are properly localized.
Implement try/catch blocks for all message type operations:
try {
await messageTypes.createMessageType(newMessageTypeData);
} catch (error) {
console.error('Failed to create message type:', error.message);
// Handle error (e.g., show user feedback, log error)
}
Common error scenarios:
- Invalid input data
- Duplicate message type
- Language not found
- Network failures
- Implement proper access controls for message type management.
- Validate and sanitize all input data, especially template strings, to prevent injection attacks.
- Be cautious about exposing sensitive information in message templates.
- Implement logging for message type operations for audit purposes.
- Combine with the Messages module to create a robust messaging system.
- Utilize message types in conjunction with the Notifications module for structured notifications.
- Integrate with a localization system for managing multilingual message templates.
- Use message types to categorize and filter messages in user interfaces.
Implement a function to render message templates dynamically:
function renderMessageTemplate(template: string, data: Record<string, any>): string {
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => data[key] || match);
}
// Usage
const messageType = await messageTypes.getMessageTypes({ column: 'VERB', operator: 'EQ', value: 'notify' })[0];
const renderedMessage = renderMessageTemplate(messageType.template, { message: 'Server maintenance scheduled' });
console.log(renderedMessage);
Implement bulk operations for efficient management:
async function bulkCreateMessageTypes(typesData: MessageTypeInputInterface[]) {
return Promise.all(typesData.map(data => messageTypes.createMessageType(data)));
}
async function bulkUpdateMessageTypes(updates: Array<{id: number, data: Partial<MessageTypeInputInterface>}>) {
return Promise.all(updates.map(({id, data}) => messageTypes.updateMessageType(id, data)));
}
// Usage
const newTypes = await bulkCreateMessageTypes([
{ languages_id: 1, name: 'User Welcome', verb: 'welcome', template: 'Welcome, {{username}}!', templates_plura: 'Welcome, new users!' },
{ languages_id: 1, name: 'Order Confirmation', verb: 'confirm_order', template: 'Your order #{{order_id}} is confirmed.', templates_plura: 'Orders confirmed.' }
]);
await bulkUpdateMessageTypes([
{ id: 1, data: { template: 'Updated: Welcome, {{username}}!' } },
{ id: 2, data: { name: 'Updated Order Confirmation' } }
]);
Implement analytics to track usage of different message types:
async function getMessageTypeUsage(timeRange: { start: Date, end: Date }) {
const allTypes = await messageTypes.getMessageTypes();
const usage = await Promise.all(allTypes.map(async type => {
const count = await getMessageCountByType(type.id, timeRange); // Implement this based on your data structure
return { id: type.id, name: type.name, count };
}));
return usage.sort((a, b) => b.count - a.count);
}
// Usage
const typeUsage = await getMessageTypeUsage({ start: new Date('2023-01-01'), end: new Date() });
console.log('Most used message types:', typeUsage.slice(0, 5));
By effectively utilizing the Message Types module, you can create a structured and flexible messaging system within your Kanvas-powered application. This module enables you to categorize messages, implement dynamic templates, and support multilingual content, enhancing the overall communication capabilities of your platform.