How I created a Reddit bot with Laravel

The boring part: research

The first thing I had to find out was if the Reddit submissions containing pictures had similarities or patterns. Developers thrive on patterns as they make our life easier. I started to scan several subs and noticed two patterns:

  • Some subs had dedicated “flairs” for in-game pictures
  • Others didn’t, but a lot of the submission titles contained the word “screenshot
  • Creating a Reddit application
  • Authenticating via OAuth2 with the credentials provided by the application
  • Use the corresponding API calls to fetch the data I need

The fun part: programming and putting it all together

Figuring out the API calls

First I had to figure out the requests’ and responses’ structure of the Reddit API. After messing around for a few hours I came up with five requests I would need to fetch and cross-post the required data.

  1. Search subreddits that have a dedicated flair:
https://oauth.reddit.com/r/[SUBREDDIT]/search.json?q=flair:”[FLAIR]”&restrict_sr=1&sort=new
https://oauth.reddit.com/r/[SUBREDDIT]/search.json?q=title:[QUERY]&restrict_sr=1&sort=new
https://oauth.reddit.com/r/[SUBREDDIT]/new.json
https://oauth.reddit.com/api/submit
https://oauth.reddit.com/api/editusertext

Creating a client with Guzzle

With great API calls, comes a great HTTP client a wise man once said… So that’s what I set out to do. Laravel ships with Guzzle, which is the most common library to build your HTTP request with.

Keeping track of cross-posts

I created a simple model with Artisan to keep track of all the cross-posts:

php artisan make:model RedditCrossPost
Database snippet

A structured way to define the subreddits to scan

To “feed” the bot with the subreddits to scan, I’ve set up a simple but effective structure.

  • FlairQuery
  • SearchQuery
  • NewestQuery

Start cross-posting

I decided to run the bot every 15 minutes, frequently enough to generate new content, but not too much for Reddit to consider it as spam. Laravel allows you to do this with scheduled tasks.

$schedule->command('reddit:run-bot')->cron('*/15 * * * *')
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Wrapping up

HOORAY! My bot is currently up and running and doing a very good job at centralizing a lot of in-game screenshots. The time I have to spend to manually moderate it is close to zero, which means I have accomplished what I had set out to do from the beginning:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store