Everything I learnt about running Twitter bots (2018 edition)

25 November, 2017 7 minutes read python

For the past 10 years, I've been fortunate to be involved in making software that was adopted mostly by teenagers and fell in love with the demographic. Born into the hi-speed wifi era, teenagers are using the digital ecosystem the amplify and empower their relationships and social circles. Sometimes though, this ecosystem, especially around social networks, can bring out feelings of loneliness and being excluded from a community.

What's all that has to do with bots?

Around 7 months ago I went on a mission to use code to spread positivity and created CheerBot. CheerBot is a bot that lives on Twitter with one goal on its mind: Send positive messages to people that needs them the most. The thought process behind CheerBot is a simple one: Positive messages can come a long way, especially coming for our friends which we often times take for granted. Getting an occasional "you are loved" or "one of your friends wants you to know they have your back" at the right time proved to be transformational and the reactions to CheerBot so far are truly amazing.

Building CheerBot exposed me to the Twitter API for the first time and gathered notes along the way. I went from not knowing what I'm doing and getting blocked by Twitter to going on vacation and keeping CheerBot running on 90% uptime (as long as I have internet, CheerBot is running). Here's my story.

The early days: Getting blocked by Twitter, a lot (how not to run a Twitter bot)

In the early days, the core flow for CheerBot was:

  • A user sends a private message (DM) to CheerBot with the syntax: Cheer @friend
  • CheerBot will tweet a random positive message at the friend "Hey @friend, one of your friends wants you know that you're not alone".

What a rollercoaster it was. People responded super well to the concept and sending roughly 30 cheers per hour. Their friends got exposed to it, loved it and wanted to cheer other friends. Positivity was spreading!

The problem was I couldn't keep CheerBot up for more than a couple of hours straight. The "your application was restricted from performing write actions" (basically messaging people) screen happened a lot and users were disappointed when it wasn't consistently working and I was scrambling for a solution. What's worse is that as I was trying to figure out the issue the and tweak my code accordingly, the restriction happened more often, from hours to half-hours to minutes.

My first reaction was "CheerBot is a positive project for the community, I should get a free pass!". I went through a full appeal process with Twitter's safety team, was re-granted access but got restricted shortly after, went through a discussion in the official forums - they won't respond to individual cases. Was looking into Twitter's automation rules - kind of ambiguous. I was feeling skeptical about CheerBot's future at this point. I even tried couple of moonshots like getting in touch with the Twitter community projects team and Twitters's sales team to get some insight on how big players are getting a pass doing similar things (hint: 💰).

Then I actually got a hold of a nice person from Twitter's support who couldn't tell me much about how to go around the restriction rules but said the following: "CheerBot is a great positive project and I would hate to see it go. You have to understand that for each great project like yours we have hundreds of spammers and malicious projects. You're super driven, you'll find a way".

My path to 90% uptime, how to avoid restrictions

The following months after were a crazy research frenzy on how Twitter's spam protection actually works and what can I do to keep CheerBot's core functionality intact while not going into Twitter's red zone. Some of the information is available online, some I got from Twitter agents I worked with in the past (obviously they couldn't spill the beans, but were helpful) and the majority of it I got though tons of trial and error and deduction.

Over the past couple years Twitter has done some major leaps in detecting automated patterns and bot behavior, most of it through machine learning and to an extent it's a cat and mouse game between the bad people who use bots for phishing and scam and Twitter's security algorithms. The more a bot adopts patterns and usage habits that fits a human user, the more difficult is it to block it. In addition, automated services push tweets through the API that has its own rules on connections, how often can you tweet and who are you tweeting at in relation to the user that initiated the request.

After a ton of research, there are couple of flow limitations that either flagged CheerBot or triggered a restriction:

  • You don't want to send unsolicited @ mentions: Even though a user sent CheerBot a message and asked to cheer another person, that doesn't mean the other person gave his consent to receive a mention. Yes, as a user you can spam @ mentions to whoever you want, as an automated program this will get you flagged.

  • The ratio of @ mentions to other stuff matters: Looking at the bot account globally you want to have a reasonable ratio of @ mentions to other type of tweets. When CheerBot was all about the @'s, even though it had all the right rules in place to be user friendly, in ended up getting restricted every couple of days. So through trial and error I figured that diversity matters.

  • Don't do the obvious bad stuff: make sure people on the receiving and of tweets aren't being spammed, make sure links are appended correctly and if possible use Twitter cards if you're hosting the links since content of direct messages and tweets is also monitored.

  • Encourage positive interaction: Building credibility of CheerBot as an account was one of the things I believe is keeping it on the safe side of things. It was kind of easy for CheerBot since reactions were really positive and people are happy to engage with it (no brag! well just a bit) but overall, people liking, responding and on the backside not reporting the tweets made by the bot is what is giving it critical mass in being considered a positive force in Twitter's ecosystem.

Here's my current working model for CheerBot

  • CheerBot is working in 30 minute timeframes and has several spam checks.

  • When you send CheerBot a private message (DM) and cheer a friend, it'll first check if you're sending multiple cheers to the same friend over a short period of time (if so, you're spamming cheers and CheerBot will ignore that).

  • If the user level spam check clears, CheerBot will check how many cheers were sent overall during the 30 minute timeframe. if those are busy 30 minutes, CheerBot will apologise and tell you to try in a little while. I'm working on a queue system to save the cheer for when it's safe so the user isn't bothered. The thought process here is to avoid sending multiple @ tweets over a short period of time.

  • When both personal and global checks are clear, I'm looking into the user's relationship with the person he'd like to cheer and that effect what type of tweet goes out: If the person being cheered follows both the user who sent the request and CheerBot, CheerBot will send an @ mention to the person. If the person follows the user but not CheerBot there is a very small number of those @ mentions that are being sent in a day and he may get it, but usually CheerBot will tweet on its own account and encourage the friend to share it.

  • After a cheer is being sent, CheerBot will check how many consecutive @ mentions was sent in the current timeframe and if needed, make a random cool tweet on the account.

Additional stuff that helps

  • I try to combine both manual and automatic operations on the account: As a part of CheerBot's development I gather feedback, run surveys and talk to users from the account so I assume it makes it more human than a fully on automatic bot account.

  • A lot of stuff CheerBot do is slightly randomized: CheerBot can react very fast to every request but there is actually a small, random time between every user action and bot reaction. I'm sure Twitter's spam protection algorithm sees right through it but it makes me feel like it's not full on botting. I learnt that is also makes the user experience slightly better since there is a slight delay between a user sending a text and getting feedback, people tend to be more conversational with CheerBot rather than treat it like a bot.

CheerBot's such a fun ride and I'm happy to try and help if you have have any questions about running a bot on Twitter.

Feel free to send a message my way @omerkaplan!