DIY Reddit Bots with DataFire

Bobby Brennan
Dec 30, 2015 · 4 min read

Today I’d like to highlight a super fun use case for DataFire - Reddit bots.

What’s a Bot?

Here’s a list of 300+ bots currently helping (and occasionally annoying) the Reddit community.

Building a Reddit Bot

  • Scan r/jokes for jokes in the form “so-and-so walks into a bar”
  • Reply with the comment: “That must have hurt!”

Sounds easy right?

Without DataFire

  1. Connect to the Reddit API
  2. Write code that searches r/jokes and makes comments
  3. Keep that code running in the background somewhere

Here’s an article that goes in to some depth on how to build a bot from scratch using Python, but with DataFire we can eliminate steps 1 and 3 and just focus on the code.

With DataFire

Let’s use DataFire to create a Dataflow that runs our Reddit bot. The Dataflow will have three steps:

  • Search r/jokes for “walks into a bar”
  • Get all the comments for the top joke
  • If we haven’t already commented, say “That must have hurt!”

Searching Jokes

function request() {
return {
subreddit: 'jokes',
q: 'walks into a bar',
sort: 'new',
}
}

If you haven’t already, this is a good time to authenticate your Reddit account with DataFire. In the Settings section, click the orange Authorize button. Be sure to select the read and submit permission scopes.

If you don’t want to use your main account, you can also create a separate Reddit account for the bot.

Getting Comments

function request(data) {  // Find a joke that matches our "into a bar" pattern
var joke = data[0].data.children.filter(function(joke) {
return joke.data.selftext.match(/([A-Z][^\.]* into a bar)/);
})[0];
// If there's no matching joke, bail out.
if (!joke) return [];
return {article: joke.data.id, depth: 0, sort: 'new'};
}

This code scans through data[0] - the response from our search in r/jokes - to find jokes that contain the phrase “into a bar”. If it finds something, it retrieves the comments for that joke.

Posting a Comment

Now we’ll get a little fancier with the code:

function request(data) {  // First check if we've already commented
var alreadyCommented = false;
var comments = data[1][1].data.children;
comments.forEach(function(comment) {
if (comment.data.author === 'BadPunBot')
alreadyCommented = true;
})
if (alreadyCommented) return [];
// Next extract the "into a bar" sentence
var joke = data[1][0].data.children[0];
var quote = joke.data.selftext.match(/([A-Z][^\.]* into a bar)/);
if (!quote) return [];
quote = quote[1];
return {
thing_id: joke.kind + '_' + joke.data.id,
text: '> '+ quote + '\n\nThat must have hurt.',
api_type: 'json',
}
}

There are three things happening here:

  • First we scan through data[1] (the response from GET /comments/{article}) to see if BadPunBot has already made a post on this thread. If so, we bail out by returning an empty array [].
  • Next, we use a regular expression to grab the sentence that contains “into a bar”. We’ll quote this in our comment
  • Finally, we issue the request to POST /api/comment, with the quoted text and our witty reply.

Running the Dataflow

Going Forward

  • Use DatumBox’s adult content detection to auto-moderate a subreddit
  • Use the Wikimedia API to automatically post the first paragraph of any Wikipedia article that gets linked
  • Add your own API to search for relevant content to promote on Reddit

There’s so much that can be done here - we’re excited to see what the Reddit community builds!

DataFire.io

Blog for datafire.io

Bobby Brennan

Written by

I’m a Software Engineer, specializing in Dev Tools, NLP, and Machine Learning

DataFire.io

Blog for datafire.io