Skip to content

Commit

Permalink
feat: Add custom attribute variables (#23)
Browse files Browse the repository at this point in the history
* feat: add custom variables

* chore: bump version
  • Loading branch information
muhsin-k committed Dec 9, 2023
1 parent c413ba9 commit a0bd051
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.16",
"version": "0.0.17",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand Down
32 changes: 30 additions & 2 deletions src/canned.ts
@@ -1,4 +1,9 @@
import { Conversation, Sender, Variables } from './types/conversation';
import {
Conversation,
Sender,
Variables,
CustomAttributes,
} from './types/conversation';
const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g;

const skipCodeBlocks = (str: string) => str.replace(/```(?:.|\n)+?```/g, '');
Expand Down Expand Up @@ -29,9 +34,11 @@ export const getMessageVariables = ({
const {
meta: { assignee, sender },
id,
custom_attributes: conversationCustomAttributes = {},
} = conversation;
const { custom_attributes: contactCustomAttributes } = sender || {};

return {
const standardVariables = {
'contact.name': capitalizeName(sender?.name || ''),
'contact.first_name': getFirstName({ user: sender }),
'contact.last_name': getLastName({ user: sender }),
Expand All @@ -44,6 +51,27 @@ export const getMessageVariables = ({
'agent.last_name': getLastName({ user: assignee }),
'agent.email': assignee?.email ?? '',
};
const conversationCustomAttributeVariables = Object.entries(
conversationCustomAttributes as CustomAttributes
).reduce((acc: CustomAttributes, [key, value]) => {
acc[`conversation.custom_attribute.${key}`] = value;
return acc;
}, {});

const contactCustomAttributeVariables = Object.entries(
contactCustomAttributes as CustomAttributes
).reduce((acc: CustomAttributes, [key, value]) => {
acc[`contact.custom_attribute.${key}`] = value;
return acc;
}, {});

const variables = {
...standardVariables,
...conversationCustomAttributeVariables,
...contactCustomAttributeVariables,
};

return variables;
};

export const replaceVariablesInMessage = ({
Expand Down
6 changes: 6 additions & 0 deletions src/types/conversation.ts
@@ -1,6 +1,7 @@
export interface Conversation {
meta: Meta;
id: number;
custom_attributes: CustomAttributes;
}

export interface Meta {
Expand All @@ -13,6 +14,7 @@ export interface Sender {
email?: string;
name?: string;
phone_number?: string;
custom_attributes?: CustomAttributes;
}

export interface Assignee {
Expand All @@ -25,3 +27,7 @@ export interface Assignee {
export interface Variables {
[key: string]: string | number;
}

export interface CustomAttributes {
[key: string]: any;
}
8 changes: 8 additions & 0 deletions test/canned.test.ts
Expand Up @@ -98,9 +98,14 @@ describe('#getMessageVariables', () => {
name: 'john Doe',
email: 'john.doe@gmail.com',
phone_number: '1234567890',
custom_attributes: { priority: 'high' },
},
},
id: 1,
custom_attributes: {
car_model: 'Tesla Model S',
car_year: '2022',
},
};
expect(getMessageVariables({ conversation })).toEqual({
'contact.name': 'John Doe',
Expand All @@ -114,6 +119,9 @@ describe('#getMessageVariables', () => {
'agent.first_name': 'Samuel',
'agent.last_name': 'Smith',
'agent.email': 'samuel@example.com',
'contact.custom_attribute.priority': 'high',
'conversation.custom_attribute.car_model': 'Tesla Model S',
'conversation.custom_attribute.car_year': '2022',
});
});
});
Expand Down

0 comments on commit a0bd051

Please sign in to comment.