Here at , 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. StdLib 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 ”, 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. Marvin This guide is powered by . You can read the introduction to StdLib sourcecode . In short, sourcecode is the easiest way to get started building and web apps using new serverless . 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. StdLib sourcecode here deploying technology Step 1: Register a Reddit App Log into Reddit, head over to the , and click ‘create an app’. Reddit app preferences page 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 and scroll down to the inline editor. Open the functions folder and you’ll find four files: Reddit Bot Sourcecode page : Reads the most recent comment from a subreddit, looks for keywords, and responds to comments that contain them. __main__ : 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. clean : Used by , this function responds to a post or comment. Remember, Reddit supports markdown so you can really be creative here. reply __main__ : Used by , this function takes deletes a post made by the bot. delete clean Now you can click ‘Create Service from Source’ and you will be prompted to enter some environment variables. The first two, and can be found the Reddit . 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. REDDIT_SECRET REDDIT_KEY app preferences page We can try out the bot directly from the command line. If you don’t have the , you can get then by running: StdLib command line tools $ npm install lib.cli -g Navigate to a StdLib workspace or create one inside an empty directory with . You need to be authenticated to run this service, that prevents others from posting from your Reddit account. You can get a library token . Now you can run: $ lib init here $ lib <username>.reddit-bot[@dev] stdlibbots -t <library token> And go to and you should see a post from your bot. this thread 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 . 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: Scheduled Tasks $ 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 . You can modify the code and release it to a dev environment with or create a new release version with . $ lib get <username>/reddit-bot $ lib up dev $ lib release If you create something that you like, you can convert your service into a new sourcecode with . 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 $ lib source:publish And that’s it! Thanks for reading! Hopefully this guide has shown you some of the capabilities of . 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: , or follow and the team on Twitter. Stdlib Sourcecode steven@stdlib.com me StdLib As always, we look forward to hearing from you and happy building
Share Your Thoughts