Looking for a way to make Twitter more useful, if only for other people? One way is to create an automated Twitter bot that tweets images with useful descriptions. You could do this manually… or you could build it with Node.js and host it on a Raspberry Pi. Read on to find out how.

Why Build a Twitter Bot?

If you’ve ever been on Twitter and seen accounts that post photos, or facts, or cartoons, etc., then it’s overwhelmingly likely that these are automated. It’s a great way to build an audience of people interested in the same topic.

Get Started: Build Your Database

If you want to build a photo tweeting bot, you’ll need to start by collecting the images you want to share. These should either be your own images, or ones you’ve acquired under a Creative Commons or some other open source license.

You should also keep note of attribution and other information that you want to go with the images. We’ll come back to this information later, once the bot is up and running.

Follow the on-screen prompt, and wait while your Pi updates. Once you’re done, reboot with

sudo reboot

When you’re done, use curl to download Node.js:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

Next, install it with

sudo apt-get install -y nodejs

When all is done, run a check to ensure the software was installed correctly. The easiest is to check for the version number:

node -v

The response should be something like v8.11.3 (or higher). If you see something like that, you can be confident that Node.js is ready to use.

Build Your Twitter Bot

The next stage is to input the code that will create the bot. Begin by creating a directory:

mkdir twitterbot

Then, change it to the new directory for your bot:

cd twitterbot

Here, create a file called server.js

sudo nano server.js

In this file, input a single line:

console.log('I am a Twitter bot!');

Press Ctrl + X to save and exit, then run the script:

node '/home/pi/twitterbot/server.js'

This should return the phrase “I am a Twitter bot!”. Now that you’ve confirmed this is working, it’s time to install the Twit library with npm (the Node Package Manager). Ensure this is installed by entering:

npm -v

Again, you should see a version number displayed.

Follow this with:

npm init

This begins by prompting you for information about the Node.js app you’re creating. Default options are displayed (like this) so you can just tap Enter to accept them. You can choose to input your own details too. Once this configuration is over, you’ll be asked to confirm the details with “yes”.

The next stage is to install the Twit module from the npm.

npm install twit --save

Wait while the files download into the node_modules subdirectory. Once that’s done, open the server.js file again in nano.

Create a Twitter App

To build a working Twitter bot, you’ll need to create a Twitter app. This is a simple process, which requires you to first sign up for a new Twitter account. Note that this also requires a phone number to verify the account, and once this is done, head to developer.twitter.com to create the app.

If you don’t have a developer account, this may take some time, as there is a certain amount of form filling needed. This is a step Twitter has taken to avoid spam, so take your time and answer the questions accurately.

Click Create an App, and add the details as requested. At the time of writing, the developer system is undergoing an overhaul, so you may have to wait a few days (and answer some additional questions).

Next, switch to the Keys and Tokens tab, and under Permissions find the Access permission and ensure it is set to Read and Write (use Edit if not). Then switch to Keys and Tokens and make a note of the following:

Consumer Key

Consumer Secret

Under Access token, click Create to generate:

Access Token

Access Token Secret

These are the API keys which you’ll need for giving the bot access to your Twitter account.

This function picks an image at random from the images folder, and once selected is uploaded to Twitter using the media/upload API.

Next, add the following code. This will find the images directory, and take an image from it, posting one at random every 60 seconds. You can (and should) edit this timing, which is represented in the code with the figure 60000. Longer gaps are advisable.

A single use of the node server.js command will then prompt the photos to begin tweeting! (Should you need to end the posts, press Ctrl + Z to cancel the server.js script.)

Adding Text to Your Photo Tweets

If you need to add text to your images, this can be done using an array. The array will refer to the filenames of the images and list the text that should be added. For instance, you might add attribution to images that you didn’t take. Or you might add some facts or a quote.

Begin by creating images.js

sudo nano images.js

Here, add the following code. This is an array, with two elements, file, and source. These hold the file name of the image, and the attribution (typically a URL).

Christian Cawley is a Deputy Editor at MakeUseOf, covering security, Linux, DIY and programming, with extensive experience in desktop and software support. Christian is a regular contributor to Linux User & Developer magazine, as well as specials including Raspberry Pi for Beginners, and Raspberry Pi for Kids. He's a Raspberry Pi tinkerer, Android user, podcaster and foodie.