Slender the Blender
Slender the Blender

Reputation: 21

Node.js - How to Upload an MP4 to Twitter

I'm currently learning how to make a Twitter bot with Node.js, and I can't figure out how to upload an mp4 to Twitter.

I have searched around on Google and YouTube on how but it just seems to either give me more errors or just seem to not be needed for how I have done it.

Now, I am new to Node.js so forgive me if I seem to be mistaking basic things.

Here is my current code:

const Twit = require('twit'),
  fs = require('fs'),
  path = require('path'),
  config = require(path.join( __dirname, 'config.js') );




async function main() {
  const T = new Twit( config );

  T.post('statuses/update', {
    status: 'File time lezgo',
    media: "./media/congrats.mp4"
  }, function( err, data, response ) {
    console.log( data )
  } );
}

console.log("Starting the bot...");

setInterval(main, 5000)

If anyone has an explanation on how to do it or if I am missing something then please do let me know, as I - like I said - am new to Node.js and the Twitter API.

Upvotes: 1

Views: 794

Answers (1)

Shivam
Shivam

Reputation: 3642

I don't think you can directly attach Media to your tweet, you have to first upload the media and use the returned media_id in the statuses/update endpoint

Following example is taken from twit lib itself (little bit cleaned though)

let b64content = fs.readFileSync('/path/to/media', {
    encoding: 'base64'
})

// first we must post the media to Twitter
T.post('media/upload', {
    media_data: b64content
}, (err, data, response) => {

    if (err) {
        console.error(err);
    }
    // now we can assign alt text to the media, for use by screen readers and
    // other text-based presentations and interpreters
    let mediaIdStr = data.media_id_string
    let altText = "This is an ALT text"
    let meta_params = {
        media_id: mediaIdStr,
        alt_text: {
            text: altText
        }
    }

    T.post('media/metadata/create', meta_params, (err, data, response) => {
        if (!err) {
            // now we can reference the media and post a tweet (media will attach to the tweet)
            let params = {
                status: 'File time lezgo',
                media_ids: [mediaIdStr]
            }

            T.post('statuses/update', params, (err, data, response) => {
                console.log(data)
            })
        }
    })
})

This can be further cleaned up by using async/await but for now this should work

Update

For .mp4 type of files you will have to use postMediaChunked method

let filePath = '/absolute/path/to/file.mp4'

T.postMediaChunked({
    file_path: filePath
}, (err, data, response) => {

    if (err) {
        cosnole.error(err);
    }

    let mediaIdStr = data.media_id_string;

    let params = {
        status: 'File time lezgo',
        media_ids: [mediaIdStr]
    }

    T.post('statuses/update', params, (err, data, response) => {
        console.log(data)
    })
})

Upvotes: 1

Related Questions