Why I wrote a WhatsApp bot

In 2011, when I entered into the final year of my undergraduate degree, procrastination was the norm amongst my peers and I. I doubt that this is any different today, or even that it was different 30 years ago, but with the advent of smartphones and social networks becoming increasingly mainstream, one particular website was distracting students across campus the most.

I knew that whilst I was sitting alone in my room, tapping away at my final year project, thousands of other people my age were doing the same thing. Maybe if I could just find somebody marginally more interesting to talk to, I could put off writing this thesis for a few minutes. This compulsion, this urge to believe that something incredible is going on with people that I know is what drew me to click that bookmarked Facebook tab about every 2 minutes.

I would be disappointed almost every time, of course - everyone was doing the exact same thing as me - but every so often that comment or like would feed the habit and the cycle would continue. This was enough of a problem for me and my housemates that we even approached the matter for an HCI module, producing a design for a browser extension which would help keep you focused. Obviously, we never got 'round to actually building it as we were too busy seeing what everyone else was getting up to on Facebook.

I never decided to conscientiously 'quit' Facebook like so many hopelessly obsessed people do once they realise that they're having serious problems focusing - only to invariably return to it within a [day|week|month]. Instead, I took a step back and thought about why I cared about knowing what people were doing so much. To cut a long story slightly shorter, I realised that I didnt.

"When you're 20 you care what everyone thinks, when you're 40 you stop caring what everyone thinks and when you're 60 you realize no one was ever thinking about you in the first place"

Whilst making this post embarrasingly ironic, I do also genuinely attribute some part of my success to the realisation that the large majority of those people didn't give a shit about what I was doing and that I shouldn't care what they were doing either. Since that point, I have put Facebook into a largely 'tag-only' mode, where I rarely post any updates and I am just tagged or mentioned by other people who use it.

We are social creatures, though, so that void needed to be filled by something (slightly) more meaningful. Instead of broadcasting to and consuming from hundreds of people I don't know, I cut that down to about the 20 or so people that I cared about at any one time and made a simple, hyperlocal, but fulfilling way to communicate with them; the humble WhatsApp group.

Our WhatsApp groups contain mostly inane messages, ranging from fantasy football chatter to the surprising prevalent toilet-selfie. We put such weight in this social channel that even if just two of us are meeting up we will organise it in front of everyone on the group, who are customarily treated to a picture from the perspective of both parties once the meeting happens, much to the chagrin of my girlfriend who thinks this is the lamest thing anybody could do (her being right isn't the point).

Despite the fact that the chat is mostly nonsense, it's our nonsense and we've gone to great lengths to ensure that we have a full history of all of the messages backed up somewhere for good measure. Through new phones, new members and new numbers, we strive to keep a golden record of all the bollocks we've spouted. We will never look at this history, but it seems important to have nonetheless.

Until now, this history has been fragmented and distributed, based on the text backups WhatsApp allows you to email to yourself and a cross-section of image folders from group members' multiple phones. This doesn't really seem good enough, so I set out about thinking of a way to have a golden source of all of this data - surely WhatsApp has some way that I can pull history from an API, right?

Unfortunately, WhatsApp has a sordid history of being notoriously unfriendly to community developers and security researchers, often threatening legal action. At the time of writing, I can't even find a responsible disclosure or bug bounty program for WhatsApp or mention of WhatsApp in any of Facebook's. They've also outright declared that there will not be an API anytime soon.

I realised that it was time to take matters into my own hands. I needed some way of keeping a full history of our chat, media and all. How else would we be able to generate stats, like who was the most active, got the most 👍 responses, or went the longest without a reply?

My initial thought was to simply ingest WhatsApp backups periodically, but the prospect of manually merging with the media data was enough to stop me in my tracks - we needed a fully automated and sustainable solution. A little while later (probably whilst in the shower where I do all of my best thinking) I realised that the best way to track of this was simply as a participant of the group. Seeing as the data is ostensibly encrypted, there wouldn't be a way to read this from the phone itself. That left only one solution: simulate a phone and use that to collect the data.

From that idea, BotsApp was born. BotsApp is a node library, built on top of the hard work of other people working on reversing the WhatsApp API. You can use it to construct a simple bot which will log all of your group messages to some other system, as well as sending messages back to the group:

'use strict';

var Botsapp = require('botsapp');  
var process = require('process');

var yourBot = new Botsapp.Bot({  
  adapter: {
    msisdn: '123456789', // phone number with country code
    username: 'YourBot', // your name on WhatsApp
    password: 'asdfghjkl', // WhatsApp password
    ccode: '44' // country code
  }
});

// Register a handler which logs every message
var anyMessage = new Botsapp.Trigger().always();  
yourBot.registerTrigger(anyMessage, function onTrigger(event) {  
  console.log(event);
  // Now save the message to your store
});

// Get a thumbsup, give a thumbsup
var thumbsupEmoji = new Buffer([240, 159, 145, 141]);  
var thumbsUp = new Botsapp.Trigger().withEmoji(thumbsupEmoji);

yourBot.registerTrigger(thumbsUp, function onTrigger(event) {  
  var emoji = thumbsupEmoji.toString('utf8');

  yourBot.sendMessage(event.from, emoji, function onSend() {
    console.log('Sent emoji to', event.from);
  });
});

Whilst this has been working very well for me, WhatsApp are definitely on a mission to censor all such things from the internet, and as of the time of writing, the 'whatsapi' project that BotsApp depended on has been removed under the constant threat of legal action.

I doubt that Botsapp will last much longer itself either, but just in case a scourge of annoying WhatsApp bots appear, I thought I better preemptively set the record straight about why I created this project. In the meantime, I'll just keep hoping that somebody more amenable to the community joins WhatsApp and starts making some much-needed change.