From 6e6b00584de0c8b3ea2596b1e5937da17f2e53e8 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Mon, 27 Jan 2025 15:53:33 -0500 Subject: [PATCH] create-ticket-message --- .../create-ticket-message.mjs | 166 ++++++++++++++++++ components/gorgias_oauth/package.json | 2 +- 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 components/gorgias_oauth/actions/create-ticket-message/create-ticket-message.mjs diff --git a/components/gorgias_oauth/actions/create-ticket-message/create-ticket-message.mjs b/components/gorgias_oauth/actions/create-ticket-message/create-ticket-message.mjs new file mode 100644 index 0000000000000..1af414ab2056f --- /dev/null +++ b/components/gorgias_oauth/actions/create-ticket-message/create-ticket-message.mjs @@ -0,0 +1,166 @@ +import gorgiasOauth from "../../gorgias_oauth.app.mjs"; +import constants from "../../common/constants.mjs"; +import { + axios, ConfigurationError, +} from "@pipedream/platform"; + +export default { + key: "gorgias_oauth-create-ticket-message", + name: "Create Ticket Message", + description: "Create a message for a ticket in the Gorgias system. [See the documentation](https://developers.gorgias.com/reference/create-ticket-message)", + version: "0.0.1", + type: "action", + props: { + gorgiasOauth, + ticketId: { + propDefinition: [ + gorgiasOauth, + "ticketId", + ], + description: "The ID of the ticket to add a message to", + }, + channel: { + propDefinition: [ + gorgiasOauth, + "channel", + ], + }, + fromAddress: { + propDefinition: [ + gorgiasOauth, + "address", + ], + label: "From Address", + }, + toAddress: { + propDefinition: [ + gorgiasOauth, + "address", + ], + label: "To Address", + }, + message: { + type: "string", + label: "Message", + description: "Message of the ticket. Accepts HTML", + }, + via: { + propDefinition: [ + gorgiasOauth, + "via", + ], + optional: false, + }, + subject: { + propDefinition: [ + gorgiasOauth, + "subject", + ], + optional: true, + }, + attachmentUrl: { + type: "string", + label: "Attachment URL", + description: "The URL to access to the attached file", + optional: true, + }, + attachmentName: { + type: "string", + label: "Attachment File Name", + description: "The name of the file to attach", + optional: true, + }, + fromAgent: { + type: "boolean", + label: "From Agent", + description: "Whether the message was sent by your company to a customer, or the opposite", + default: false, + optional: true, + }, + sentDatetime: { + type: "string", + label: "Sent Datetime", + description: "When the message was sent. If ommited, the message will be sent by Gorgias. E.g. `2025-01-27T19:38:52.028Z`", + optional: true, + }, + sourceType: { + type: "string", + label: "Source Type", + description: "Describes more detailed channel information of how the message is sent/received", + options: constants.sourceTypes, + optional: true, + }, + }, + methods: { + createMessage({ + ticketId, ...opts + }) { + return this.gorgiasOauth._makeRequest({ + method: "POST", + path: `/tickets/${ticketId}/messages`, + ...opts, + }); + }, + async getAttachmentInfo($, url) { + const { headers } = await axios($, { + method: "HEAD", + url, + returnFullResponse: true, + }); + return { + contentType: headers["content-type"], + size: headers["content-length"], + }; + }, + }, + async run({ $ }) { + if ((this.attachmentUrl && !this.attachmentName) + || (!this.attachmentUrl && this.attachmentName) + ) { + throw new ConfigurationError("Must enter both Attachment URL and Attachment File Name"); + } + + let contentType, size; + if (this.attachmentUrl) { + ({ + contentType, size, + } = await this.getAttachmentInfo($, this.attachmentUrl)); + } + + const response = await this.createMessage({ + $, + ticketId: this.ticketId, + data: { + channel: this.channel, + source: { + type: this.sourceType, + from: { + address: this.fromAddress, + }, + to: [ + { + address: this.toAddress, + }, + ], + }, + body_html: this.message, + via: this.via, + subject: this.subject, + from_agent: this.fromAgent, + sent_datetime: this.sentDatetime, + attachments: this.attachmentUrl && [ + { + url: this.attachmentUrl, + name: this.attachmentName, + content_type: contentType, + size, + }, + ], + }, + }); + + $.export("$summary", `Succesfully created ticket message with ID: ${response.id}`); + + return response; + }, +}; diff --git a/components/gorgias_oauth/package.json b/components/gorgias_oauth/package.json index 1c19492bb6d2e..35c8e20a4fd2d 100644 --- a/components/gorgias_oauth/package.json +++ b/components/gorgias_oauth/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/gorgias_oauth", - "version": "0.4.1", + "version": "0.5.0", "description": "Pipedream Gorgias OAuth Components", "main": "gorgias_oauth.app.mjs", "keywords": [