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

What’s a Bot?

Bots are computer programs that act like humans, and are usually used inside chat clients or forums. Bots can be simple (e.g. chastising users who curse) or complex (e.g. able to hold entire conversations).

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

Building a Reddit Bot

So what does it take to build a bot on Reddit? Let’s start with a simple example: BadPunBot. Here’s what he’ll do:

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

To get our bot running, we’ll have to do a few things:

Connect to the Reddit API Write code that searches r/jokes and makes comments 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

Note: you can follow along with the instructions by visiting datafire.io/editor, or jump straight to the finished product.

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

For the first step, we’ll click Add Link, and choose the Reddit link followed by the GET /r/{subreddit}/search operation. Then we specify our request:

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

Next we’ll retrieve the comments for the first joke. Again, click Add Link, and choose the Reddit link followed by the GET /comments/{article} operation. Here’s the code for the request:

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

Finally, we need to post our comment! Again, click Add Link, and choose the Reddit link followed by the POST /api/comment operation.

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 + '



That 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 [].

) 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

We’re ready to go! You can click Fire Now to test the Dataflow and submit your first snarky comment. To run the Dataflow every day, click Save, be sure that Repeat is set to “On”, and enter the days/times you want the Dataflow to run (free accounts can run Dataflows up to once per day)

Going Forward

BadPunBot a simple (and fairly useless) Reddit bot. Many more interesting things can happen by taking advantage of the hundreds of other services available on DataFire. For example, you could:

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!