Skip to main content
shopping_basket Basket 0
Login

TwitterBots are us (How to tweet from Node-RED)

DrLucyRogers
0
Twitter bots - or automated tweets - have got themselves a bad name - mainly from spamming people. However, there are some that are useful. This project shows how to make your own Twitter bot using a Raspberry Pi and Node-RED.

Parts list

Qty Product Part number
1 OKdo Raspberry Pi 4 B Starter Kit 4 GB 202-0644

Twitter bots - or automated tweets - have got themselves a bad name - however, there are some that are useful and not spam. Twitter actually encourages the useful ones - for example, I use @UKmfg to automatically retweet anything about #UKmfg - or manufacturing in the UK. Although I have to put a few measures in to make sure that swear words or hijacked hashtags are not automatically retweeted.

For the rules on automation from Twitter see hereMany thanks to @AndyPiper for his help on getting this to work!

In this project, I will share how to make a Twitter bot using a Raspberry Pi and Node-RED software.

This used to be very simple - now there are more hoops to jump.

NOTE: This is working as of 17 November 2020 - but be aware of updates to Twitter which may break it.

Preliminaries 1

1) Get a developer account on Twitter and find the keys and tokens.

The instructions and information to do this can be found here: https://developer.twitter.com/en/apply-for-access

Note: You only need one developer account, and, as long as you have the correct permissions, you can then use this to get the keys and tokens so you can tweet from any other account.

2) Go to the developer portal https://developer.twitter.com/en/portal/dashboard 

Add an app (bottom of overview tab) and then record the API key, API Secret Key, and Bearer token somewhere safe. Copy and paste it - do not manual transcribe them.

Then click "App Settings" at the bottom of the page.

3) Set the following:

App permisions: Read and write

Enable 3-legged OAuth

Callback URL - any website address here (I use https://twitter.com)

Website URL - any website here (https://twitter.com

4) Keys and Tokens

At the top of the page, click the "Keys and Tokens" tab.

You should have already saved the API key, API Secret Key, and Bearer token.

Generate the "Access Token and Secret" and copy these to your safe place.

If you want to tweet from your "Twitter Developer Account" then these are the keys and tokens you need. However, if you want to tweet from a different account, you need to jump through these further hoops:

[Note: you can also get these keys using GO - details here https://github.com/smaeda-ks/tw-oob-oauth-cli]

This doesn't have to be done on the Pi, but if you are using it, you first need to intall RubyGems by using the code 

sudo apt install ruby-full

More details here: https://www.raspberrypi.org/documentation/linux/software/ruby.md 

Then install Twurl - by using the code

sudo gem install twurl

info is here: https://developer.twitter.com/en/docs/tutorials/using-twurl

Then type the code 

twurl authorize --consumer-key ABC123 --consumer-secret ZYX789

Where ABC123 is the API key you saved earlier (I don't know why it's called something different)

and ZYX789 is the API secret key

Follow the onscreen instructions, and go to the website https://api.twitter etc that it lists. (Make sure you are either logged out of twitter or logged in to the account you want to tweet from). Enter the supplied Pin in to the terminal. 

Congratulations. You have now generated the access keys and tokens! But now to find them ...

5) Finding the keys and tokens!

Now you need to find the .twurlrc file.

. files are hidden, so you need to show hidden files on the Pi, this is in File Manager, View tab and Tick the "Show Hidden"

The .twurlrc file will be in the pi directory.

It should be in your home/USER directory on Mac, and c:\Users\USERNAME\APPData\Local\ directory on a PC.

Open the file and there are the consumer_key and the consumer_secret! Copy and paste those somewhere safe.

Preliminaries 2

1) Install the latest Raspberry Pi operating system on to your Pi

Details here: https://www.raspberrypi.org/software/ (For this project I am using a Raspberry Pi 4 and Raspbian Pi OS (32 bit) 2020-08-20). Follow the onscreen instructions to update and complete the set-up.

2) Install Node-RED

Note: DO NOT USE THE 'RECOMMENDED PROGRAMS' link on the Pi menu, instead go to the "Running on a Pi" section at https://nodered.org/docs/getting-started/raspberrypi and follow the instructions there - use the code: 

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

And say "Yes" to installing the Pi Nodes.

Wait a long time

Once it is installed run the code:

sudo systemctl enable nodered.service

This will make Node-RED automatically start every time the Pi boots up.

You can then go to a browser and type in localhost:1880 to access Node-RED

3) Install the Twitter Node on Node-RED

Click on the Hamburger Menu (three horizontal lines) in the top right of the sheet

Select Manage Palette and click on the "Install" tab.

Search for "node-red-node-twitter" and click install

The twitter input and output nodes will now be in the left hand column in the "Social" area.

Start writing the flow

Connect an inject node to a twitter out node.

Change the Inject node msg.payload = to "Hello World"

Double click on the Twitter out node.

Click the pen icon next to "add new twitter-credentials ..."

Complete the properties:

Twitter ID: The twitter account you want to tweet from 

API Key: consumer_key from the .twurl file

API Secret Key: consumer_secret

Access Token: token from the .twurl file

Access Token secret: secret from the .twurl file

Save and deploy!

Now click the inject node and your tweet should go live.

(Note, you can't duplicate the same tweet, so it may be worth putting the timestamp in for testing purposes!)

Now it is working with an inject node, you have the basis for using the twitter node in any flow. The set up is the same for a twitter input node.

Happy tweeting!

I am an inventor, engineer, writer and presenter. Other stuff: Royal Academy of Engineering Visiting Professor of Engineering: Creativity and Communication at Brunel University London; Fellow of the Institution of Mechanical Engineers and have a PhD in bubbles; Judge on BBC Robot Wars.

Related Content

DesignSpark Electrical Logolinkedin