Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions components/sinch/actions/send-fax/send-fax.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import sinch from "../../sinch.app.mjs";
import { getFileStreamAndMetadata } from "@pipedream/platform";
import FormData from "form-data";

export default {
key: "sinch-send-fax",
name: "Send Fax",
description: "Send a fax to a contact. [See the documentation](https://developers.sinch.com/docs/fax/api-reference/fax/tag/Faxes/#tag/Faxes/operation/sendFax)",
version: "0.0.1",
type: "action",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: false,
},
props: {
sinch,
to: {
type: "string",
label: "To",
description: "The phone number to send the fax to",
},
file: {
type: "string",
label: "File Path or URL",
description: "Provide either a file URL or a path to a file in the /tmp directory (for example, /tmp/myFile.pdf).",
},
from: {
type: "string",
label: "From",
description: "The phone number of the sender",
optional: true,
},
headerText: {
type: "string",
label: "Header Text",
description: "Text that will be displayed at the top of each page of the fax. 50 characters maximum.",
optional: true,
},
retryDelaySeconds: {
type: "integer",
label: "Retry Delay Seconds",
description: "The number of seconds to wait between retries if the fax is not yet completed",
optional: true,
},
syncDir: {
type: "dir",
accessMode: "read",
sync: true,
optional: true,
},
},
async run({ $ }) {
const data = new FormData();

const {
stream, metadata,
} = await getFileStreamAndMetadata(this.file);

data.append("file", stream, {
contentType: metadata.contentType,
knownLength: metadata.size,
filename: metadata.name,
});

data.append("to", this.to);
if (this.from) {
data.append("from", this.from);
}
if (this.headerText) {
data.append("headerText", this.headerText);
}
if (this.retryDelaySeconds) {
data.append("retryDelaySeconds", this.retryDelaySeconds);
}

const response = await this.sinch.sendFax({
$,
data,
headers: data.getHeaders(),
});

$.export("$summary", `Successfully sent fax to ${this.to}`);
return response;
},
};
88 changes: 88 additions & 0 deletions components/sinch/actions/send-message/send-message.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import sinch from "../../sinch.app.mjs";
import constants from "../../common/constants.mjs";
import { ConfigurationError } from "@pipedream/platform";

export default {
key: "sinch-send-message",
name: "Send Message",
description: "Send a message to a contact. [See the documentation](https://developers.sinch.com/docs/conversation/api-reference/conversation/tag/Messages/#tag/Messages/operation/Messages_SendMessage)",
version: "0.0.1",
type: "action",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: false,
},
props: {
sinch,
appId: {
propDefinition: [
sinch,
"appId",
],
},
message: {
type: "string",
label: "Message",
description: "The message to send",
},
contactId: {
propDefinition: [
sinch,
"contactId",
],
description: "The ID of the recipient. Overrides channel and identity",
optional: true,
},
channel: {
type: "string",
label: "Channel",
description: "The channel to send the message to",
options: constants.CHANNELS,
optional: true,
},
identity: {
type: "string",
label: "Identity",
description: "The channel identity. This will differ from channel to channel. For example, a phone number for SMS, WhatsApp, and Viber Business.",
optional: true,
},
},
async run({ $ }) {
if (!this.contactId && !this.identity) {
throw new ConfigurationError("You must provide either a contact ID or an identity.");
}

if (this.identity && !this.channel) {
throw new ConfigurationError("You must provide a channel when providing an identity.");
}

const response = await this.sinch.sendMessage({
$,
data: {
app_id: this.appId,
recipient: this.contactId
? {
contact_id: this.contactId,
}
: {
identified_by: {
channel_identities: [
{
channel: this.channel,
identity: this.identity,
},
],
},
},
message: {
text_message: {
text: this.message,
},
},
},
});
$.export("$summary", "Successfully sent message");
return response;
},
};
20 changes: 20 additions & 0 deletions components/sinch/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const CHANNELS = [
"WHATSAPP",
"RCS",
"SMS",
"MESSENGER",
"VIBER",
"VIBERBM",
"MMS",
"INSTAGRAM",
"TELEGRAM",
"KAKAOTALK",
"KAKAOTALKCHAT",
"LINE",
"WECHAT",
"APPLEBC",
];

export default {
CHANNELS,
};
8 changes: 6 additions & 2 deletions components/sinch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/sinch",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Sinch Components",
"main": "sinch.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.1.0",
"form-data": "^4.0.4"
}
}
}
134 changes: 129 additions & 5 deletions components/sinch/sinch.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,135 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "sinch",
propDefinitions: {},
propDefinitions: {
appId: {
type: "string",
label: "App ID",
description: "The ID of an app",
async options() {
const { apps } = await this.listApps();
return apps?.map(({
id: value, display_name: label,
}) => ({
label,
value,
})) || [];
},
},
contactId: {
type: "string",
label: "Contact ID",
description: "The ID of a contact",
async options({ prevContext }) {
const {
contacts, next_page_token: next,
} = await this.listContacts({
params: {
page_token: prevContext?.nextPageToken,
},
});
return {
options: contacts?.map(({
id: value, display_name: label,
}) => ({
label,
value,
})) || [],
context: {
nextPageToken: next,
},
};
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_getBaseUrl(api) {
if (api === "conversation") {
return `https://${this.$auth.region}.conversation.api.sinch.com/v1`;
}
if (api === "fax") {
return "https://fax.api.sinch.com/v3";
}
},
_makeRequest({
$ = this, path, api = "conversation", headers, ...opts
}) {
return axios($, {
url: `${this._getBaseUrl(api)}/projects/${this.$auth.project_id}${path}`,
headers: {
...headers,
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
...opts,
});
},
createWebhook(opts = {}) {
return this._makeRequest({
method: "POST",
api: "conversation",
path: "/webhooks",
...opts,
});
},
deleteWebhook({
webhookId, ...opts
}) {
return this._makeRequest({
method: "DELETE",
api: "conversation",
path: `/webhooks/${webhookId}`,
...opts,
});
},
createService(opts = {}) {
return this._makeRequest({
method: "POST",
api: "fax",
path: "/services",
...opts,
});
},
deleteService({
serviceId, ...opts
}) {
return this._makeRequest({
method: "DELETE",
api: "fax",
path: `/services/${serviceId}`,
...opts,
});
},
listApps(opts = {}) {
return this._makeRequest({
api: "conversation",
path: "/apps",
...opts,
});
},
listContacts(opts = {}) {
return this._makeRequest({
api: "conversation",
path: "/contacts",
...opts,
});
},
sendMessage(opts = {}) {
return this._makeRequest({
method: "POST",
api: "conversation",
path: "/messages:send",
...opts,
});
},
sendFax(opts = {}) {
return this._makeRequest({
method: "POST",
api: "fax",
path: "/faxes",
...opts,
});
},
},
};
};
28 changes: 28 additions & 0 deletions components/sinch/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sinch from "../../sinch.app.mjs";
import { ConfigurationError } from "@pipedream/platform";

export default {
props: {
sinch,
db: "$.service.db",
http: "$.interface.http",
},
methods: {
_getHookId() {
return this.db.get("hookId");
},
_setHookId(hookId) {
this.db.set("hookId", hookId);
},
generateMeta() {
throw new ConfigurationError("generateMeta is not implemented");
},
},
async run(event) {
const { body } = event;
if (!body) {
return;
}
this.$emit(body, this.generateMeta(body));
},
};
Loading
Loading