actions/download-recording-media/download-recording-media.mjs
import twilio from "../../twilio.app.mjs"; import stream from "stream"; import { promisify } from "util"; import fs from "fs"; import { axios } from "@pipedream/platform"; export default { key: "twilio-download-recording-media", name: "Download Recording Media", description: "Download a recording media file. [See the documentation](https://www.twilio.com/docs/voice/api/recording#fetch-a-recording-media-file)", version: "0.1.9", annotations: { destructiveHint: false, openWorldHint: true, readOnlyHint: false, }, type: "action", props: { twilio, recordingID: { propDefinition: [ twilio, "recordingID", ], }, format: { propDefinition: [ twilio, "format", ], }, filePath: { type: "string", label: "File Path", description: "The destination path in [`/tmp`](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory) for the downloaded the file (e.g., `/tmp/myFile.mp3`). Make sure to include the file extension.", }, syncDir: { type: "dir", accessMode: "write", sync: true, }, }, methods: { getFileStream({ $, downloadUrl, }) { return axios($, { url: downloadUrl, auth: { username: `${this.twilio.$auth.AccountSid}`, password: `${this.twilio.$auth.AuthToken}`, }, responseType: "stream", }); }, }, async run({ $ }) { const recording = await this.twilio.getRecording(this.recordingID); const client = this.twilio.getClient(); const uri = client.api.absoluteUrl(recording.uri).replace(".json", ""); const downloadUrl = uri + this.format; const fileStream = await this.getFileStream({ $, downloadUrl, }); const pipeline = promisify(stream.pipeline); const filePath = this.filePath.startsWith("/tmp") ? this.filePath : `/tmp/${this.filePath}`; await pipeline( fileStream, fs.createWriteStream(filePath), ); $.export("$summary", `Successfully downloaded the recording media file to "${filePath}"`); return { filePath, }; }, };