Skip to content

Commit

Permalink
New Components - openphone (#14505)
Browse files Browse the repository at this point in the history
* openphone init

* [Components] openphone #14493
Sources
 - New Call Recording Completed (Instant)
 - New Outgoing Call Completed (Instant)
 - New Incoming Call Completed (Instant)

Actions
 - Send Message
 - Create Contact
 - Update Contact

* pnpm update

* fix source
  • Loading branch information
luancazarine authored Nov 7, 2024
1 parent ac89f9b commit a981fcf
Show file tree
Hide file tree
Showing 14 changed files with 599 additions and 7 deletions.
80 changes: 80 additions & 0 deletions components/openphone/actions/create-contact/create-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { parseObject } from "../../common/utils.mjs";
import openphone from "../../openphone.app.mjs";

export default {
key: "openphone-create-contact",
name: "Create Contact",
description: "Create a new contact in OpenPhone. [See the documentation](https://www.openphone.com/docs/api-reference/contacts/create-a-contact)",
version: "0.0.1",
type: "action",
props: {
openphone,
firstName: {
propDefinition: [
openphone,
"firstName",
],
},
lastName: {
propDefinition: [
openphone,
"lastName",
],
optional: true,
},
company: {
propDefinition: [
openphone,
"company",
],
optional: true,
},
role: {
propDefinition: [
openphone,
"role",
],
optional: true,
},
emails: {
propDefinition: [
openphone,
"emails",
],
optional: true,
},
phoneNumbers: {
propDefinition: [
openphone,
"phoneNumbers",
],
optional: true,
},
customFields: {
propDefinition: [
openphone,
"customFields",
],
optional: true,
},
},
async run({ $ }) {
const response = await this.openphone.createContact({
$,
data: {
defaultFields: {
firstName: this.firstName,
lastName: this.lastName,
company: this.company,
role: this.role,
emails: parseObject(this.emails),
phoneNumbers: parseObject(this.phoneNumbers),
},
customFields: parseObject(this.customFields),
},
});

$.export("$summary", `Successfully created contact with ID: ${response.data.id}`);
return response;
},
};
57 changes: 57 additions & 0 deletions components/openphone/actions/send-message/send-message.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ConfigurationError } from "@pipedream/platform";
import openphone from "../../openphone.app.mjs";

export default {
key: "openphone-send-message",
name: "Send a Text Message via OpenPhone",
description: "Send a text message from your OpenPhone number to a recipient. [See the documentation](https://www.openphone.com/docs/api-reference/messages/send-a-text-message)",
version: "0.0.1",
type: "action",
props: {
openphone,
from: {
propDefinition: [
openphone,
"from",
],
},
to: {
type: "string",
label: "To",
description: "Recipient phone number in E.164 format.",
},
content: {
type: "string",
label: "Content",
description: "The text content of the message to be sent.",
},
},
async run({ $ }) {
try {
const response = await this.openphone.sendTextMessage({
$,
data: {
content: this.content,
from: this.from,
to: [
this.to,
],
setInboxStatus: "done",
},
});
$.export("$summary", `Successfully sent message to ${this.to}`);
return response;

} catch ({ response }) {
let errorMessage = "";

if (response.data.errors) {
errorMessage = `Prop: ${response.data.errors[0].path} - ${response.data.errors[0].message}`;
} else {
errorMessage = response.data.message;
}

throw new ConfigurationError(errorMessage);
}
},
};
87 changes: 87 additions & 0 deletions components/openphone/actions/update-contact/update-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { parseObject } from "../../common/utils.mjs";
import openphone from "../../openphone.app.mjs";

