|
| 1 | +import gorgiasOauth from "../../gorgias_oauth.app.mjs"; |
| 2 | +import constants from "../../common/constants.mjs"; |
| 3 | +import { |
| 4 | + axios, ConfigurationError, |
| 5 | +} from "@pipedream/platform"; |
| 6 | + |
| 7 | +export default { |
| 8 | + key: "gorgias_oauth-create-ticket-message", |
| 9 | + name: "Create Ticket Message", |
| 10 | + description: "Create a message for a ticket in the Gorgias system. [See the documentation](https://developers.gorgias.com/reference/create-ticket-message)", |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + gorgiasOauth, |
| 15 | + ticketId: { |
| 16 | + propDefinition: [ |
| 17 | + gorgiasOauth, |
| 18 | + "ticketId", |
| 19 | + ], |
| 20 | + description: "The ID of the ticket to add a message to", |
| 21 | + }, |
| 22 | + channel: { |
| 23 | + propDefinition: [ |
| 24 | + gorgiasOauth, |
| 25 | + "channel", |
| 26 | + ], |
| 27 | + }, |
| 28 | + fromAddress: { |
| 29 | + propDefinition: [ |
| 30 | + gorgiasOauth, |
| 31 | + "address", |
| 32 | + ], |
| 33 | + label: "From Address", |
| 34 | + }, |
| 35 | + toAddress: { |
| 36 | + propDefinition: [ |
| 37 | + gorgiasOauth, |
| 38 | + "address", |
| 39 | + ], |
| 40 | + label: "To Address", |
| 41 | + }, |
| 42 | + message: { |
| 43 | + type: "string", |
| 44 | + label: "Message", |
| 45 | + description: "Message of the ticket. Accepts HTML", |
| 46 | + }, |
| 47 | + via: { |
| 48 | + propDefinition: [ |
| 49 | + gorgiasOauth, |
| 50 | + "via", |
| 51 | + ], |
| 52 | + optional: false, |
| 53 | + }, |
| 54 | + subject: { |
| 55 | + propDefinition: [ |
| 56 | + gorgiasOauth, |
| 57 | + "subject", |
| 58 | + ], |
| 59 | + optional: true, |
| 60 | + }, |
| 61 | + attachmentUrl: { |
| 62 | + type: "string", |
| 63 | + label: "Attachment URL", |
| 64 | + description: "The URL to access to the attached file", |
| 65 | + optional: true, |
| 66 | + }, |
| 67 | + attachmentName: { |
| 68 | + type: "string", |
| 69 | + label: "Attachment File Name", |
| 70 | + description: "The name of the file to attach", |
| 71 | + optional: true, |
| 72 | + }, |
| 73 | + fromAgent: { |
| 74 | + type: "boolean", |
| 75 | + label: "From Agent", |
| 76 | + description: "Whether the message was sent by your company to a customer, or the opposite", |
| 77 | + default: false, |
| 78 | + optional: true, |
| 79 | + }, |
| 80 | + sentDatetime: { |
| 81 | + type: "string", |
| 82 | + label: "Sent Datetime", |
| 83 | + description: "When the message was sent. If ommited, the message will be sent by Gorgias. E.g. `2025-01-27T19:38:52.028Z`", |
| 84 | + optional: true, |
| 85 | + }, |
| 86 | + sourceType: { |
| 87 | + type: "string", |
| 88 | + label: "Source Type", |
| 89 | + description: "Describes more detailed channel information of how the message is sent/received", |
| 90 | + options: constants.sourceTypes, |
| 91 | + optional: true, |
| 92 | + }, |
| 93 | + }, |
| 94 | + methods: { |
| 95 | + createMessage({ |
| 96 | + ticketId, ...opts |
| 97 | + }) { |
| 98 | + return this.gorgiasOauth._makeRequest({ |
| 99 | + method: "POST", |
| 100 | + path: `/tickets/${ticketId}/messages`, |
| 101 | + ...opts, |
| 102 | + }); |
| 103 | + }, |
| 104 | + async getAttachmentInfo($, url) { |
| 105 | + const { headers } = await axios($, { |
| 106 | + method: "HEAD", |
| 107 | + url, |
| 108 | + returnFullResponse: true, |
| 109 | + }); |
| 110 | + return { |
| 111 | + contentType: headers["content-type"], |
| 112 | + size: headers["content-length"], |
| 113 | + }; |
| 114 | + }, |
| 115 | + }, |
| 116 | + async run({ $ }) { |
| 117 | + if ((this.attachmentUrl && !this.attachmentName) |
| 118 | + || (!this.attachmentUrl && this.attachmentName) |
| 119 | + ) { |
| 120 | + throw new ConfigurationError("Must enter both Attachment URL and Attachment File Name"); |
| 121 | + } |
| 122 | + |
| 123 | + let contentType, size; |
| 124 | + if (this.attachmentUrl) { |
| 125 | + ({ |
| 126 | + contentType, size, |
| 127 | + } = await this.getAttachmentInfo($, this.attachmentUrl)); |
| 128 | + } |
| 129 | + |
| 130 | + const response = await this.createMessage({ |
| 131 | + $, |
| 132 | + ticketId: this.ticketId, |
| 133 | + data: { |
| 134 | + channel: this.channel, |
| 135 | + source: { |
| 136 | + type: this.sourceType, |
| 137 | + from: { |
| 138 | + address: this.fromAddress, |
| 139 | + }, |
| 140 | + to: [ |
| 141 | + { |
| 142 | + address: this.toAddress, |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + body_html: this.message, |
| 147 | + via: this.via, |
| 148 | + subject: this.subject, |
| 149 | + from_agent: this.fromAgent, |
| 150 | + sent_datetime: this.sentDatetime, |
| 151 | + attachments: this.attachmentUrl && [ |
| 152 | + { |
| 153 | + url: this.attachmentUrl, |
| 154 | + name: this.attachmentName, |
| 155 | + content_type: contentType, |
| 156 | + size, |
| 157 | + }, |
| 158 | + ], |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + $.export("$summary", `Succesfully created ticket message with ID: ${response.id}`); |
| 163 | + |
| 164 | + return response; |
| 165 | + }, |
| 166 | +}; |
0 commit comments