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.
1. INSTALL NODE
First, we have to download and install Node:
1 2 3 |
wget https://nodejs.org/download/release/v0.10.0/node-v0.10.0-linux-arm-pi.tar.gz cd /usr/local sudo tar xzvf ~/node-v0.10.0-linux-arm-pi.tar.gz --strip=1 |
2. TEST THAT IT WORKS
To test if Node and npm (Node’s package manager) was properly installed, use the “version” command.
1 2 |
node -v npm -v |
If you get a version number, you’re good!
3. UPDATE NODE
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.
1 2 3 |
sudo npm cache clean -f sudo npm install -g n sudo n stable |
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.
4. MAKE YOUR BOT!
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.
5. SET UP YOUR SERVER
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:
1 |
pwd |
To find where Node is installed, just type:
1 |
which node |
Note down both the paths. Then, we add a new line to the super-user’s crontab file:
1 |
sudo crontab -e |
Add this line, using your paths from earlier:
1 |
@reboot sudo /usr/local/bin/node /home/pi/Bots/SuggestionBot/bot.js &>/dev/null |
6. TEST YOUR BOT!
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!
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?)
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!
Thanks Jeff! I’ll post a link when I make it.
Hi Jeff, Another question: how do I confirm the file transfer via SSH?
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 typels
to list the files.Of course cd and ls and just look. Duh. Sorry.
That’s ok! These tutorials are for people new to stuff like the command line!