Skip to content

feat: add inbox support to message variables handling #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Feel free to send us feedback on [Twitter](https://twitter.com/chatwootapp) or [

If there's anything you'd like to chat about, please feel free to join our [Discord](https://discord.gg/cJXdrwS) chat!

_Chatwoot_ © 2017-2021, Chatwoot Inc - Released under the MIT License.
_Chatwoot_ © 2017-2025, Chatwoot Inc - Released under the MIT License.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.47",
"version": "0.0.48",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand Down
5 changes: 5 additions & 0 deletions src/canned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Variables,
CustomAttributes,
Contact,
Inbox,
} from './types/conversation';
const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g;

Expand Down Expand Up @@ -41,9 +42,11 @@ export const getLastName = ({ user }: { user: Sender }) => {
export const getMessageVariables = ({
conversation,
contact,
inbox,
}: {
conversation: Conversation;
contact?: Contact;
inbox?: Inbox;
}) => {
const {
meta: { assignee, sender },
Expand All @@ -60,6 +63,8 @@ export const getMessageVariables = ({
'contact.phone': sender?.phone_number,
'contact.id': sender?.id,
'conversation.id': id,
'inbox.id': inbox?.id,
'inbox.name': inbox?.name,
'agent.name': capitalizeName(assignee?.name || ''),
'agent.first_name': getFirstName({ user: assignee }),
'agent.last_name': getLastName({ user: assignee }),
Expand Down
6 changes: 6 additions & 0 deletions src/types/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Conversation {
meta: Meta;
id: number;
code: string;
custom_attributes: CustomAttributes;
first_reply_created_at: number;
waiting_since: number;
Expand Down Expand Up @@ -28,6 +29,11 @@ export interface Contact {
custom_attributes?: CustomAttributes;
}

export interface Inbox {
id: number;
name: string;
}

export interface Assignee {
id: number;
email?: string;
Expand Down
19 changes: 18 additions & 1 deletion test/canned.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const variables = {
'contact.email': '[email protected]',
'contact.phone': '1234567890',
'conversation.id': 1,
'conversation.code': 'CW123',
'inbox.id': 1,
'inbox.name': 'Inbox 1',
'agent.first_name': 'Samuel',
'agent.last_name': 'Smith',
'agent.email': '[email protected]',
Expand Down Expand Up @@ -52,6 +55,13 @@ describe('#replaceVariablesInMessage', () => {
);
});

it('returns the message with inbox name', () => {
const message = 'Welcome to {{inbox.name}}';
expect(replaceVariablesInMessage({ message, variables })).toBe(
'Welcome to Inbox 1'
);
});

it('returns the message if the variable is not present in variables', () => {
const message = 'Please dm me at {{contact.twitter}}';
expect(replaceVariablesInMessage({ message, variables })).toBe(
Expand Down Expand Up @@ -102,6 +112,7 @@ describe('#getMessageVariables', () => {
},
},
id: 1,
code: 'CW123',
custom_attributes: {
car_model: 'Tesla Model S',
car_year: '2022',
Expand All @@ -117,15 +128,21 @@ describe('#getMessageVariables', () => {
phone_number: '1234567890',
custom_attributes: { priority: 'high' },
};
const inbox = {
id: 1,
name: 'Inbox 1',
};

expect(getMessageVariables({ conversation, contact })).toEqual({
expect(getMessageVariables({ conversation, contact, inbox })).toEqual({
'contact.name': 'John Doe',
'contact.first_name': 'John',
'contact.id': 3,
'contact.last_name': 'Doe',
'contact.email': '[email protected]',
'contact.phone': '1234567890',
'conversation.id': 1,
'inbox.id': 1,
'inbox.name': 'Inbox 1',
'agent.name': 'Samuel Smith',
'agent.first_name': 'Samuel',
'agent.last_name': 'Smith',
Expand Down
6 changes: 6 additions & 0 deletions test/sla.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('SLAHelper', () => {
},
},
id: 1,
code: 'CW123',
custom_attributes: {},
first_reply_created_at: 0,
waiting_since: 0,
Expand Down Expand Up @@ -74,6 +75,7 @@ describe('SLAHelper', () => {
},
},
id: 1,
code: 'CW123',
custom_attributes: {},
first_reply_created_at: 0,
waiting_since: 0,
Expand Down Expand Up @@ -110,6 +112,7 @@ describe('SLAHelper', () => {
},
},
id: 1,
code: 'CW124',
custom_attributes: {},
first_reply_created_at: 1704066200,
waiting_since: 1704065940,
Expand Down Expand Up @@ -145,6 +148,7 @@ describe('SLAHelper', () => {
},
},
id: 1,
code: 'CW125',
custom_attributes: {},
first_reply_created_at: 1704066200,
waiting_since: 1704066060,
Expand Down Expand Up @@ -180,6 +184,7 @@ describe('SLAHelper', () => {
},
},
id: 1,
code: 'CW126',
custom_attributes: {},
first_reply_created_at: 1704066200,
waiting_since: 0,
Expand Down Expand Up @@ -215,6 +220,7 @@ describe('SLAHelper', () => {
},
},
id: 1,
code: 'CW127',
custom_attributes: {},
first_reply_created_at: 1704066200,
waiting_since: 0,
Expand Down