You too can make a Node-based Twitter bot, right now
The students’ eyes opened wide in a mix of elation and evil-mad-scientist.
Lines of code projected at the front of the class had just done something in the real world: They sent a tweet. And you could see it, right there on the internet. The power of this little exercise was crystal clear to the undergraduates. And they couldn’t hide their giddiness.
“Use this only for good,” I admonished.
They had followed along as I built basic Twitter bot. You can do it, too.
The class lesson, at the New School Journalism + Design program, was a slimmed-down version of the experience—and thrill—I had building @HiWeatherbot. Tweet a city at @HiWeatherbot and you (should) get a forecast back.
I was inspired to build the bot after Melody Kramer showed me @BBCWeatherBot, which dutifully replies with a forecast if you send it a city and a day. But it works only for UK locations. Could I make one for the US?
I decided to try.
Naming the Baby
To tweet, I needed a new account.
It’s probably unwise to experiment on your main Twitter account, since it’s possible to accidentally run afoul of Twitter’s posting rules (which I did) and get your account blocked. So I set up @HiWeatherbot (@Weatherbot was taken.)
There’s also something I needed from this account: a collection of keys and access tokens. I got those by signing into Twitter as @HiWeatherbot, going to the Twitter apps page and registering a new app. To make it work, the app had to have both “read” and “write” access, too.
Bot Duties, Defined
The steps I wanted this bot to perform were:
- Wait patiently until someone sends a tweet containing @HiWeatherbot
- When someone does that, distinguish between a tweet directed at it and a mere mention
- If it’s directed at the bot, grab the rest of the tweet and try to geocode it
- Take the resulting latitude and longitude and send it to the Forecast.io API
- Use the resulting forecast data to compose a pithy tweet
- Reply to the original sender
Under the Hood
Veltman writes code in node.js like our reporters write on-air scripts—quickly, concisely, and effectively. I’ve been trying to learn more node, so that’s what I used.
As with many coding languages, wonderful programmer people have already written components to do tasks you might have. Like, say, navigate the complex authentication handshake with Twitter. In node, one installs such modules with a package manager.
To communicate with Twitter, I used a module called Twit. It not only handles the tweeting part, but will “listen” for certain mentions in the Twitterverse like “@HiWeatherbot” (or, of course, “iPhone”) and springs into action if they are detected. That was a new concept for me, and seemed like some kind of magic.
Twit also handles the complicated Twitter authentication, using my keys and access tokens. Those can be hard-coded into the program, but I keep them separated.
Turns out that if someone directs a tweet at you, the “reply to user” data from Twitter gets filled with your user number — even if the tweet is not not actually in reply to anything. That’s how I distinguish between a mention and a directed tweet. (I also have the bot tweet at me if someone mentions @HiWeatherbot.)
Having been tweeted at, the code assumes the rest of the tweet is a location name (not the safest of assumptions, but this is an experiment), and turns that text into a latitude and longitude with the Geocoder node module.
Coordinates in hand, it then makes an API request for that point on the planet to Forecast.io, using the Request node module, and gets a whole lot of cool forecast data in return.
Plucking out just the key parts, the program assembles a little tweet and sends it as a reply to the original sender.
Run, Bot, Run
The bot runs wonderfully on my laptop, with the simple command:
But I like to close my laptop on occasion, so I needed a place for it to run 24/7. That place is an Amazon EC2 virtual computer in The Cloud. You, too, can make a server in the cloud. Here’s how I teach undergraduates to set up their own.
I keep my weatherbot running using one more node module called Forever. Just make sure you don’t run your bot on both your laptop and your server at the same time. That’s a Twitter no-no, and may be why my bot was banned for a bit(!)
Someone noted that I should provide temperatures in both Celsius and Fahrenheit, so I added a code to turn °F to °C if the forecast location is not the United States, the Bahamas, Belize, the Cayman Islands, Palau, American Samoa or the U.S. Virgin Islands.
I also wanted to provide today’s forecast if it’s before noon, tomorrow’s if it’s after noon. So I did a little temporal math using the Moment.js module.
The basics of this project also led me to build a home weatherbot with a display, which my family uses every morning to plan our day.
John Keefe is the Senior Editor on the Data News Team at WNYC, which helps infuse the station’s journalism with data reporting, maps and sensor projects. Previously Keefe was WNYC’s news director for nine years. He’s on the board of the Online News Association and is an advisor to CensusReporter.org. Keefe tweets at @jkeefe and blogs at johnkeefe.net and datanews.wnyc.org.