Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Tweet Media Files with Twit.js


    In this lesson, we’ll create multiple functions to request, download, and save photos and data from NASA's API, and then have our bot upload these photos to Twitter and post them along with their descriptions. We'll also learn how to tweet videos using a video from NASA’s space archives.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    In addition to Twit, we'll also need request since we'll be making requests, and FS since we'll be working with the file system. The first thing we're going to do is get NASA's photo of the day. We'll wrap all these in functions so we can use them again. We'll need our parameters which are a URL,

    You will need your queries. We only need to give it the API key which you'll need to register for with NASA, and then add to your environment variables. Then an encoding, which will be binary.

    We can say request.getParameters, and then our callback, error response, and the body. With this API, we need to parse the body. We'll make another function saveFile, and we'll pass it the body and the name we want to save our file as. Let's write the saveFile function.

    First, we'll say barFile = fs.createWriteStream with the file name. The way we save it is requestBody.pipeFile, and then we'll also say .onClose function error. If there is an error then we'll log it out, otherwise we can say media is saved.

    Let's see what this does. If we run that, we see media saved. If we look in our folder, we can see that the image is there. Let's also print out the information that we're getting. We've got a title, we've got the URL of the image, we've got an interesting explanation, we've got the date. One thing we're going to need going forward is a small description.

    We'll say the description text is equal to the body's title. Here that would be penumbral eclipse rising. We're going to upload our media, and we're going to pass this the description text and the file name.

    There are two ways to upload media to Twitter, but the Twitter API will eventually only use the chunked version, so that's the one I'll show you today. To upload media in chunks, we first need to give it the file path.

    To get the file path we'll get the path to our directory, dirname plus a slash, plus our file name. We'll say bot.postMediaChunked, a file path which we'll pass our file path variable, and our callback. If there's an error, we'll log it, otherwise, let's log out our data.

    Let's erase this one for now. We can see that after uploading this media, Twitter gives us back information about it including media ID, which we'll need in a second.

    From here we're going to want to post our image to Twitter. We need a few parameters, the first being the status which is going to be our description text, and the media IDs, which is the media ID string. Here, we'll post our status with our parameters. To post our image is straightforward, and works the same way that we post a status.

    It's bot.postStatuses/update the params and our callback. If there's an error we'll log it out, otherwise, we'll say status posted.

    Let's see if this works. If we go to our Twitter account, we can see that the image has been posted. That's a beautiful one. You can also upload a video, so we'll do that directly by saying uploadMedia. We need a description and a file name.

    If we go back to our timeline, we can see that the video was posted as well.