diff --git a/components/peach/actions/create-contact/create-contact.mjs b/components/peach/actions/create-contact/create-contact.mjs new file mode 100644 index 0000000000000..eee0da3abb1aa --- /dev/null +++ b/components/peach/actions/create-contact/create-contact.mjs @@ -0,0 +1,46 @@ +import { clearObj } from "../../common/utils.mjs"; +import peach from "../../peach.app.mjs"; + +export default { + key: "peach-create-contact", + name: "Create Contact", + description: "Creates a contact in your account. [See the documentation](https://peach.apidocumentation.com/reference#tag/contacts/post/subscribers)", + version: "0.0.1", + type: "action", + props: { + peach, + phoneNumber: { + propDefinition: [ + peach, + "phoneNumber", + ], + }, + contactName: { + propDefinition: [ + peach, + "contactName", + ], + optional: true, + }, + contactEmail: { + propDefinition: [ + peach, + "contactEmail", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.peach.createContact({ + $, + data: clearObj({ + name: this.contactName, + email: this.contactEmail, + phone_number: this.phoneNumber, + }), + }); + + $.export("$summary", `Created the contact successfully with waId: ${this.phoneNumber}`); + return response; + }, +}; diff --git a/components/peach/actions/send-template-message/send-template-message.mjs b/components/peach/actions/send-template-message/send-template-message.mjs new file mode 100644 index 0000000000000..b7f4ec0dbd664 --- /dev/null +++ b/components/peach/actions/send-template-message/send-template-message.mjs @@ -0,0 +1,66 @@ +import { clearObj } from "../../common/utils.mjs"; +import peach from "../../peach.app.mjs"; + +export default { + key: "peach-send-template-message", + name: "Send Template Message", + description: "Send a predefined message to a contact within the Peach app. [See the documentation](https://peach.apidocumentation.com/reference#tag/messaging/post/transactional_messages)", + version: "0.0.1", + type: "action", + props: { + peach, + phoneNumber: { + propDefinition: [ + peach, + "phoneNumber", + ], + }, + templateName: { + type: "string", + label: "Template Name", + description: "WhatsApp approved utility template name", + }, + contactName: { + propDefinition: [ + peach, + "contactName", + ], + optional: true, + }, + contactEmail: { + propDefinition: [ + peach, + "contactEmail", + ], + optional: true, + }, + arguments: { + type: "object", + label: "Arguments", + description: "Arguments for the template", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.peach.sendTransactionalMessage({ + $, + data: clearObj({ + template_name: this.templateName, + to: { + name: this.contactName, + email: this.contactEmail, + phone_number: this.phoneNumber, + }, + arguments: this.arguments + ? Object.keys(this.arguments).map((key) => ({ + key, + value: this.arguments[key], + })) + : {}, + }), + }); + + $.export("$summary", `Message sent successfully to ${this.phoneNumber}`); + return response; + }, +}; diff --git a/components/peach/common/utils.mjs b/components/peach/common/utils.mjs new file mode 100644 index 0000000000000..816afd11fddb2 --- /dev/null +++ b/components/peach/common/utils.mjs @@ -0,0 +1,16 @@ +export const clearObj = (obj) => { + return Object.entries(obj) + .filter(([ + , + v, + ]) => (v != null && v != "" && JSON.stringify(v) != "{}")) + .reduce((acc, [ + k, + v, + ]) => ({ + ...acc, + [k]: (!Array.isArray(v) && v === Object(v)) + ? clearObj(v) + : v, + }), {}); +}; diff --git a/components/peach/package.json b/components/peach/package.json index ed38312b58984..b44a64755861e 100644 --- a/components/peach/package.json +++ b/components/peach/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/peach", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Peach Components", "main": "peach.app.mjs", "keywords": [ @@ -11,5 +11,9 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.0" } -} \ No newline at end of file +} + diff --git a/components/peach/peach.app.mjs b/components/peach/peach.app.mjs index 4c3eca5e6f6ef..df54b05c31088 100644 --- a/components/peach/peach.app.mjs +++ b/components/peach/peach.app.mjs @@ -1,11 +1,57 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "peach", - propDefinitions: {}, + propDefinitions: { + phoneNumber: { + type: "string", + label: "Phone Number", + description: "The phone number of the contact to send the message to", + }, + contactName: { + type: "string", + label: "Contact Name", + description: "The name of the contact", + }, + contactEmail: { + type: "string", + label: "Contact Email", + description: "The email of the contact", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://app.trypeach.io/api/v1"; + }, + _headers() { + return { + "Authorization": `${this.$auth.api_token}`, + "Content-Type": "application/json", + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: this._baseUrl() + path, + headers: this._headers(), + ...opts, + }); + }, + sendTransactionalMessage(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/transactional_messages", + ...opts, + }); + }, + createContact(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/contacts", + ...opts, + }); }, }, -}; \ No newline at end of file +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e63339c1d9a80..509b3aa852e5b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6548,7 +6548,10 @@ importers: specifiers: {} components/peach: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.0 + dependencies: + '@pipedream/platform': 3.0.0 components/peaka: specifiers: