Skip to content

matthewbub/reddit-slackbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reddit Slack bot

MIT License

Browse through the most popular safe-for-work subreddits directly. This application is licensed under MIT. All 3rd party data is sourced directly from Reddit.

This application was built to experiment with Slacks Bolt library. It is simply a fun proof of concept.

Preview live @ Ghst Solutions Slack Workspace

Slash Commands

Command Name Optional parameters Description
/reddit r/any-subreddit The quickest way to browse is via the /reddit command. If no parameters are passed, the bot will display the reddit home page. If a valid parameter is passed, the bot will attempt to fetch or throw "Whoops. Something went wrong".

Local Setup

This app runs locally on ngrok. Let's summarize what we will need to do to get started.

Set environmental variables

Create the .env.local file. We can start by renaming the .env.example to match the dotenv declaration.

# rename .env.example to .env.local
mv .env.example .env.local

Start ngrok server

~/ngrok http 3009

Once the ngrok server is online we will need to update the URLs we plan on using via https://api.slack.com/apps. We are going to refer to https://e6829b5713d5.ngrok.io as the base URL for this example. Keep in mind you will be assigned a unique temporary ngrok URL when running the above command

Let's also take a moment to verify the credentials used here also match the local database url used in the .env.example file. Once we've built the local database, we are ready to start the Bolt app.

Start the Bolt app

What is Bolt? For our understanding and general context, Bolt is a wrapper that sits on top of an Express server, used to Slack applications.

Now we are ready to set up our source code. Assuming we have set our environmental variables this should be a quick and painless process.

# install project dependencies
npm install

# we ignore /dist by default
# let's build that
npm run build 

# start the Bolt app
npm run start

At this point we should be presented with the standard app listening on http://localhost:3009 message in the terminal. This is a great point to begin testing your Slack connections.

Frequent reasons this process might fail

Frequent reasons this process might fail

  • Your environmental variables aren't being read by dotenv.

Check out the ./lib/index.ts file and verify that your .env.local file matches the absolute path specified in the dotenv.config() method.

const path = require('path');
// ...
// Configures local environment
require('dotenv').config({path: path.resolve(__dirname, "../.env.local")});

// DEBUGGING
console.log(path.resolve(__dirname, "../.env.local"));
  • Slack App does not have required permissions
  • Ngrok server shut down and needs to be restarted By default the ngrok URL is active for 2 hours at which point it will automatically timeout. When we reset the ngrok server we will need to update the base URL as defined in the Start ngrok server section.

This can be easily identify as the root issue to most unexpected errors. Be sure to keep an eye on your ngrok server throughout development.

  • [Slash Commands] Reddit Bot needs to be invited to the channel Found this to be the case when migrating to @slack/bolt. This thread on Stack Overflow helped provide a solution.

About

Reddit integrated into Slack as a message bot

Topics

Resources

License

Stars

Watchers

Forks

Contributors