Skip to content

Commit

Permalink
[Component]: hey: New components
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortes committed Oct 25, 2024
1 parent 31906cf commit dffe261
Show file tree
Hide file tree
Showing 10 changed files with 761 additions and 10 deletions.
96 changes: 96 additions & 0 deletions components/heyy/actions/create-contact/create-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import app from "../../heyy.app.mjs";

export default {
key: "heyy-create-contact",
name: "Create Contact",
description: "Creates a new contact for the business. [See the documentation](https://documenter.getpostman.com/view/27408936/2sA2r3a6DW#a1249b8d-10cf-446a-be35-eb8793ffa967).",
version: "0.0.1",
type: "action",
props: {
app,
phoneNumber: {
propDefinition: [
app,
"phoneNumber",
],
},
firstName: {
propDefinition: [
app,
"firstName",
],
},
lastName: {
propDefinition: [
app,
"lastName",
],
},
email: {
propDefinition: [
app,
"email",
],
},
labels: {
propDefinition: [
app,
"labels",
],
},
attributes: {
propDefinition: [
app,
"attributes",
],
},
},
methods: {
createContact(args = {}) {
return this.app.post({
path: "/contacts",
...args,
});
},
},
async run({ $ }) {
const {
createContact,
phoneNumber,
firstName,
lastName,
email,
labels,
attributes,
} = this;

const response = await createContact({
$,
data: {
phoneNumber,
firstName,
lastName,
email,
...(labels?.length && {
labels: labels.map((name) => ({
name,
})),
}),
attributes:
attributes && Object.entries(attributes)
.reduce((acc, [
externalId,
value,
]) => ([
...acc,
{
externalId,
value,
},
]), []),
},
});
$.export("$summary", `Successfully created contact with ID \`${response.data.id}\`.`);
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import app from "../../heyy.app.mjs";
import constants from "../../common/constants.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "heyy-send-whatsapp-message",
name: "Send WhatsApp Message",
description: "Sends a WhatsApp message to a contact. [See the documentation](https://documenter.getpostman.com/view/27408936/2sa2r3a6dw)",
version: "0.0.1",
type: "action",
props: {
app,
channelId: {
propDefinition: [
app,
"channelId",
],
},
phoneNumber: {
label: "Phone Number",
description: "The phone number of the contact.",
propDefinition: [
app,
"contactId",
() => ({
mapper: ({
firstName, phoneNumber: value,
}) => ({
label: firstName || value,
value,
}),
}),
],
},
msgType: {
type: "string",
label: "Message Type",
description: "The type of message to send.",
options: Object.values(constants.MSG_TYPE),
reloadProps: true,
},
},
additionalProps() {
const { msgType } = this;

const bodyText = {
type: "string",
label: "Body Text",
description: "The text of the message to send.",
};

if (msgType === constants.MSG_TYPE.TEXT) {
return {
bodyText,
};
}

if (msgType === constants.MSG_TYPE.IMAGE) {
return {
bodyText,
fileId: {
type: "string",
label: "File ID",
description: "The ID of the file to attach to the message.",
},
};
}

if (msgType === constants.MSG_TYPE.TEMPLATE) {
return {
messageTemplateId: {
type: "string",
label: "Message Template ID",
description: "The ID of the message template to use.",
optional: true,
options: async ({ page }) => {
const { data: { messageTemplates } } = await this.app.getMessageTemplates({
params: {
page,
sortBy: "updatedAt",
order: "DESC",
},
});
return messageTemplates.map(({
id: value, name: label,
}) => ({
label,
value,
}));
},
},
};
}

if (msgType === constants.MSG_TYPE.INTERACTIVE) {
return {
bodyText,
buttons: {
type: "string[]",
label: "Buttons",
description: "The buttons to include in the message. Each row should have a JSON formated string. Eg. `{ \"id\": \"STRING\", \"title\": \"STRING\" }`.",
},
headerText: {
type: "string",
label: "Header Text",
description: "The header text of the message to send.",
optional: true,
},
footerText: {
type: "string",
label: "Footer Text",
description: "The footer text of the message to send.",
optional: true,
},
};
}

return {};
},
methods: {
sendWhatsappMessage({
channelId, ...args
} = {}) {
return this.app.post({
path: `/${channelId}/whatsapp_messages/send`,
...args,
});
},
},
async run({ $ }) {
const {
sendWhatsappMessage,
channelId,
phoneNumber,
msgType,
bodyText,
fileId,
messageTemplateId,
headerText,
footerText,
buttons,
} = this;

const response = await sendWhatsappMessage({
$,
channelId,
data: {
phoneNumber,
type: msgType,
bodyText,
fileId,
messageTemplateId,
headerText,
footerText,
buttons: utils.parseArray(buttons),
},
});
$.export("$summary", "Succesfully sent WhatsApp message.");
return response;
},
};
109 changes: 109 additions & 0 deletions components/heyy/actions/update-contact/update-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import app from "../../heyy.app.mjs";

export default {
key: "heyy-update-contact",
name: "Update Contact",
description: "Updates the details of a contact under your business.",
version: "0.0.1",
type: "action",
props: {
app,
contactId: {
propDefinition: [
app,
"contactId",
],
},
phoneNumber: {
optional: true,
propDefinition: [
app,
"phoneNumber",
],
},
firstName: {
optional: true,
propDefinition: [
app,
"firstName",
],
},
lastName: {
propDefinition: [
app,
"lastName",
],
},
email: {
propDefinition: [
app,
"email",
],
},
labels: {
propDefinition: [
app,
"labels",
],
},
attributes: {
propDefinition: [
app,
"attributes",
],
},
},
methods: {
updateContact({
contactId, ...args
} = {}) {
return this.app.put({
path: `/contacts/${contactId}`,
...args,
});
},
},
async run({ $ }) {
const {
updateContact,
contactId,
phoneNumber,
firstName,
lastName,
email,
labels,
attributes,
} = this;

const response = await updateContact({
$,
contactId,
data: {
phoneNumber,
firstName,
lastName,
email,
...(labels?.length && {
labels: labels.map((name) => ({
name,
})),
}),
attributes:
attributes && Object.entries(attributes)
.reduce((acc, [
externalId,
value,
]) => ([
...acc,
{
externalId,
value,
},
]), []),
},
});

$.export("$summary", `Successfully updated contact with ID \`${response.data.id}\`.`);
return response;
},
};
Loading

0 comments on commit dffe261

Please sign in to comment.