Skip to content

Commit

Permalink
Frontapp refactor and receive custom messages action (PipedreamHQ#2826)
Browse files Browse the repository at this point in the history
* Refactored actions and added new action file receive-custom-messages

* First version without tests

* Added new line at the end of file package.json

* Addressed minor fixes in components according to CR

* Addressed comments in PR

* Added filter params in paginateOptions function

* Added null value at the end of options

* Replaced api lib with axios due to some error using the sdk

* fixed several issues adrresed in PR and pnpm additions

* WIP trying to fix formData issus with attachments

* Fixed attachment and response issues

* Fixed undefined string in update-conversation action

* Fixed cc and bcc not being sent in send-new-message
  • Loading branch information
jcortes authored Jun 30, 2022
1 parent aaeb01a commit 7300747
Show file tree
Hide file tree
Showing 10 changed files with 1,150 additions and 190 deletions.
206 changes: 116 additions & 90 deletions components/frontapp/actions/import-message/import-message.mjs
Original file line number Diff line number Diff line change
@@ -1,74 +1,90 @@
// legacy_hash_id: a_a4iKLb
import { axios } from "@pipedream/platform";
import frontApp from "../../frontapp.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "frontapp-import-message",
name: "Import Message",
description: "Appends a new message into an inbox.",
version: "0.1.2",
description: "Appends a new message into an inbox. [See the docs here](https://dev.frontapp.com/reference/import-inbox-message).",
version: "0.1.3",
type: "action",
props: {
frontapp: {
type: "app",
app: "frontapp",
frontApp,
inboxId: {
propDefinition: [
frontApp,
"inboxId",
],
},
handle: {
type: "string",
label: "Handle",
description: "Handle used to reach the contact. Can be an email address, a twitter, handle, a phone number, custom handle ...",
},
name: {
type: "string",
label: "Name",
description: "Name of the contact.",
optional: true,
},
author_id: {
type: "string",
authorId: {
propDefinition: [
frontApp,
"teammateId",
],
label: "Author ID",
description: "ID of the teammate who is the author of the message. Ignored if the message is inbound.",
optional: true,
},
to: {
type: "any",
description: "List of recipient handles who received the message.",
propDefinition: [
frontApp,
"to",
],
},
cc: {
type: "any",
description: "List of recipient handles who received a copy of the message.",
optional: true,
propDefinition: [
frontApp,
"cc",
],
},
bcc: {
type: "any",
description: "List of the recipeient handles who received a blind copy of the message.",
optional: true,
propDefinition: [
frontApp,
"bcc",
],
},
subject: {
type: "string",
label: "Subject",
description: "Subject of the message.",
optional: true,
},
body: {
type: "string",
label: "Body",
description: "Body of the message.",
},
body_format: {
type: "string",
description: "Format of the message body. Ignored if the message type is not email. Can be one of: 'html', 'markdown'. (Default: 'markdown')",
optional: true,
options: [
"html",
"markdown",
bodyFormat: {
propDefinition: [
frontApp,
"bodyFormat",
],
},
external_id: {
externalId: {
type: "string",
label: "External ID",
description: "External identifier of the message. Front won't import two messages with the same external ID.",
},
created_at: {
createdAt: {
type: "integer",
description: "Date at which the message as been sent or received. A timestamp is expected as in 1453770984.123",
label: "Created At",
description: "Date at which the message has been sent or received. A timestamp is expected as in `1655507769`",
default: Math.floor(Date.now() / 1000),
},
type: {
type: "string",
description: "Type of the message to import. Can be one of: 'email', 'sms', 'intercom', 'custom'. (Default: 'email')",
label: "Type",
description: "Type of the message to import. Can be one of: `email`, `sms`, `intercom`, `custom`. (Default: `email`)",
optional: true,
options: [
"email",
Expand All @@ -77,94 +93,104 @@ export default {
"custom",
],
},
assignee_id: {
type: "string",
assigneeId: {
propDefinition: [
frontApp,
"teammateId",
],
label: "Assignee ID",
description: "ID of the teammate who will be assigned to the conversation.",
optional: true,
},
tags: {
type: "any",
propDefinition: [
frontApp,
"tagIds",
],
description: "List of tag names to add to the conversation (unknown tags will automatically be created)",
optional: true,
},
thread_ref: {
type: "string",
description: "Custom reference which will be used to thread messages. If you omit this field, we'll thread by sender instead.",
optional: true,
threadRef: {
propDefinition: [
frontApp,
"threadRef",
],
},
is_inbound: {
isInbound: {
type: "boolean",
label: "Is Inbound",
description: "Whether or not the message is received (inbound) or sent (outbound) by you",
},
archive: {
isArchive: {
type: "boolean",
label: "Is Archive",
description: "Whether or not the message should be directly archived once imported. (Default: true)",
optional: true,
},
should_skip_rules: {
shouldSkipRules: {
type: "boolean",
label: "Should Skip Rules",
description: "Whether or not the rules should apply to this message. (Default: true)",
optional: true,
},
inbox_id: {
type: "string",
description: "Id of the inbox into which the message should be append.",
},
},
async run({ $ }) {
//FrontApp api specifies body should be sent as a data binary.
//One way to comply with this is to populate an JS object normally
//and stringify it before requesting.
const {
inboxId,
handle,
name,
authorId,
subject,
body,
bodyFormat,
externalId,
createdAt,
type,
assigneeId,
threadRef,
isArchive,
isInbound,
shouldSkipRules,
} = this;

const to = utils.parse(this.to);
const cc = utils.parse(this.cc);
const bcc = utils.parse(this.bcc);
const tags = utils.parse(this.tags);

var messageToImportData = {
const data = {
sender: {
handle: this.handle,
name: this.name,
author_id: this.author_id,
author_id: authorId,
name,
handle,
},
to: typeof this.to == "undefined"
? this.to
: JSON.parse(this.to),
cc: typeof this.cc == "undefined"
? this.cc
: JSON.parse(this.cc),
bcc: typeof this.bcc == "undefined"
? this.bcc
: JSON.parse(this.bcc),
subject: this.subject,
body: this.body,
body_format: this.body_format,
external_id: this.external_id,
created_at: this.created_at,
type: this.type,
assignee_id: this.assignee_id,
tags: typeof this.tags == "undefined"
? this.tags
: JSON.parse(this.tags),
to,
cc,
bcc,
subject,
body,
body_format: bodyFormat,
external_id: externalId,
created_at: createdAt,
type,
assignee_id: assigneeId,
tags,
metadata: {
thread_ref: this.thread_ref,
is_inbound: new Boolean(this.is_inbound),
is_archived: typeof this.archive == "undefined"
? true
: new Boolean(this.archive),
should_skip_rules: typeof this.should_skip_rules == "undefined"
? true
: new Boolean(this.should_skip_rules),
thread_ref: threadRef,
is_inbound: isInbound,
is_archived: isArchive,
should_skip_rules: shouldSkipRules,
},
};

const effectiveRequestBody = JSON.stringify(messageToImportData);
$.export("effective_request_body", effectiveRequestBody);
const response =
await this.frontApp.importMessage({
inboxId,
data,
});

return await axios($, {
method: "post",
url: `https://api2.frontapp.com/inboxes/${this.inbox_id}/messages`,
headers: {
"Authorization": `Bearer ${this.frontapp.$auth.oauth_access_token}`,
"Content-Type": "application/json",
"Accept": "application/json",
},
data: effectiveRequestBody,
});
const { message_uid: messageId } = response;

$.export("$summary", `Successfully imported message with ID ${messageId}`);

return response;
},
};
Loading

0 comments on commit 7300747

Please sign in to comment.