export default {
key: "openphone-update-contact",
name: "Update Contact",
description: "Update an existing contact on OpenPhone. [See the documentation](https://www.openphone.com/docs/api-reference/contacts/update-a-contact-by-id)",
version: "0.0.1",
type: "action",
props: {
openphone,
contactId: {
type: "string",
label: "Contact ID",
description: "The unique identifier of the contact.",
},
firstName: {
propDefinition: [
openphone,
"firstName",
],
optional: true,
},
lastName: {
propDefinition: [
openphone,
"lastName",
],
optional: true,
},
company: {
propDefinition: [
openphone,
"company",
],
optional: true,
},
role: {
propDefinition: [
openphone,
"role",
],
optional: true,
},
emails: {
propDefinition: [
openphone,
"emails",
],
optional: true,
},
phoneNumbers: {
propDefinition: [
openphone,
"phoneNumbers",
],
optional: true,
},
customFields: {
propDefinition: [
openphone,
"customFields",
],
optional: true,
},
},
async run({ $ }) {
const response = await this.openphone.updateContact({
$,
contactId: this.contactId,
data: {
defaultFields: {
firstName: this.firstName,
lastName: this.lastName,
company: this.company,
role: this.role,
emails: parseObject(this.emails),
phoneNumbers: parseObject(this.phoneNumbers),
},
customFields: parseObject(this.customFields),
},
});

$.export("$summary", `Successfully updated contact with ID ${this.contactId}`);
return response;
},
};
26 changes: 26 additions & 0 deletions components/openphone/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const parseObject = (obj) => {
if (!obj) return undefined;

let parsedObj = obj;
if (typeof obj === "string") {
try {
parsedObj = JSON.parse(obj);
} catch (e) {
return obj;
}
}

if (Array.isArray(parsedObj)) {
return parsedObj.map((item) => parseObject(item));
}
if (typeof parsedObj === "object") {
for (const [
key,
value,
] of Object.entries(parsedObj)) {
parsedObj[key] = parseObject(value);
}
}

return parsedObj;
};
118 changes: 114 additions & 4 deletions components/openphone/openphone.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,121 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "openphone",
propDefinitions: {},
propDefinitions: {
from: {
type: "string",
label: "From",
description: "The sender's phone number. Can be either your OpenPhone phone number ID or the full phone number in E.164 format.",
async options() {
const { data } = await this.listPhoneNumbers();
return data.map(({
id: value, name, formattedNumber,
}) => ({
label: `${name} - ${formattedNumber}`,
value,
}));
},
},
firstName: {
type: "string",
label: "First Name",
description: "The contact's first name.",
},
lastName: {
type: "string",
label: "Last Name",
description: "The contact's last name.",
optional: true,
},
company: {
type: "string",
label: "Company",
description: "The contact's company name.",
optional: true,
},
role: {
type: "string",
label: "Role",
description: "The contact's role.",
optional: true,
},
emails: {
type: "string[]",
label: "Emails",
description: "Array of objects of contact's emails. **Example:** `{\"name\": \"Company Email\", \"value\": \"[email protected]\"}`.",
},
phoneNumbers: {
type: "string[]",
label: "Phone Numbers",
description: "Array of objects of contact's phone numbers. **Example:** `{\"name\": \"Company Phone\", \"value\": \"+12345678901\"}`.",
},
customFields: {
type: "string[]",
label: "Custom Fields",
description: "Array of objects of custom fields for the contact. **Example:** `{\"key\": \"inbound-lead\", \"value\": \"[\"option1\", \"option2\"]\"}`.",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.openphone.com/v1";
},
_headers() {
return {
Authorization: `${this.$auth.api_key}`,
};
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
listPhoneNumbers(opts = {}) {
return this._makeRequest({
path: "/phone-numbers",
...opts,
});
},
createWebhook(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/webhooks/calls",
...opts,
});
},
deleteWebhook(webhookId) {
return this._makeRequest({
method: "DELETE",
path: `/webhooks/${webhookId}`,
});
},
sendTextMessage(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/messages",
...opts,
});
},
createContact(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/contacts",
...opts,
});
},
updateContact({
contactId, ...opts
}) {
return this._makeRequest({
method: "PATCH",
path: `/contacts/${contactId}`,
...opts,
});
},
},
};
7 changes: 5 additions & 2 deletions components/openphone/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/openphone",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream OpenPhone Components",
"main": "openphone.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
}
Loading

0 comments on commit a981fcf

Please sign in to comment.