Skip to content

Commit 2e3fa9b

Browse files
authored
Create conversation without contact reply (#319)
* Added "create_conversation_without_admin_reply" to messages.create * Made createConversation ... optional * Add integration test * Documentation updated to reflect addition * Message can be created without conversation + clean up
1 parent c9a0f7c commit 2e3fa9b

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,24 @@ const response = await client.messages.create({
10131013
});
10141014
```
10151015

1016+
#### [Create conversation without contact reply](https://developers.intercom.com/intercom-api-reference/reference/admin-initiated-conversation)
1017+
1018+
```typescript
1019+
const response = await client.messages.create({
1020+
messageType: 'inapp',
1021+
body: 'Look at me, I am a conversation now',
1022+
from: {
1023+
type: 'admin',
1024+
id: '394051',
1025+
},
1026+
to: {
1027+
type: 'user',
1028+
id: '536e564f316c83104c000020',
1029+
},
1030+
createConversationWithoutContactReply: true,
1031+
});
1032+
```
1033+
10161034
### Notes
10171035

10181036
#### [Create a note](https://developers.intercom.com/intercom-api-reference/reference/create-note-for-contact)

lib/message.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default class Message {
1515
template,
1616
from,
1717
to,
18+
createConversationWithoutContactReply:
19+
create_conversation_without_contact_reply,
1820
}: CreateMessageBody) {
1921
const data: CreateMessageRequest = {
2022
message_type,
@@ -23,6 +25,7 @@ export default class Message {
2325
template,
2426
from,
2527
to,
28+
create_conversation_without_contact_reply,
2629
};
2730

2831
return this.client.post<MessageObject>({
@@ -39,10 +42,16 @@ interface CreateMessageRequest {
3942
to: Recepient;
4043
subject?: string;
4144
template?: string;
45+
create_conversation_without_contact_reply?: boolean;
4246
}
4347

44-
interface CreateMessageBody extends Omit<CreateMessageRequest, 'message_type'> {
48+
interface CreateMessageBody
49+
extends Omit<
50+
CreateMessageRequest,
51+
'message_type' | 'create_conversation_without_contact_reply'
52+
> {
4553
messageType: MessageType;
54+
createConversationWithoutContactReply?: boolean;
4655
}
4756

4857
type Recepient = {

test/integration/messages.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { token } from './utils/config';
2+
import { Client, Operators } from '../../dist';
3+
import { randomString } from './utils/random';
4+
import { MessageType } from '../../lib/message/message.types';
5+
import { RecepientType } from '../../lib/message';
6+
import assert from 'assert';
7+
8+
describe('Messages', () => {
9+
let adminId: string;
10+
let userIntercomId: string;
11+
const client = new Client({
12+
tokenAuth: { token },
13+
});
14+
15+
before(async () => {
16+
const adminList = await client.admins.list();
17+
adminId = adminList.admins[0].id;
18+
19+
const createdUser = await client.contacts.createUser({
20+
externalId: randomString(),
21+
name: 'Message Test User',
22+
});
23+
userIntercomId = createdUser.id;
24+
});
25+
26+
it('Message that creates a converation', async () => {
27+
const requestBody = {
28+
message_type: MessageType.INAPP,
29+
body: 'Hey, look at me! I am the conversations creator now!',
30+
from: {
31+
type: RecepientType.ADMIN,
32+
id: adminId,
33+
},
34+
to: {
35+
type: RecepientType.USER,
36+
id: userIntercomId,
37+
},
38+
};
39+
const response = await client.messages.create({
40+
messageType: requestBody.message_type,
41+
body: requestBody.body,
42+
from: requestBody.from,
43+
to: requestBody.to,
44+
createConversationWithoutContactReply: true,
45+
});
46+
47+
const messageId = response.id;
48+
49+
// Give Intercom a few seconds to index conversation
50+
await new Promise((resolve) => setTimeout(resolve, 5000));
51+
52+
const searchResults = await client.conversations.search({
53+
data: {
54+
query: {
55+
field: 'source.id',
56+
operator: Operators.EQUALS,
57+
value: messageId,
58+
},
59+
},
60+
});
61+
assert.equal(searchResults.total_count > 0, true);
62+
});
63+
64+
it('Create message, no conversation', async () => {
65+
const requestBody = {
66+
message_type: MessageType.INAPP,
67+
body: 'Message without creating conversation',
68+
from: {
69+
type: RecepientType.ADMIN,
70+
id: adminId,
71+
},
72+
to: {
73+
type: RecepientType.USER,
74+
id: userIntercomId,
75+
},
76+
};
77+
const response = await client.messages.create({
78+
messageType: requestBody.message_type,
79+
body: requestBody.body,
80+
from: requestBody.from,
81+
to: requestBody.to,
82+
});
83+
assert.notEqual(response, undefined);
84+
});
85+
});

0 commit comments

Comments
 (0)