Skip to content

Commit

Permalink
[Components] fractel #11791
Browse files Browse the repository at this point in the history
Actions
- Call Phone
- Send SMS/MMS
  • Loading branch information
luancazarine committed May 6, 2024
1 parent 407a066 commit 38ebdb0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 91 deletions.
15 changes: 9 additions & 6 deletions components/fractel/actions/call-phone/call-phone.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fractel from "../../fractel.app.mjs";
import { axios } from "@pipedream/platform";

export default {
key: "fractel-call-phone",
name: "Call Phone",
description: "Initiates a new phone call to the provided number.",
version: "0.0.{{ts}}",
version: "0.0.1",
type: "action",
props: {
fractel,
Expand All @@ -30,12 +29,16 @@ export default {
},
async run({ $ }) {
const response = await this.fractel.initiateCall({
phoneNumber: this.phoneNumber,
to: this.to,
message: this.message,
$,
data: {
phoneNumber: this.phoneNumber,
to: this.to,
service_type: "TTS",
service_id: this.message,
},
});

$.export("$summary", `Successfully initiated a call to ${this.to}`);
$.export("$summary", `Successfully initiated a call with Id: ${response.call.id}`);
return response;
},
};
63 changes: 33 additions & 30 deletions components/fractel/actions/send-sms-mms/send-sms-mms.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseObject } from "../../common/utils.mjs";
import fractel from "../../fractel.app.mjs";
import { axios } from "@pipedream/platform";

export default {
key: "fractel-send-sms-mms",
Expand All @@ -9,49 +9,52 @@ export default {
type: "action",
props: {
fractel,
to: fractel.propDefinitions.to,
phoneNumber: fractel.propDefinitions.phoneNumber,
message: {
...fractel.propDefinitions.message,
optional: true,
},
media: {
...fractel.propDefinitions.media,
optional: true,
phoneNumber: {
propDefinition: [
fractel,
"phoneNumber",
],
description: "The phone number to send the message to, including country code.",
},
confirmationUrl: {
...fractel.propDefinitions.confirmationUrl,
optional: true,
to: {
propDefinition: [
fractel,
"to",
],
},
confirmationUrlUsername: {
...fractel.propDefinitions.confirmationUrlUsername,
message: {
propDefinition: [
fractel,
"message",
],
description: "The message content for SMS or MMS.",
optional: true,
},
confirmationUrlPassword: {
...fractel.propDefinitions.confirmationUrlPassword,
media: {
propDefinition: [
fractel,
"media",
],
optional: true,
},
},
async run({ $ }) {
if (!this.message && !this.media) {
throw new Error("Either message or media must be provided.");
}

const data = {
to: this.to,
from: this.phoneNumber,
message: this.message,
media: this.media,
confirmation_url: this.confirmationUrl,
confirmation_url_username: this.confirmationUrlUsername,
confirmation_url_password: this.confirmationUrlPassword,
};

const response = await this.fractel.sendMessage(data);
const response = await this.fractel.sendMessage({
$,
data: {
to: this.to,
phonenumber: this.phoneNumber,
message: this.message,
media: this.media && parseObject(this.media),
},
});

$.export("$summary", `Successfully sent ${this.message
? "SMS"
: "MMS"} to ${this.to}`);
: "MMS"} with Id: ${response.message.id}`);
return response;
},
};
22 changes: 22 additions & 0 deletions components/fractel/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const parseObject = (obj) => {
if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};
82 changes: 28 additions & 54 deletions components/fractel/fractel.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export default {
phoneNumber: {
type: "string",
label: "Phone Number",
description: "The phone number to call or send the message to, including country code.",
description: "The phone number to call to, including country code.",
async options() {
const { fonenumbers } = await this.listPhoneNumbers();
return fonenumbers.map(({ phonenumber }) => phonenumber);
},
},
to: {
type: "string",
Expand All @@ -17,81 +21,51 @@ export default {
message: {
type: "string",
label: "Message",
description: "The message content for SMS or MMS.",
description: "The message content for TTS.",
},
media: {
type: "string",
type: "string[]",
label: "Media URL",
description: "The URL of the media file for MMS.",
optional: true,
},
confirmationUrl: {
type: "string",
label: "Confirmation URL",
description: "The URL to receive message delivery confirmations.",
optional: true,
},
confirmationUrlUsername: {
type: "string",
label: "Confirmation URL Username",
description: "The username for HTTP Basic authentication for the confirmation URL.",
optional: true,
},
confirmationUrlPassword: {
type: "string",
label: "Confirmation URL Password",
description: "The password for HTTP Basic authentication for the confirmation URL.",
description: "List of URL of the media file for MMS.",
optional: true,
},
},
methods: {
_baseUrl() {
return "https://api.fonestorm.com/v2";
},
async _makeRequest(opts = {}) {
const {
$ = this, method = "GET", path, headers, ...otherOpts
} = opts;
_headers() {
return {
token: `${this.$auth.oauth_access_token}`,
};
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
...otherOpts,
method,
url: this._baseUrl() + path,
headers: {
...headers,
Authorization: `Bearer ${this.$auth.api_key}`,
},
headers: this._headers(),
...opts,
});
},
async initiateCall({
phoneNumber, to, message,
}) {
initiateCall(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/calls",
data: {
from: phoneNumber,
to,
message,
},
...opts,
});
},
async sendMessage({
to, message, media, confirmationUrl, confirmationUrlUsername, confirmationUrlPassword,
}) {
const data = {
to,
message,
};
if (media) data.media = media;
if (confirmationUrl) {
data.confirmation_url = confirmationUrl;
if (confirmationUrlUsername) data.confirmation_url_username = confirmationUrlUsername;
if (confirmationUrlPassword) data.confirmation_url_password = confirmationUrlPassword;
}
listPhoneNumbers(opts = {}) {
return this._makeRequest({
path: "/phonenumbers",
...opts,
});
},
sendMessage(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/messages/send",
data,
...opts,
});
},
},
Expand Down
5 changes: 4 additions & 1 deletion components/fractel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/fractel",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream FracTEL Components",
"main": "fractel.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.6.5"
}
}

0 comments on commit 38ebdb0

Please sign in to comment.