DEV Community

Cover image for Twitter Bot which tweets Mars Photos
Ashish Mishra
Ashish Mishra

Posted on • Originally published at blog.thecolorfulslate.com on

Twitter Bot which tweets Mars Photos

Hi! In this article, we will build a simple Twitter bot with Python, which will use NASA Open source API to tweet Mars Image every 1 hour. I have built this Bot and hosted it on Heroku and it's online for the past few months. You can view it here Mars Image Bot. So, follow along, as I detail below the entire process.

Step 1: Getting Credentials from NASA Open API

As we are going to pull our Images from NASA Open source API, which in turn gets these Images from Mars Rover. So, let's first visit https://api.nasa.gov/, Here we have to enter few details, and it will give us an API Key to access its APIs.

image.png

After entering all details, you should see the below screen, where API Key will be shown, please note this carefully, we will need it.

image.png

Now, from the menu, select Browse APIs and enter Mars in the search box, you should see all mars related APIs.

image.png

Now, we are going to use the below API, which sends us random Image Data every time we change the SOL number.

https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=12&api\_key=DEMO\_KEY

Replace the DEMO_KEY with your API Key.

This Endpoint returns a Photos Array, which contains all information. we will be traversing it and finding the Image Endpoint.

image.png

Okay, So we have all the info which we needed from NASA.

Step 2: Code our Python Bot

We will first get the Twitter API keys, for that go to this URL Developer Portal and follow the process, which is self-explanatory. In the end, you should have your Access Key and Consumer Key. (Do let me know in the comments if you face any issues.)

Let's start coding. 😈

First Lets import the tweepy and other libraries, and set the Twitter keys in a variable. I have used config variables, as I am going to deploy it to a Server Later on, you can directly assign the values, for local development.

image.png

Now, try to make a connection with Twitter, If all goes well you should see the "Connection Done!" message, in the terminal.

image.png

Now, as we are connected to Twitter, let's try to pull the Image from NASA API, which we are going to use. First, we are getting the current date and time, and printing it out. Next, we are trying to fetch a random Integer, if you remember in NASA API, we need to pass a random SOL. So, we will add this number in the API Url, and make a get request. If the request code is 200, we will start reading the response, or else will print an error message.

image.png

As our Image URL is in the photos array, we will save this array in photos_array, and as you can see in the response, there are multiple images object in the array, so let's pick a random object from the array by again using the random library. Once we found the Image, pass the message and image to another function, from where we will actually update the Twitter status.

image.png

This function is very simple, it makes a web call to the Image URL and then pulls it. Further, it uses the below method of tweepy library to update Twitter status.

api.update_with_media(filename, status=message)

Enter fullscreen mode Exit fullscreen mode

Now, if you have followed all these steps and call the get_random_image_from_nasa function, you should be able to tweet an image, but let's schedule it to run every one hour.

image.png

Here, we are using, schedule library, to run the above function every hour.

Congrats!, now your bot is working.

Let me know if you want me to write about how to host this bot to Heroku and run free for life. 🐶

Github Repo URL - https://github.com/a-blank-slate/mars-image-tweeting-bot/blob/main/tweet.py

Thanks! for reading this.

Latest comments (0)