Tutorial: Node on Raspberry Pi (for Bots)


Almost all my bots have been written in Python, but I’ve been meaning to try Node.js for more interactive bots for some time. Daniel Shiffman’s excellent new tutorials were enough to get my jump-started, and I created @BotSuggestion, a bot whose only activity is following accounts suggested by Twitter, slowly conforming to their algorithm.

I run all my bots on a Raspberry Pi under my desk (see my tutorial on how to get that set up), but getting an ongoing Node server running took a little more work.

First, we have to download and install Node:

To test if Node and npm (Node’s package manager) was properly installed, use the “version” command.

If you get a version number, you’re good!

This may not be the most up-to-date version, so we update from using npm. Warning: this is a little dangerous, so don’t use this method to update once you’ve got your bots running.

This may take a while, so be patient. When it’s done, run the tests from the previous step again to make sure everything is ok.

Write your bot in Javascript. A few suggestions for developing bots with Node:

  • I’m using the Twit package to handle connecting with Twitter via OAuth; it can be installed with npm.
  • Test locally on your machine, which makes it easier to find problems.
  • Use full paths whenever possible – these will have to be updated when you deploy to your Pi.

Now that you have a bot, move its files to your Pi. Log in using SSH, so we can run some commands. Once everything is set up, we need two paths: where your bot is located and where Node is installed.

To get your bot’s location, use cd  to move into that directory, the use this command to get the full path:

To find where Node is installed, just type:

Note down both the paths. Then, we add a new line to the super-user’s crontab file:

Add this line, using your paths from earlier:

Reboot your Pi from SSH using the command sudo reboot . When it comes on, your Pi should start the Node server and run your bot. Check that something happens in its feed – if so, you should be all ready!

Did you find this post helpful or interesting? A small donation goes a long way towards helping produce this content. If you can't, please share what you've done with others!
Donate via Flattr

7 Replies to “Tutorial: Node on Raspberry Pi (for Bots)”

  1. Hi Jeff – Is there a specific type of Raspberry Pi you recommend for this project? I’m a beginner in Node.js but I’ve got a Twitter bot ready to go and would love to try out your tutorial. (And maybe add on a breadboard and LED that flashes when my bot tweets something–any resources on that?)

  2. Any of the Pis should work, though the Zero might be a bit challenging. If you want to interface with electronics, I’d suggest looking at the Adafruit site – they have lots of great tutorials. If you do make a bot, post it here!

  3. Not quite sure what you mean? I usually use SSH to send commands, but I use SFTP to send files (like you would to a web server) – I have a tutorial on that here. But, if you want to see what files are in a folder, move there using the cd command, then type ls to list the files.

Leave a Reply

Your email address will not be published. Required fields are marked *