DEV Community

Hamza for XDevelopers

Posted on

Build a Twitter NASA app πŸš€

This project is hosted on Glitch: https://glitch.com/~dailynasa

Once a day, NASA features an astronomy or space science related image through their public API. The images are pretty cool and teach you about the incredible solar system we live in!

As an astronomy enthusiast, I wondered how I could easily view these images and even get them to appear on my Twitter timeline. I got to work on this, and ended up putting together this fully automated Daily NASA app that Tweets images released each day by NASA.

Our node.js app will connect to the NASA API, temporarily store media provided and post a Tweet with the title + image/video via the Twitter API. Here is an example. Pretty simple!

In this tutorial, I'll also show you how to build this app using the Twitter and NASA APIs. To see the app in action, check out the Twitter page @DailyNASA.

What will we be doing today?

Our app is going to be hosted on Glitch, so we won't be doing much except making sure our app is properly configured.

Once our project has been remixed and the .env file's keys and tokens properly configured, we'll schedule a cron-job to make our app Tweet. A cron-job is essentially an automated wake-up call.

How does the code work exactly?

  1. The app will be activated via a webhook. The webhook will be pinged by our cron-job at a specified time of day ⏰
  2. Our app will connect to the NASA API πŸš€
  3. The app will check if the media provided by the NASA API is an image URL or video URL. If it’s a photo, our app will download the image and Tweet it. If it’s a video, the app will just Tweet the video URL:
    • Video πŸ“Ή - the app will save the video URL + title and Tweet this via the Twitter statuses/update endpoint. Here’s an example.
    • Image πŸ“· - the app will save the image to a temporary directory and Tweet this via the Twitter statuses/update endpoint. Here’s an example. Because it's a temporary directory, we don't have to worry about filling up space (pun intended) πŸ₯

Prerequisites

Before we get started, you'll need the following:

Once you have all of these, you're ready to launch πŸš€!

Recipe

Step 1 - πŸ’Ώ

a) On our Glitch project page, remix this project! Click on the "Remix Project" button on the top left of this page.

Remix

b) Name your app.

I called mine dailynasa

Name

Step 2 - πŸ”‘

A) Enter your Twitter apps keys and tokens in the projects .env file

Keys

B) Enter your NASA API Key on the projects .env file

C) Give your app endpoint a name

For example, if I label mine wakeup, your .env file should look like this: BOT_ENDPOINT='awaken'. My final endpoint will look like this: https://dailynasa.glitch.me/wakeup

🚨 IMPORTANT - keep all access keys, tokens, and endpoint names private 🚨

Step 3 - TESTING TESTING !!

Now you've configured our app, let's test it out.

I usually reccomend viewing the logs in Tools β†’ Logs to verify our app runs when we wake it up.

In a new browser window, enter your unique endpoint URL and hit enter. (You can also test the URL via your command line using the following command: curl https://dailynasa.glitch.me/wakeup

Give it a second, and on your Twitter account you should see the daily NASA Tweet posted!

Now you've verified that it works, let's make sure that it's automated so you don't have to keep doing this every single day.

Step 4 - ⏰

We'll be using cron-job.org to ping our endpoint and make our app post a daily Tweet.

Create a new cron-job and fill out the page to this effect:

cron-job

🚨 IMPORTANT - scheduling your app to run too frequently can seem spammy and lead to violation of Twitters automation policy. 🚨

Once a job has run, you can view the response on this page.

And just like that, we're done!

Congratulations!

You did it! 😎

If you enjoy learning from tutorials or have any questions, check out Twitter’s Community Forums.

Top comments (6)

Collapse
 
bauripalash profile image
Palash Bauri πŸ‘»

The Title was really confusing , but still Awesome Work! β™₯️

Collapse
 
oihamza profile image
Hamza

Thank you! What did you find confusing about the title? πŸ€”

Collapse
 
bauripalash profile image
Palash Bauri πŸ‘»

I thought it was about building some app πŸ˜…

Thread Thread
 
oihamza profile image
Hamza

Ah, I see. It's an application on Glitch, but a bot on Twitter. I know it sounds a little confusing.😝

Collapse
 
jessicagarson profile image
Jessica Garson

This is so awesome! The NASA image of the day API has long been one of my favorite ones!

Collapse
 
oihamza profile image
Hamza

Mine, too! ❀️