yomo/redditbot

This package is abandoned and no longer maintained. No replacement package was suggested.

Laravel package for developing Reddit bots

v1.0.3 2019-01-27 14:28 UTC

This package is auto-updated.

Last update: 2022-06-04 11:35:36 UTC


README

RedditBot is a simple-to-use Laravel 5.x library that wraps the Reddit API to make reading, posting and commenting on Reddit threads really easy!

This library was partially inspired by Decronym.

Installation

Require this package with composer using the following command:

composer require yomo/redditbot

Laravel 5.5 or higher

Execute:

php artisan vendor:publish --tag=redditbot

Laravel 5.4 or lower

If you are running Laravel 5.4 and below, you need to add the service provider to the providers array in config/app.php

Redditbot\Providers\RedditbotServiceProvider::class

Then execute:

php artisan vendor:publish --tag=redditbot

Usage

This library assumes your bot has already been created through Reddit's App Panel, through which you would need to register a "personal use script" application.
More details on how to configure your bot can be found via Reddit's Quick Start instructions

Your .env file will require the following settings:

REDDIT_USERNAME
REDDIT_PASSWORD
REDDIT_APP_ID
REDDIT_APP_SECRET

Retrieving comments from a subreddit and responding

Once this is configured, you can create an instance of the Reddit class with:

$reddit = new Reddit();
$comments = $reddit->fetchComments('sheiseverywhere');

Comments are returned as a Collection, which you can easily iterate through using foreach or ->each. An example below shows how the bot replies to any instance of a specific phrase, using the "sinceLastCheck" function:

$reddit = new Reddit();
$comments = $reddit->fetchComments('jimny');

$comments->sinceLastCheck()->each(function($comment){

   if (strpos($comment->body, 'Suzuki Jimny') !== false) {
     $comment->reply('The Suzuki Jimny is the best off-roader ever built');   # This replies to the specific comment
     $comment->comment('Visit /r/jimny for more information'); # This submits a top-level post to the main thread
   }

});

There is also a Facade available, with an example shown below:

   $firstNewComment = Reddit::fetchComments('jimny')->sinceLastCheck()->first();

About the author

Stuart Steedman is the head of development at Yonder Media, a South African digital media agency operating out of Pretoria. He specialises in PHP and Laravel development, and is a speaker at tech and development related conferences.