Skip to content

Commit

Permalink
[ACTION] Intercom Upsert Contact #14268 (#14479)
Browse files Browse the repository at this point in the history
* [ACTION] Intercom Upsert Contact #14268
Actions
 - Upsert Contact

* Update components/intercom/actions/upsert-contact/upsert-contact.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update components/intercom/actions/upsert-contact/upsert-contact.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update components/intercom/actions/upsert-contact/upsert-contact.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
luancazarine and coderabbitai[bot] authored Nov 1, 2024
1 parent 8fd30ef commit 02197b3
Show file tree
Hide file tree
Showing 21 changed files with 192 additions and 29 deletions.
2 changes: 1 addition & 1 deletion components/intercom/actions/create-note/create-note.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "intercom-create-note",
name: "Create Note",
description: "Creates a note for a specific user. [See the docs here](https://developers.intercom.com/intercom-api-reference/reference/create-note-for-contact)",
version: "0.0.3",
version: "0.0.4",
type: "action",
props: {
intercom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "intercom-send-incoming-message",
name: "Send Incoming Message",
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)",
version: "0.0.3",
version: "0.0.4",
type: "action",
props: {
intercom,
Expand Down
131 changes: 131 additions & 0 deletions components/intercom/actions/upsert-contact/upsert-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { ROLE_OPTIONS } from "../../common/constants.mjs";
import intercom from "../../intercom.app.mjs";

export default {
key: "intercom-upsert-contact",
name: "Upsert Contact",
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)",
version: "0.0.1",
type: "action",
props: {
intercom,
email: {
type: "string",
label: "Email",
description: "The contact's email.",
},
role: {
type: "string",
label: "Role",
description: "The role of the contact.",
options: ROLE_OPTIONS,
optional: true,
},
externalId: {
type: "string",
label: "External Id",
description: "A unique identifier for the contact which is given to Intercom.",
optional: true,
},
phone: {
type: "string",
label: "Phone",
description: "The contact's phone number.",
optional: true,
},
name: {
type: "string",
label: "Name",
description: "The contact's name.",
optional: true,
},
avatar: {
type: "string",
label: "Avatar",
description: "An image URL containing the avatar of a contact.",
optional: true,
},
unsubscribedFromEmails: {
type: "boolean",
label: "Unsubscribed From Emails",
description: "Whether the contact is unsubscribed from emails.",
optional: true,
},
customAttributes: {
type: "object",
label: "Custom Attributes",
description: "The custom attributes which are set for the contact.",
optional: true,
},
},
async run({ $ }) {
let response = {};
let requestType = "created";
let data = {
email: this.email,
role: this.role,
externalId: this.externalId,
phone: this.phone,
name: this.name,
avatar: this.avatar,
unsubscribedFromEmails: this.unsubscribedFromEmails,
customAttributes: this.customAttributes,
};

data = Object.entries(data).filter(([
,
value,
]) => (value != "" && value != undefined))
.reduce((obj, [
key,
value,
]) => Object.assign(obj, {
[key]: value,
}), {});

const {
data: contact, total_count: total,
} = await this.intercom.searchContact({
data: {
query: {
operator: "AND",
value: [
{
field: "email",
operator: "=",
value: this.email,
},
],
},
pagination: {
per_page: 1,
},
},
});

if (total) {
const {
id: contactId,
// eslint-disable-next-line no-unused-vars
owner_id,
...contactInfos
} = contact[0];
response = await this.intercom.updateContact({
$,
contactId,
data: {
...contactInfos,
...data,
},
});
requestType = "updated";
} else {
response = await this.intercom.createContact({
$,
data,
});
}
$.export("$summary", `Successfully ${requestType} contact with ID ${response.id}`);
return response;
},
};
10 changes: 10 additions & 0 deletions components/intercom/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const ROLE_OPTIONS = [
{
label: "User",
value: "user",
},
{
label: "Lead",
value: "lead",
},
];
40 changes: 31 additions & 9 deletions components/intercom/intercom.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@ export default {
* @params {Object} [opts.data] - The request body
* @returns {*} The response may vary depending on the specific API request.
*/
async makeRequest(opts) {
const {
method,
url,
endpoint,
data,
$,
} = opts;
async makeRequest({
method,
url,
endpoint,
$,
...opts
}) {
const config = {
method,
url: url ?? `https://api.intercom.io/${endpoint}`,
headers: {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
Accept: "application/json",
},
data,
...opts,
};
return axios($ || this, config);
},
Expand Down Expand Up @@ -210,6 +209,29 @@ export default {
$,
});
},
searchContact(opts = {}) {
return this.makeRequest({
method: "POST",
endpoint: "contacts/search",
...opts,
});
},
createContact(opts = {}) {
return this.makeRequest({
method: "POST",
endpoint: "contacts",
...opts,
});
},
updateContact({
contactId, ...opts
}) {
return this.makeRequest({
method: "PUT",
endpoint: `contacts/${contactId}`,
...opts,
});
},
/**
* Create an incoming message from a user
* @params {Object} data - The request body parameters including a `from` object and
Expand Down
2 changes: 1 addition & 1 deletion components/intercom/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/intercom",
"version": "0.4.0",
"version": "0.5.0",
"description": "Pipedream Intercom Components",
"main": "intercom.app.mjs",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-conversation-closed",
name: "New Closed Conversation",
description: "Emit new event each time a conversation is closed.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-lead-added-email",
name: "Lead Added Email",
description: "Emit new event each time a lead adds their email address.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-admin-reply",
name: "New Reply From Admin",
description: "Emit new event each time an admin replies to a conversation.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
2 changes: 1 addition & 1 deletion components/intercom/sources/new-company/new-company.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-company",
name: "New Companies",
description: "Emit new event each time a new company is added.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-conversation-rating-added",
name: "New Conversation Rating Added",
description: "Emit new event each time a new rating is added to a conversation.",
version: "0.0.1",
version: "0.0.2",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-conversation",
name: "New Conversations",
description: "Emit new event each time a new conversation is added.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
2 changes: 1 addition & 1 deletion components/intercom/sources/new-event/new-event.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-event",
name: "New Event",
description: "Emit new event for each new Intercom event for a user.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
props: {
Expand Down
2 changes: 1 addition & 1 deletion components/intercom/sources/new-lead/new-lead.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-lead",
name: "New Leads",
description: "Emit new event each time a new lead is added.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
6 changes: 3 additions & 3 deletions components/intercom/sources/new-topic/new-topic.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import app from "../../intercom.app.mjs";
import { v4 as uuid } from "uuid";
import crypto from "crypto";
import { v4 as uuid } from "uuid";
import app from "../../intercom.app.mjs";

export default {
key: "intercom-new-topic",
name: "New Topic (Instant)",
description: "Emit new event for each new topic that you subscribed in your webhook. [See more here](https://developers.intercom.com/building-apps/docs/setting-up-webhooks).",
version: "0.0.2",
version: "0.0.3",
type: "source",
dedupe: "unique",
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-unsubscription",
name: "New Unsubscriptions",
description: "Emit new event each time a user unsubscribes from receiving emails.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-user-reply",
name: "New Reply From User",
description: "Emit new event each time a user replies to a conversation.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
2 changes: 1 addition & 1 deletion components/intercom/sources/new-user/new-user.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-new-user",
name: "New Users",
description: "Emit new event each time a new user is added.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-tag-added-to-conversation",
name: "Tag Added To Conversation",
description: "Emit new event each time a new tag is added to a conversation.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-tag-added-to-lead",
name: "Tag Added To Lead",
description: "Emit new event each time a new tag is added to a lead.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "intercom-tag-added-to-user",
name: "Tag Added To User",
description: "Emit new event each time a new tag is added to a user.",
version: "0.0.5",
version: "0.0.6",
type: "source",
dedupe: "unique",
methods: {
Expand Down

0 comments on commit 02197b3

Please sign in to comment.