Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 20, 2019 05:54 pm GMT

Build a Twitter NASA app

This project is hosted on Glitch: https://glitch.com/~daily-nasa

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 its a photo, our app will download the image and Tweet it. If its 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. Heres an example.
    • Image - the app will save the image to a temporary directory and Tweet this via the Twitter statuses/update endpoint. Heres 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 Twitters Community Forums.


Original Link: https://dev.to/twitterdev/build-a-twitter-nasa-app-57kb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To