Build a “Serverless” Reddit Bot in 3 Steps with Node.js and StdLib Sourcecode

Written by notoriaga | Published 2017/10/24
Tech Story Tags: javascript | reddit | stdlib | serverless | bots

TLDRvia the TL;DR App

Here at StdLib, we’re big fans of Reddit. If you spend any time browsing Reddit, you will without a doubt encounter a few bots. They have many different purposes, from moderating subreddits to summarizing articles. Regardless of their goal, many of these bots perform just two basic tasks: reading comments and replying to ones that match certain criteria.

In this guide, you’ll learn how to deploy an infinitely-scalable, “serverless” Reddit bot from just your browser. By the end, you’ll have a bot that will read and respond to comments that include the phrase “Hey Marvin”, but you’ll be fully able to customize what your bot responds to. The only configuration you’ll need to do is to specify a subreddit you’d like to scan and your Reddit credentials.

This guide is powered by StdLib sourcecode. You can read the introduction to StdLib sourcecode here. In short, sourcecode is the easiest way to get started building and deploying web apps using new serverless technology. By leveraging templates built by StdLib’s partners and community, you can deploy a variety of projects in addition to this Reddit bot, such as Slack apps, Stripe stores, and Twilio messaging hubs, directly from the browser.

Step 1: Register a Reddit App

Log into Reddit, head over to the Reddit app preferences page, and click ‘create an app’.

Creating a Reddit App

Give it whatever name you’d like and select the script type. A script-based app will post comments from the account that created it. So, if you don’t want this bot posting under your normal account, you should create a new one. Accounts with verified emails can post more so make sure to add an email if you use a new account. Finally, Reddit apps require a redirect URI. You won’t be using it however, so just put in a dummy one, such as [http://www.example.com/](http://www.example.com/)

Step 2: Deploy the Reddit App

Reddit Bot Sourcecode

Keeping the app preferences page open, navigate to the Reddit Bot Sourcecode page and scroll down to the inline editor. Open the functions folder and you’ll find four files:

  • __main__ : Reads the most recent comment from a subreddit, looks for keywords, and responds to comments that contain them.
  • clean : Takes in a number, and removes any comments posted with a score (upvotes plus downvotes) less than that number. This lets users reading the subreddit remove unwanted comments.
  • reply : Used by __main__ , this function responds to a post or comment. Remember, Reddit supports markdown so you can really be creative here.
  • delete : Used by clean , this function takes deletes a post made by the bot.

Now you can click ‘Create Service from Source’ and you will be prompted to enter some environment variables. The first two, REDDIT_SECRET and REDDIT_KEY can be found the Reddit app preferences page. The last two are just the username and password for the Reddit account. Once those are filled in you can click Deploy, and your bot will be live to a development environment.

We can try out the bot directly from the command line. If you don’t have the StdLib command line tools, you can get then by running:

$ npm install lib.cli -g

Navigate to a StdLib workspace or create one inside an empty directory with $ lib init . You need to be authenticated to run this service, that prevents others from posting from your Reddit account. You can get a library token here. Now you can run:

$ lib <username>.reddit-bot[@dev] stdlibbots -t <library token>

And go to this thread and you should see a post from your bot.

Step 3: Running the Bot

Creating a Scheduled Task from the Command Line

With an API set up to read and respond to posts, all you have to do now is keep it running. This is a great use case for StdLib’s Scheduled Tasks. You can only run Scheduled Tasks with immutable release versions of a service. To get the code for your bot and publish a release version of it run:

$ lib get <username>/reddit-bot$ cd <username>/reddit-bot$ lib release

Now you can set a scheduled task with:

$ lib tasks:create <username>/reddit-bot

The resulting prompt will ask you for one of your StdLib library tokens. Your Reddit bot will only respond to requests authenticated with one of your library tokens, and specifying a token here allows the scheduled task to properly trigger your bot. Next, the prompt will ask for which subreddit you want your bot to run on. Finally, you’ll need to specify how often you want the task to run. Depending on the popularity of the subreddit you can get away with running the bot less often, but once every five minutes (or twelve times an hour) should work.

Now, you should set a scheduled task to remove unwanted comments with:

$ lib tasks:create <username>/reddit-bot clean

The prompt is going to ask what threshold you want to set for deletion and how often it should be run. Something like -5 and once an hour would be a good starting place.

And that’s it! You now have a Reddit bot monitoring a subreddit and automatically replying to comments.

Adding Functionality

Now that you have a simple bot up and running, you might be interested in creating more complex workflows. As I mentioned earlier, you can retrieve you can get the code for your service with $ lib get <username>/reddit-bot . You can modify the code and release it to a dev environment with $ lib up dev or create a new release version with $ lib release .

If you create something that you like, you can convert your service into a new sourcecode with $ lib source . That will create a new copy of your code, in a new directory. You can publish this sourcecode for others to use with $ lib source:publish .

And that’s it! Thanks for reading! Hopefully this guide has shown you some of the capabilities of Stdlib Sourcecode. Building a Reddit bot is just one of the many ways you can get started with sourcecode. If you have a neat idea you’d like to share, reach out to me directly by e-mail: steven@stdlib.com, or follow me and the StdLib team on Twitter.

As always, we look forward to hearing from you and happy building


Published by HackerNoon on 2017/10/24