From 2b6e0d2a9e0910884e2455ea3d6e73549bed2b41 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Thu, 31 Oct 2024 15:16:34 -0300 Subject: [PATCH 1/4] [ACTION] Intercom Upsert Contact #14268 Actions - Upsert Contact --- .../actions/create-note/create-note.mjs | 2 +- .../send-incoming-message.mjs | 2 +- .../actions/upsert-contact/upsert-contact.mjs | 131 ++++++++++++++++++ components/intercom/common/constants.mjs | 10 ++ components/intercom/intercom.app.mjs | 40 ++++-- components/intercom/package.json | 2 +- .../conversation-closed.mjs | 2 +- .../lead-added-email/lead-added-email.mjs | 2 +- .../new-admin-reply/new-admin-reply.mjs | 2 +- .../sources/new-company/new-company.mjs | 2 +- .../new-conversation-rating-added.mjs | 2 +- .../new-conversation/new-conversation.mjs | 2 +- .../intercom/sources/new-event/new-event.mjs | 2 +- .../intercom/sources/new-lead/new-lead.mjs | 2 +- .../intercom/sources/new-topic/new-topic.mjs | 6 +- .../new-unsubscription/new-unsubscription.mjs | 2 +- .../sources/new-user-reply/new-user-reply.mjs | 2 +- .../intercom/sources/new-user/new-user.mjs | 2 +- .../tag-added-to-conversation.mjs | 2 +- .../tag-added-to-lead/tag-added-to-lead.mjs | 2 +- .../tag-added-to-user/tag-added-to-user.mjs | 2 +- 21 files changed, 192 insertions(+), 29 deletions(-) create mode 100644 components/intercom/actions/upsert-contact/upsert-contact.mjs create mode 100644 components/intercom/common/constants.mjs diff --git a/components/intercom/actions/create-note/create-note.mjs b/components/intercom/actions/create-note/create-note.mjs index ba5e85e80a117..978dad18d5a79 100644 --- a/components/intercom/actions/create-note/create-note.mjs +++ b/components/intercom/actions/create-note/create-note.mjs @@ -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, diff --git a/components/intercom/actions/send-incoming-message/send-incoming-message.mjs b/components/intercom/actions/send-incoming-message/send-incoming-message.mjs index 75178c8f21849..516f3563c1e06 100644 --- a/components/intercom/actions/send-incoming-message/send-incoming-message.mjs +++ b/components/intercom/actions/send-incoming-message/send-incoming-message.mjs @@ -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, diff --git a/components/intercom/actions/upsert-contact/upsert-contact.mjs b/components/intercom/actions/upsert-contact/upsert-contact.mjs new file mode 100644 index 0000000000000..7bf915a00170e --- /dev/null +++ b/components/intercom/actions/upsert-contact/upsert-contact.mjs @@ -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 email.", + 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: 5, + }, + }, + }); + + 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} note with ID ${response.id}`); + return response; + }, +}; diff --git a/components/intercom/common/constants.mjs b/components/intercom/common/constants.mjs new file mode 100644 index 0000000000000..cfa8b3e7d544e --- /dev/null +++ b/components/intercom/common/constants.mjs @@ -0,0 +1,10 @@ +export const ROLE_OPTIONS = [ + { + label: "User", + value: "user", + }, + { + label: "Lead", + value: "lead", + }, +]; diff --git a/components/intercom/intercom.app.mjs b/components/intercom/intercom.app.mjs index d0749fa1a6a7d..b432e2e3e4f33 100644 --- a/components/intercom/intercom.app.mjs +++ b/components/intercom/intercom.app.mjs @@ -45,14 +45,13 @@ 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}`, @@ -60,7 +59,7 @@ export default { Authorization: `Bearer ${this.$auth.oauth_access_token}`, Accept: "application/json", }, - data, + ...opts, }; return axios($ || this, config); }, @@ -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 diff --git a/components/intercom/package.json b/components/intercom/package.json index 0cd9b5d28f76e..7f4c0cd330f9d 100644 --- a/components/intercom/package.json +++ b/components/intercom/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/intercom", - "version": "0.4.0", + "version": "0.5.0", "description": "Pipedream Intercom Components", "main": "intercom.app.mjs", "keywords": [ diff --git a/components/intercom/sources/conversation-closed/conversation-closed.mjs b/components/intercom/sources/conversation-closed/conversation-closed.mjs index 6215d45ab3c46..e5f7fcca44126 100644 --- a/components/intercom/sources/conversation-closed/conversation-closed.mjs +++ b/components/intercom/sources/conversation-closed/conversation-closed.mjs @@ -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: { diff --git a/components/intercom/sources/lead-added-email/lead-added-email.mjs b/components/intercom/sources/lead-added-email/lead-added-email.mjs index 2e7137ead1547..50d4d45f846eb 100644 --- a/components/intercom/sources/lead-added-email/lead-added-email.mjs +++ b/components/intercom/sources/lead-added-email/lead-added-email.mjs @@ -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: { diff --git a/components/intercom/sources/new-admin-reply/new-admin-reply.mjs b/components/intercom/sources/new-admin-reply/new-admin-reply.mjs index 907a1aaada9ac..3eef629455950 100644 --- a/components/intercom/sources/new-admin-reply/new-admin-reply.mjs +++ b/components/intercom/sources/new-admin-reply/new-admin-reply.mjs @@ -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: { diff --git a/components/intercom/sources/new-company/new-company.mjs b/components/intercom/sources/new-company/new-company.mjs index fe46f463b84c1..b6de1a661f35c 100644 --- a/components/intercom/sources/new-company/new-company.mjs +++ b/components/intercom/sources/new-company/new-company.mjs @@ -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: { diff --git a/components/intercom/sources/new-conversation-rating-added/new-conversation-rating-added.mjs b/components/intercom/sources/new-conversation-rating-added/new-conversation-rating-added.mjs index c6c652cc6acf4..d4de8c5b9e826 100644 --- a/components/intercom/sources/new-conversation-rating-added/new-conversation-rating-added.mjs +++ b/components/intercom/sources/new-conversation-rating-added/new-conversation-rating-added.mjs @@ -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: { diff --git a/components/intercom/sources/new-conversation/new-conversation.mjs b/components/intercom/sources/new-conversation/new-conversation.mjs index 547c6f3a0879d..6a1c0315b020c 100644 --- a/components/intercom/sources/new-conversation/new-conversation.mjs +++ b/components/intercom/sources/new-conversation/new-conversation.mjs @@ -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: { diff --git a/components/intercom/sources/new-event/new-event.mjs b/components/intercom/sources/new-event/new-event.mjs index c4ee3fa096183..b3f7ba6c7c4a0 100644 --- a/components/intercom/sources/new-event/new-event.mjs +++ b/components/intercom/sources/new-event/new-event.mjs @@ -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: { diff --git a/components/intercom/sources/new-lead/new-lead.mjs b/components/intercom/sources/new-lead/new-lead.mjs index dae88ca980589..64a999cd8d86b 100644 --- a/components/intercom/sources/new-lead/new-lead.mjs +++ b/components/intercom/sources/new-lead/new-lead.mjs @@ -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: { diff --git a/components/intercom/sources/new-topic/new-topic.mjs b/components/intercom/sources/new-topic/new-topic.mjs index edd04e0c7407e..2c1a431e49b15 100644 --- a/components/intercom/sources/new-topic/new-topic.mjs +++ b/components/intercom/sources/new-topic/new-topic.mjs @@ -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: { diff --git a/components/intercom/sources/new-unsubscription/new-unsubscription.mjs b/components/intercom/sources/new-unsubscription/new-unsubscription.mjs index 53e222a11c801..be8e1eef2fd32 100644 --- a/components/intercom/sources/new-unsubscription/new-unsubscription.mjs +++ b/components/intercom/sources/new-unsubscription/new-unsubscription.mjs @@ -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: { diff --git a/components/intercom/sources/new-user-reply/new-user-reply.mjs b/components/intercom/sources/new-user-reply/new-user-reply.mjs index a02030a437f3a..44be581762364 100644 --- a/components/intercom/sources/new-user-reply/new-user-reply.mjs +++ b/components/intercom/sources/new-user-reply/new-user-reply.mjs @@ -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: { diff --git a/components/intercom/sources/new-user/new-user.mjs b/components/intercom/sources/new-user/new-user.mjs index 6f27c94dbda24..09fc3c0172a58 100644 --- a/components/intercom/sources/new-user/new-user.mjs +++ b/components/intercom/sources/new-user/new-user.mjs @@ -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: { diff --git a/components/intercom/sources/tag-added-to-conversation/tag-added-to-conversation.mjs b/components/intercom/sources/tag-added-to-conversation/tag-added-to-conversation.mjs index 95efa65929810..bede6b1036d92 100644 --- a/components/intercom/sources/tag-added-to-conversation/tag-added-to-conversation.mjs +++ b/components/intercom/sources/tag-added-to-conversation/tag-added-to-conversation.mjs @@ -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: { diff --git a/components/intercom/sources/tag-added-to-lead/tag-added-to-lead.mjs b/components/intercom/sources/tag-added-to-lead/tag-added-to-lead.mjs index 326b2c85f6504..31bf4134855ab 100644 --- a/components/intercom/sources/tag-added-to-lead/tag-added-to-lead.mjs +++ b/components/intercom/sources/tag-added-to-lead/tag-added-to-lead.mjs @@ -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: { diff --git a/components/intercom/sources/tag-added-to-user/tag-added-to-user.mjs b/components/intercom/sources/tag-added-to-user/tag-added-to-user.mjs index 2bb250640625a..648c2d1455870 100644 --- a/components/intercom/sources/tag-added-to-user/tag-added-to-user.mjs +++ b/components/intercom/sources/tag-added-to-user/tag-added-to-user.mjs @@ -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: { From 8667c81b63646622db95655acca679b0de0e8822 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Thu, 31 Oct 2024 15:45:10 -0300 Subject: [PATCH 2/4] Update components/intercom/actions/upsert-contact/upsert-contact.mjs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- components/intercom/actions/upsert-contact/upsert-contact.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/intercom/actions/upsert-contact/upsert-contact.mjs b/components/intercom/actions/upsert-contact/upsert-contact.mjs index 7bf915a00170e..a0d6ebc272327 100644 --- a/components/intercom/actions/upsert-contact/upsert-contact.mjs +++ b/components/intercom/actions/upsert-contact/upsert-contact.mjs @@ -30,7 +30,7 @@ export default { phone: { type: "string", label: "Phone", - description: "The contact's email.", + description: "The contact's phone number.", optional: true, }, name: { From 224b6216d99cc461f149c88e0b5bb626a092c4a7 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Thu, 31 Oct 2024 15:45:29 -0300 Subject: [PATCH 3/4] Update components/intercom/actions/upsert-contact/upsert-contact.mjs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- components/intercom/actions/upsert-contact/upsert-contact.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/intercom/actions/upsert-contact/upsert-contact.mjs b/components/intercom/actions/upsert-contact/upsert-contact.mjs index a0d6ebc272327..faf9453e2125d 100644 --- a/components/intercom/actions/upsert-contact/upsert-contact.mjs +++ b/components/intercom/actions/upsert-contact/upsert-contact.mjs @@ -98,7 +98,7 @@ export default { ], }, pagination: { - per_page: 5, + per_page: 1, }, }, }); From bff82ae7ecb84d76bf9d7b8eab4681a074b381b5 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Thu, 31 Oct 2024 15:45:42 -0300 Subject: [PATCH 4/4] Update components/intercom/actions/upsert-contact/upsert-contact.mjs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- components/intercom/actions/upsert-contact/upsert-contact.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/intercom/actions/upsert-contact/upsert-contact.mjs b/components/intercom/actions/upsert-contact/upsert-contact.mjs index faf9453e2125d..673dbf8a5a567 100644 --- a/components/intercom/actions/upsert-contact/upsert-contact.mjs +++ b/components/intercom/actions/upsert-contact/upsert-contact.mjs @@ -125,7 +125,7 @@ export default { data, }); } - $.export("$summary", `Successfully ${requestType} note with ID ${response.id}`); + $.export("$summary", `Successfully ${requestType} contact with ID ${response.id}`); return response; }, };