Skip to content

Commit 2b6e0d2

Browse files
committed
[ACTION] Intercom Upsert Contact #14268
Actions - Upsert Contact
1 parent 203cf85 commit 2b6e0d2

File tree

21 files changed

+192
-29
lines changed

21 files changed

+192
-29
lines changed

components/intercom/actions/create-note/create-note.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "intercom-create-note",
55
name: "Create Note",
66
description: "Creates a note for a specific user. [See the docs here](https://developers.intercom.com/intercom-api-reference/reference/create-note-for-contact)",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
type: "action",
99
props: {
1010
intercom,

components/intercom/actions/send-incoming-message/send-incoming-message.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "intercom-send-incoming-message",
55
name: "Send Incoming Message",
66
description: "Send a message from a user into your Intercom app. [See the docs here](https://developers.intercom.com/intercom-api-reference/reference/create-a-conversation)",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
type: "action",
99
props: {
1010
intercom,
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { ROLE_OPTIONS } from "../../common/constants.mjs";
2+
import intercom from "../../intercom.app.mjs";
3+
4+
export default {
5+
key: "intercom-upsert-contact",
6+
name: "Upsert Contact",
7+
description: "Create a new contact. If there is already a contact with the email provided, the existing contact will be updated. [See the docs here](https://developers.intercom.com/docs/references/rest-api/api.intercom.io/contacts/createcontact)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
intercom,
12+
email: {
13+
type: "string",
14+
label: "Email",
15+
description: "The contact's email.",
16+
},
17+
role: {
18+
type: "string",
19+
label: "Role",
20+
description: "The role of the contact.",
21+
options: ROLE_OPTIONS,
22+
optional: true,
23+
},
24+
externalId: {
25+
type: "string",
26+
label: "External Id",
27+
description: "A unique identifier for the contact which is given to Intercom.",
28+
optional: true,
29+
},
30+
phone: {
31+
type: "string",
32+
label: "Phone",
33+
description: "The contact's email.",
34+
optional: true,
35+
},
36+
name: {
37+
type: "string",
38+
label: "Name",
39+
description: "The contact's name.",
40+
optional: true,
41+
},
42+
avatar: {
43+
type: "string",
44+
label: "Avatar",
45+
description: "An image URL containing the avatar of a contact.",
46+
optional: true,
47+
},
48+
unsubscribedFromEmails: {
49+
type: "boolean",
50+
label: "Unsubscribed From Emails",
51+
description: "Whether the contact is unsubscribed from emails.",
52+
optional: true,
53+
},
54+
customAttributes: {
55+
type: "object",
56+
label: "Custom Attributes",
57+
description: "The custom attributes which are set for the contact.",
58+
optional: true,
59+
},
60+
},
61+
async run({ $ }) {
62+
let response = {};
63+
let requestType = "created";
64+
let data = {
65+
email: this.email,
66+
role: this.role,
67+
externalId: this.externalId,
68+
phone: this.phone,
69+
name: this.name,
70+
avatar: this.avatar,
71+
unsubscribedFromEmails: this.unsubscribedFromEmails,
72+
customAttributes: this.customAttributes,
73+
};
74+
75+
data = Object.entries(data).filter(([
76+
,
77+
value,
78+
]) => (value != "" && value != undefined))
79+
.reduce((obj, [
80+
key,
81+
value,
82+
]) => Object.assign(obj, {
83+
[key]: value,
84+
}), {});
85+
86+
const {
87+
data: contact, total_count: total,
88+
} = await this.intercom.searchContact({
89+
data: {
90+
query: {
91+
operator: "AND",
92+
value: [
93+
{
94+
field: "email",
95+
operator: "=",
96+
value: this.email,
97+
},
98+
],
99+
},
100+
pagination: {
101+
per_page: 5,
102+
},
103+
},
104+
});
105+
106+
if (total) {
107+
const {
108+
id: contactId,
109+
// eslint-disable-next-line no-unused-vars
110+
owner_id,
111+
...contactInfos
112+
} = contact[0];
113+
response = await this.intercom.updateContact({
114+
$,
115+
contactId,
116+
data: {
117+
...contactInfos,
118+
...data,
119+
},
120+
});
121+
requestType = "updated";
122+
} else {
123+
response = await this.intercom.createContact({
124+
$,
125+
data,
126+
});
127+
}
128+
$.export("$summary", `Successfully ${requestType} note with ID ${response.id}`);
129+
return response;
130+
},
131+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const ROLE_OPTIONS = [
2+
{
3+
label: "User",
4+
value: "user",
5+
},
6+
{
7+
label: "Lead",
8+
value: "lead",
9+
},
10+
];

components/intercom/intercom.app.mjs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,21 @@ export default {
4545
* @params {Object} [opts.data] - The request body
4646
* @returns {*} The response may vary depending on the specific API request.
4747
*/
48-
async makeRequest(opts) {
49-
const {
50-
method,
51-
url,
52-
endpoint,
53-
data,
54-
$,
55-
} = opts;
48+
async makeRequest({
49+
method,
50+
url,
51+
endpoint,
52+
$,
53+
...opts
54+
}) {
5655
const config = {
5756
method,
5857
url: url ?? `https://api.intercom.io/${endpoint}`,
5958
headers: {
6059
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
6160
Accept: "application/json",
6261
},
63-
data,
62+
...opts,
6463
};
6564
return axios($ || this, config);
6665
},
@@ -210,6 +209,29 @@ export default {
210209
$,
211210
});
212211
},
212+
searchContact(opts = {}) {
213+
return this.makeRequest({
214+
method: "POST",
215+
endpoint: "contacts/search",
216+
...opts,
217+
});
218+
},
219+
createContact(opts = {}) {
220+
return this.makeRequest({
221+
method: "POST",
222+
endpoint: "contacts",
223+
...opts,
224+
});
225+
},
226+
updateContact({
227+
contactId, ...opts
228+
}) {
229+
return this.makeRequest({
230+
method: "PUT",
231+
endpoint: `contacts/${contactId}`,
232+
...opts,
233+
});
234+
},
213235
/**
214236
* Create an incoming message from a user
215237
* @params {Object} data - The request body parameters including a `from` object and

components/intercom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/intercom",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Pipedream Intercom Components",
55
"main": "intercom.app.mjs",
66
"keywords": [

components/intercom/sources/conversation-closed/conversation-closed.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "intercom-conversation-closed",
66
name: "New Closed Conversation",
77
description: "Emit new event each time a conversation is closed.",
8-
version: "0.0.5",
8+
version: "0.0.6",
99
type: "source",
1010
dedupe: "unique",
1111
methods: {

components/intercom/sources/lead-added-email/lead-added-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "intercom-lead-added-email",
66
name: "Lead Added Email",
77
description: "Emit new event each time a lead adds their email address.",
8-
version: "0.0.5",
8+
version: "0.0.6",
99
type: "source",
1010
dedupe: "unique",
1111
methods: {

components/intercom/sources/new-admin-reply/new-admin-reply.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "intercom-new-admin-reply",
66
name: "New Reply From Admin",
77
description: "Emit new event each time an admin replies to a conversation.",
8-
version: "0.0.5",
8+
version: "0.0.6",
99
type: "source",
1010
dedupe: "unique",
1111
methods: {

components/intercom/sources/new-company/new-company.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "intercom-new-company",
66
name: "New Companies",
77
description: "Emit new event each time a new company is added.",
8-
version: "0.0.5",
8+
version: "0.0.6",
99
type: "source",
1010
dedupe: "unique",
1111
methods: {

0 commit comments

Comments
 (0)