This topic was automatically generated from Slack. You can find the original thread here.
Hello, I am trying to prepare the step in my workflow that will upload an image to Twitter to have it later attached to the tweet I will send in the next step.
I connected my App (confirmed that it works with the step that sends the tweet without media).
My code is (with some logs to check how far it can get
import axios from 'axios';
import fs from 'fs';
export default defineComponent({
props: {
twitter: {
type: 'app',
app: 'twitter',
},
url: {
type: 'string',
label: 'Photo URL',
description: 'Full URL to the image to upload to Twitter',
},
},
async run() {
const oauthSignerUri = this.twitter.$auth.oauth_signer_uri;
const token = {
key: this.twitter.$auth.oauth_access_token,
secret: this.twitter.$auth.oauth_refresh_token,
};
const signConfig = {
token,
oauthSignerUri,
};
console.log('Step 1: Downloading the image');
const imageResponse = await axios({
url: this.url,
method: 'GET',
responseType: 'arraybuffer',
});
console.log('Image downloaded:', imageResponse);
// Save the image file locally in Pipedream
const fileName = 'image.png'; // Change the file name if necessary
const filePath = `/tmp/${fileName}`;
fs.writeFileSync(filePath, imageResponse.data);
console.log('Image saved locally:', filePath);
// Upload the image file to Twitter as media
console.log('Step 2: Uploading image to Twitter');
const mediaUploadInitRequest = {
method: 'POST',
url: 'https://upload.twitter.com/1.1/media/upload.json',
params: {
command: 'INIT',
total_bytes: fs.statSync(filePath).size,
media_type: 'image/png', // Change the media type if necessary
},
};
const mediaUploadInitResponse = await axios(this.twitter, mediaUploadInitRequest, signConfig);
const mediaId = mediaUploadInitResponse.data.media_id_string;
const mediaUploadAppendRequest = {
method: 'POST',
url: 'https://upload.twitter.com/1.1/media/upload.json',
params: {
command: 'APPEND',
media_id: mediaId,
segment_index: 0,
},
data: fs.readFileSync(filePath),
};
await axios(this.twitter, mediaUploadAppendRequest, signConfig);
const mediaUploadFinalizeRequest = {
method: 'POST',
url: 'https://upload.twitter.com/1.1/media/upload.json',
params: {
command: 'FINALIZE',
media_id: mediaId,
},
};
await axios(this.twitter, mediaUploadFinalizeRequest, signConfig);
console.log('Image uploaded to Twitter:', mediaId);
// Add more code here as needed
return { success: true, mediaId };
},
});