Building a twitter bot with node-red, with an added micro:bit to boot.

Bloggedin
6 min readAug 11, 2020

Bit of fun this week with the twitter API, node-red on a raspberry pi and a serial connected micro:bit. This week we are making a twitter bot but cunningly introducing a couple of unnecessary tech hacks along the way.

Project Outline.

Run and host node-red on a raspberry pi, which will connect to our twitter API looking for a hashtag of #rollmydice. Upon receiving the hashtag we will send a request to a serial connected micro:bit which will randomise our dice roll and return it to node-red. Node-red will then build the tweet and send the dice roll reply.

Essentially we are using the micro:bit as a very loose example of edge computing and if you just want to build a twitter bot this part is completely unnecessary.

First up on the Raspberry pi we need to fire up node-red with node-red-start (see my other articles if you don’t have node-red installed). We will need to install the twitter nodes to the palette, from the menu in node-red click manage palette and switch the tab to install. In the search field type twitter, you are looking for node-red-node-twitter click install.

This will add two nodes to the palette twitter in and twitter out. To use twitter you first need to create an API by signing up for a developer account from twitter (this doesn’t cost money but can take time for you to get authorised).

Once you create an app on the twitter developer site you can view the generated API access tokens necessary for configuring your twitter nodes.

Node-RED Flow

Let’s have a look at the full flow and then go through the details.

As mentioned earlier the micro:bit part is not completely necessary you can build your full bot purely using node-red and the code in this article should clearly point this out, however we love the micro:bit and it also adds two dimensions to the project, firstly it covers serial connections in node-red which is something we haven’t covered yet. Secondly we are offloading the dice roll to the micro:bit sure this is purely a random number generated but using computer science theory we are processing our calculations on an IoT device. In the world of buzz words this is a very simple example of edge computing (you are not going to win any awards here) as the processing is offset to another “computer”. It is not completely difficult to envisage employing this methodology to an IoT device gathering sensor data and performing calculations before returning to our node-red hub.

Caveats aside (hopefully dodging the computer scientists here) let us take a look at the micro:bit coding.

A tiny bit of MakeCode

As you can see very basic coding but it does introduce using serial over USB basically the serial port on the micro:bit is listening for a symbol sent to it, we have used the #. This will then trigger the microbit to send back a random number from 1–6. Flash this to the micro:bit and leave it connected via USB to the raspberry pi.

Back to Node-RED twitter in

Going through the flow everything starts with the twitter in node. If we double click it we have the following…

Clicking the pencil next to our twitter ID allows us to fill out all the details supplied from the API created at developer.twitter.com.

We have set the search field to all public tweets (any instance of our hashtag we will search for). In the for field we have set a #rollmydice hashtag (you can add anything you like here to trigger your bot).

The flow now takes two pathways, we will follow the micro:bit request pathway first which starts with a function from our tweet in called trigger micro:bit for dice roll.

The javascript for this function is minimal we simply assign the msg.payload to a #

msg.payload = ‘#’;

return msg;

This is passed to a serial out node which is configure as follows:

Serial nodes

So what is all this /dev/ttyACM0 well this is the path to the USB connection to the micro:bit on the Raspberry Pi you can usually get this path using dmesg in a terminal.

The port speed is set at 115200 and everything else is left as default.

It makes sense at this stage to pick up the flow that listens for the micro:bit response via the serial in node.

Again apart from adding the serial port config and changing the baud everything else is left as default here.

The microbit returns a string of the random number, we need to capture this with a function node.

In the code you can see that we capture our dice roll (with some descriptive text) into a flow variable. This will be used in the other branch from our tweet in node which also starts with a function called get tweet details.

The payload from the tweet in has additional information we can use, we can capture the user who sent the tweet for instance which is handy if we want to send them a tweet back (note the name variable does not show an underscore in node-red, it should read tweet.user.screen_name).

So in the function we capture the user and build up a part of our reply and pass this to the payload, since the flow path is dependant on data returned from the micro:bit we have added a delay to the flow of 5 seconds.

After the delay we have another function called compose tweet which consists of the following code:

Building the tweet

The payload from the feeding function is assigned as tweetpart1, tweetpart2 picks up the flow variable we set earlier from the micro:bit response over serial. This function simply combines the two messages to form our tweet as a new payload.

This payload is passed to the tweet out node which concludes the flow.

Once deployed anyone tweeting with the hashtag #rollmydice will trigger the bot and get a response generated from our Raspberry pi hooked up to the micro:bit.

As mentioned it is not too complicated to reverse this project into a pure node-red twitter bot if you wish but the additional features add fun and possibilities to other projects you may have in mind, such as tweeting sensor data from various locations etc.

Thanks for blogging in…

if you like this article there are many more on www.bloggedin.co.uk

--

--

Bloggedin

Author of https://www.bloggedin.co.uk, Coding Python, Raspberry Pi, micro:bit, IoT and Node-RED.