|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import { |
| 3 | + MESSAGE_FORMAT_OPTIONS, WRAP_TYPE_OPTIONS, |
| 4 | +} from "../../common/constants.mjs"; |
| 5 | +import { parseObject } from "../../common/utils.mjs"; |
1 | 6 | import selzy from "../../selzy.app.mjs";
|
2 |
| -import { axios } from "@pipedream/platform"; |
3 | 7 |
|
4 | 8 | export default {
|
5 | 9 | key: "selzy-create-email-message",
|
6 | 10 | name: "Create Email Message",
|
7 | 11 | description: "Adds a new email message. [See the documentation](https://selzy.com/en/support/category/api/messages/)",
|
8 |
| - version: "0.0.{{ts}}", |
| 12 | + version: "0.0.1", |
9 | 13 | type: "action",
|
10 | 14 | props: {
|
11 | 15 | selzy,
|
12 | 16 | senderName: {
|
13 |
| - propDefinition: [ |
14 |
| - selzy, |
15 |
| - "senderName", |
16 |
| - ], |
| 17 | + type: "string", |
| 18 | + label: "Sender's name", |
| 19 | + description: "It is a string that does not match the email address (the sender_email argument).", |
17 | 20 | },
|
18 | 21 | senderEmail: {
|
19 |
| - propDefinition: [ |
20 |
| - selzy, |
21 |
| - "senderEmail", |
22 |
| - ], |
| 22 | + type: "string", |
| 23 | + label: "Sender's email address", |
| 24 | + description: "This email must be checked (to do this, you need to manually create at least one email with this return address via the web interface, then click on the \"send the confirmation request\" link and follow the link from the email).", |
23 | 25 | },
|
24 | 26 | subject: {
|
| 27 | + type: "string", |
| 28 | + label: "Subject", |
| 29 | + description: "String with the letter subject. It may include [substitution fields](https://selzy.com/en/support/letter/other-functions/personalization-tags/). The parameter is optional if **Template Id** is indicated.", |
| 30 | + optional: true, |
| 31 | + }, |
| 32 | + body: { |
| 33 | + type: "string", |
| 34 | + label: "Body", |
| 35 | + description: "If you transfer the entire HTML text, test such letters additionally as headers outside the body may be modified. The parameter is optional if **Template Id** or **System Template Id** is indicated.", |
| 36 | + }, |
| 37 | + listId: { |
25 | 38 | propDefinition: [
|
26 | 39 | selzy,
|
27 |
| - "subject", |
| 40 | + "listId", |
28 | 41 | ],
|
29 | 42 | },
|
30 |
| - body: { |
| 43 | + textBody: { |
| 44 | + type: "string", |
| 45 | + label: "Text Body", |
| 46 | + description: "Text version of the template. It is absent by default. If you do not provide the text version along with the HTML version, you are recommended to set the **Generate Text** parameter to 1 for automatic generation of the text part of the letter.", |
| 47 | + optional: true, |
| 48 | + }, |
| 49 | + generateText: { |
| 50 | + type: "boolean", |
| 51 | + label: "Generate Text", |
| 52 | + description: "`True` means that the text part of the letter will be generated automatically based on the HTML part. If you do not provide the text version along with the HTML version, you are recommended to set the **Generate Text** parameter to `true` for automatic generation of the text part of the letter. If the text variant of the letter is provided using the **Text Body** parameter, the **Generate Text** parameter is ignored. Thus, if the **Generate Text** value has been set to `true`, the server's response will contain a warning.", |
| 53 | + optional: true, |
| 54 | + }, |
| 55 | + rawBody: { |
| 56 | + type: "string", |
| 57 | + label: "Raw Body", |
| 58 | + description: "It is intended to save the json structure of the block editor data structure (if the value is **Message Format** = block) The parameter obtains only the JSON structure, otherwise it will not be transferred.", |
| 59 | + optional: true, |
| 60 | + }, |
| 61 | + messageFormat: { |
| 62 | + type: "string", |
| 63 | + label: "Message Format", |
| 64 | + description: `It defines the manner of creating a letter. |
| 65 | + \n 1 - If you transfer the \`text\` value in this parameter and both the body and **Text Body** parameters are filled, the body parameter will be ignored, and the letter will be created from the data, transferred in the **Text Body** parameter. |
| 66 | + \n 2 - If you transfer the \`block\` value in this parameter but do not specify **Raw Body**, the letter will be saved as **Raw HTML**. |
| 67 | + \n 3 - If you transfer the \`block\` value in this parameter, the **body** and **Raw Body** parameters must be transferred so taht you can save the message in the block editor format.`, |
| 68 | + options: MESSAGE_FORMAT_OPTIONS, |
| 69 | + optional: true, |
| 70 | + }, |
| 71 | + lang: { |
| 72 | + type: "string", |
| 73 | + label: "Lang", |
| 74 | + description: `Two-letter language code for the string with the unsubscribe link that is added to each letter automatically. |
| 75 | + If it is not specified, the language code from the API URL is used. |
| 76 | + In addition to the string with the unsubscribe link, this language also affects the interface of the unsubscribe page. Languages en, it, ua and ru are fully supported, and in case of some other languages (da, de, es, fr, nl, pl, pt, tr), the string with a link will be translated, and the control interface will be in English.`, |
| 77 | + optional: true, |
| 78 | + }, |
| 79 | + templateId: { |
31 | 80 | propDefinition: [
|
32 | 81 | selzy,
|
33 |
| - "body", |
| 82 | + "templateId", |
34 | 83 | ],
|
| 84 | + optional: true, |
35 | 85 | },
|
36 |
| - listId: { |
| 86 | + systemTemplateId: { |
37 | 87 | propDefinition: [
|
38 | 88 | selzy,
|
39 |
| - "listId", |
| 89 | + "systemTemplateId", |
40 | 90 | ],
|
| 91 | + optional: true, |
41 | 92 | },
|
42 |
| - name: { |
| 93 | + wrapType: { |
43 | 94 | type: "string",
|
44 |
| - label: "Name", |
45 |
| - description: "The optional name for the email message", |
| 95 | + label: "Wrap Type", |
| 96 | + description: "Alignment of the message text on the specified side. If the argument is missing, the text will not be aligned.", |
| 97 | + options: WRAP_TYPE_OPTIONS, |
46 | 98 | optional: true,
|
47 | 99 | },
|
48 |
| - customFields: { |
49 |
| - type: "object", |
50 |
| - label: "Custom Fields", |
51 |
| - description: "Optional custom fields for the email message", |
| 100 | + categories: { |
| 101 | + type: "string[]", |
| 102 | + label: "Categories", |
| 103 | + description: "A list of letter categories.", |
52 | 104 | optional: true,
|
53 | 105 | },
|
54 | 106 | },
|
55 | 107 | async run({ $ }) {
|
56 |
| - const emailMessageData = { |
57 |
| - sender_name: this.senderName, |
58 |
| - sender_email: this.senderEmail, |
59 |
| - subject: this.subject, |
60 |
| - body: this.body, |
61 |
| - list_id: this.listId, |
62 |
| - }; |
63 |
| - |
64 |
| - if (this.name) { |
65 |
| - emailMessageData.name = this.name; |
| 108 | + if (this.templateId && this.systemTemplateId) { |
| 109 | + throw new ConfigurationError("You can only use one of the Template Id or System Template Id parameters."); |
66 | 110 | }
|
| 111 | + const response = await this.selzy.createEmailMessage({ |
| 112 | + $, |
| 113 | + params: { |
| 114 | + sender_name: this.senderName, |
| 115 | + sender_email: this.senderEmail, |
| 116 | + subject: this.subject, |
| 117 | + body: this.body, |
| 118 | + list_id: this.listId, |
67 | 119 |
|
68 |
| - if (this.customFields) { |
69 |
| - Object.assign(emailMessageData, this.customFields); |
70 |
| - } |
| 120 | + text_body: this.textBody, |
| 121 | + generate_text: +this.generateText, |
| 122 | + raw_body: this.rawBody, |
| 123 | + message_format: this.messageFormat, |
| 124 | + lang: this.lang, |
| 125 | + template_id: this.templateId, |
| 126 | + system_template_id: this.systemTemplateId, |
| 127 | + wrap_type: this.wrapType, |
| 128 | + categories: parseObject(this.categories)?.join(","), |
| 129 | + }, |
| 130 | + }); |
71 | 131 |
|
72 |
| - const response = await this.selzy.createEmailMessage(emailMessageData); |
| 132 | + if (response.error) throw new ConfigurationError(response.error); |
73 | 133 |
|
74 |
| - $.export("$summary", `Email message created successfully with ID ${response.message_id}.`); |
| 134 | + $.export("$summary", `Email message created successfully with ID ${response.result.message_id}.`); |
75 | 135 | return response;
|
76 | 136 | },
|
77 | 137 | };
|
0 commit comments