d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Reddit Bots on Node.Js & Docker

If you are a Reddit User you must have come across “Reddit bots”. Reddit bots generally reply in a pretty helpful way to user posts or comments. We will be learning how to build a Reddit Bot with Node.js

Creating a Reddit App

First and foremost, You need to create a Reddit account for the Reddit bot to post from. Thereafter, Go to Reddit’s app preferences section and create your application.

Add a name for the application, you can also add a little description about it.

Select script option for the type of application and click on create app.

After creating the app, make note of the client ID and secret ID for the later need.

Creating the Node.js app

Install nodejs and npm if you haven’t already. Create a new directory and run npm init to create a package.json file. Let’s install the packages that we need.

  1. Dotenv

To load the sensitive environment variables for the Reddit app we will be using packge called dotenv

$ npm i -S dotenv

  1. Snoowrap and Snoostorm

Next up, we’ll be using Snoowrap as a Node.js wrapper for the Reddit API, and Snoostorm as a way to easily access the stream of Reddit comments through Snoowrap.

$ npm i -S snoowrap snoostorm

Building out the app

Let us create a .env file to store the environment variables.

CLIENT_ID=****

CLIENT_SECRET=****

REDDIT_USER=****

REDDIT_PASS=****

Index.js

Create an index.js file to start the coding.

First, we need to configure the environment variables with dotenv, and then import Snoowrap and Snoostorm

Before we start getting Reddit comments, we need to configure some options that we’ll pass into Snoostorm. This will determine the subreddit we will be streaming from, and the number of results per polling cycle. Then place the code of what function the bot will perform as per your creativity.

Run node index.js in the command prompt.

The output can be seen in the reddit app
The output seen in the reddit app

Dockerize

How docker works ?

It allows you to bundle your app with all its dependencies into a container. Indeed, after building our container, it will be our packaged application. To do so, our app should include a Dockerfile which will describe how the container would be built.

1.Create a Dockerfile

2.Create a docker-compose.yml

3.Run $docker-compose up.

You can check the reddit app after running compose up and look at the results. The bot is all ready and dockerized!

Add Comment