By Eliza , a creative dev from Paris, France.
My first encounter with Twitter bots happened last year via Python and involved my building a bot that would receive tweets with a specific hashtag and photo attached, apply an instagram-like filter to it, and retweet the user with the new photo. It was a fun first pass at getting my hands dirty with Twitter’s streaming API (which of all it’s API’s might actually be the least obvious one to use… I thought I was struggling because #n00b, etc. but a number of more experienced coders have since reassured me that it’s not just me. Wee!)
For my second pass at bot-building things went a little differently. Firstly, when it came to deciding on the botiness of the bot itself, I opted for language processing over image processing. This was for no other reason than I’ve been looking for an excuse to build something with ELIZA, one of the earliest known natural language processing computer programs created (‘64) (and also my name)
Funnily enough I was introduced to ELIZA in my teens when a computer engineer friend of mine pointed out that much like the software version, irl Eliza was a good listener too.
While I’ll totally take the compliment, I much prefer the story behind the program itself: ELIZA was developed by J. Weizenbaum in part to show that communication between man and machine could only ever be superficial in nature. Turns out he wasn’t as right as he thought because a large enough number of users attributed human-like feelings to the program with some, (his secretary included,) becoming convinced the program was some sort of AI. I find the whole idea pretty captivating - if you do too, follow this link.
ELIZA was pretty simple to set up. NPM offers a number of different takes on Weizenbaum’s chatbot, but I foundElizanode to be the most straightforward to apply.
I built the bot to interact with users based on how you interact with it: on follow or first @, the bot’s introductory interactions are triggered (via Eliza.getInitial()) and if you reply to the bot, and therefore have a running thread, Elizanode will analyse and respond to your tweet accordingly.
Simple enough… but wait! How do you achieve threaded responses? It’s a pretty important question if you want to avoid Twitter shutting down your bot for starting too many conversations or seeming like it’s tweeting people out of the blue.
As you might have guessed it comes down to twitter message id’s. But if you look at the JSON Twitter gives you, things are not necessarily so obvious. Case in point:
Right off the bat, we can write off the id’s and everything else provided under user, because it refers to the bot itself and we definitely want to avoid replying to ourselves. What we’re looking for is the id of the tweet that we are replying to, so that our interactions are stacked, or threaded.
But still, at this point my bot was replying on loop, every second. Which is definitely not what I wanted. I had achieved the threaded reply’s I was looking for but my bot would continuously tweet a response until everything crashed because there was a duplicate status.
By using in_reply_to_user_id_str I’d created a reply loop that had the bot continuously tweeting at and therefore soliciting itself. In fact, it’s the id_str way up at the top of the JSON file that I needed:
The amount of roadblocks you’ll hit is pretty dependant on how boty you make your Twitter bot. In my case, this was the only hiccup I encountered! My bot is pretty simple, it’s true, but most Twitter bots are. They’re fun and simple living projects to take on, especially if you’re curious about how to use data to power your projects.
Welcome! My name is LaToya and I'm a full stack developer, speaker, and the founder of SheNomads. I'm building an inclusive community around tech, travel, and remote work. Let's get to know each other!
1. 5 Tools Every Developer Needs to Remote Pair
2. Why I Quit My Job, Sold All My Stuff, Moved Out of My NYC Apartment, and Became a Digital Nomad
3. No, You Can't Pick My Brain
4. 5 Ways to Become a Strong Junior Developer
5. 24 Hours with a Digital Nomad: Maia Hariton