Building a twitter bot that posts pictures of cats in black and white with Node.js and unsplash API

Photo by Lucija Ros on Unsplash

So in this tutorial, we are going to build a twitter bot that gets pictures of cats from unsplash in black and white and posts them on twitter with Nodejs. Before we start you should be familiar with

  • what a bot is
  • what an API is
  • and of course nodejs

So it would be helpful to know what these are before proceeding. If you have also used any API before that would be a plus. We would be using the Unsplash and Twitter API.

Let’s Start

Photo by Bruno Nascimento on Unsplash

The unsplash API

So firstly you’ll head over to unsplash to register as a developer

Once you have registered as a developer, you then go over to create a new application. There’s quite a lot of form filling involved.

Once you’re done, you should be given the access key and secret key for the new application you just created.

Now we have registered as a developer and created an application with unsplash, let’s go over and do the same with twitter.

The Twitter API

Firstly, create a separate twitter account for your bot, except you want it to run on your personal twitter account.

Secondly, you need to apply for a developer account to twitter to use their API if you don’t have one.

This might take some time to get approved. You’ll also need to check the mail you used to apply regularly as twitter can send emails for verification.

Once your developer account gets approved, you can then proceed to creating an application.

https://developer.twitter.com/en/apps/create

This step also involves a bit of form filling. Once you’re done you should have the following for your newly created application.

consumer_key,
consumer_secret,
access_token_key,
access_token_secret

Using the unsplash API

We’ll be using an npm package called axios for our api requests

  1. Install axios
npm install axios

2. You’ll then need to require it in your app.js file (entry-point file to our application)

/app.js

 const axios = require(‘axios’);

3. We need to set our authorization header on axios with our unsplash client id. We also want to set it globally, so each request we make with axios will be made with this authorization header.

/app.js

...
axios.defaults.headers.common['Accept-Version'] = 'v1';//just add it
axios.defaults.headers.common[‘Authorization’] = ‘Client-ID <your client id goes here>’//something like this// axios.defaults.headers.common[‘Authorization’] = 'Client-ID blah'...

4. Next we’ll be making requests to the unsplash random photo endpoint using axios to get a random photo. We are adding the ‘cat’ query parameter to narrow it down to just random cats as supposed to random photos.

5. Then we’ll have to download the image after getting our response from our API call.

After we get our response, we save it in a photo variable on line 16. In our response, we get various links to the image we want in different formats but the one that’s of interest to us is

response.data.urls.raw

This is the raw link to the image we want to download. The good thing about this link is that we can append query parameters to it to modify the properties of the image we are downloading. If you notice on line 18 we are adding

&q=40&sat=-100&fit=crop

to the url before downloading the image, sat=-100 is what makes the image black and white. It sets the saturation of the image to -100. We now have our black and white cat image in /file.jpg.

If you notice, we are returning a promise on line 17 because we are intending to chain our twitter API call to it.

Using the twitter API

Now we have the image, it’s time to upload it to twitter. We’ll be using this nodejs wrapper for the twitter API.

npm install twitter

/app.js

...const Twitter = require(‘twitter’);const config = {
consumer_key: 'your consumer key',
consumer_secret: 'your consumer secret',
access_token_key: 'your access token',
access_token_secret: 'your token secret'
};
const T = new Twitter(config);...

It’s time to chain our twitter API requests to unsplash’s API request

Note: these are only snippets and not the entire code and cannot be run independently

On line 8 we are retrieving(reading) the image we downloaded from unsplash.

On line 11 we are returning a promise to upload the image to twitter. Once that promise is resolved, we get a media id for the image we just uploaded which is now used to post a tweet with an image.

On line 19 we decide what we want to tweet with the image. Here we are giving credit to the photographers who took the pictures.

On line 20 we pass the media id for the image we uploaded to twitter.

On line 22 we are returning a promise to post the tweet, once that promise is resolved, the result is logged to the console on line 25.

Our Final Product

This is a bot that posts cat-related pictures from unsplash in black and white every 1 hour by wrapping the code we went through in this tutorial in a function and calling it with JavaScript's setInterval every 1 hour. Hope this tutorial helps.

I know not what I am yet.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store