pingcheng/slack-slash-command

Laravel slack slash command plugin

0.0.6 2018-12-29 13:02 UTC

This package is auto-updated.

Last update: 2024-04-26 00:38:25 UTC


README

logo.png

68747470733a2f2f7472617669732d63692e6f72672f70696e676368656e672f736c61636b2d736c6173682d636f6d6d616e642e7376673f6272616e63683d6d6173746572 Coverage Status 68747470733a2f2f706f7365722e707567782e6f72672f70696e676368656e672f736c61636b2d736c6173682d636f6d6d616e642f762f737461626c65 68747470733a2f2f706f7365722e707567782e6f72672f70696e676368656e672f736c61636b2d736c6173682d636f6d6d616e642f6c6963656e7365

Introduction

Slack Slash Command is a Laravel package that helps developer integrate the slash command to their Laravel applications. For more information about Slack slash command, please visit: https://api.slack.com/slash-commands.

Installation

  1. Slach Slash Command recommends using composer to handling the package control, use the following command to add this package to your project

    composer require pingcheng/slack-slash-command
  2. Add service provider to config/app.php

    PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider::class,
    
  3. Publish config file

    php artisan vendor:publish --provider="PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider" --tag=config
  4. Define your .env

    SLACK_SIGNING_SECRET=*YOUR SLACK APP SIGNING SECRET*

Your first slash command

  1. Create your command extends from PingCheng\SlackSlashCommand\SlackSlashCommand

    <?php
    
    namespace App\Slack\Commands;
    
    use Illuminate\Notifications\Messages\SlackMessage;
    use PingCheng\SlackSlashCommand\SlackSlashCommand;
    
    class SampleCommand extends SlackSlashCommand
    {
        public function handle() {
            // process your command logic here...
    
            // you can return plain text as the response
            // return "Hi, I received your slash command";
    
            // or, you can return a Laravel Slack Message object
            return (new SlackMessage)
                ->success()
                ->content("Got your slash command! :smirk:")
                ->attachment(function ($attachment) {
                    $attachment->title('Details')
                        ->fields([
                            'Username' => $this->user_name,
                            'User ID' => $this->user_id,
                            'Channel Name' => $this->channel_name,
                            'Channel ID' => $this->channel_id,
                        ]);
                });
        }
    }
  2. Edit config file config/slackslashcommand.php

    <?php
    
    return [
        
        // the collection of your slack command
        // the array key is the command name (defined in your slack app console)
        'commands' => [
            'greeting' => \App\Slack\Commands\SampleCommand::class,
        ],
    
        'signing_secret' => env('SLACK_SIGNING_SECRET'),
    ];
  3. Create a controller for your slack slash command

    <?php
    
    namespace App\Http\Controllers;
    
    use PingCheng\SlackSlashCommand\CommandManager;
    use PingCheng\SlackSlashCommand\Exceptions\CommandNotFoundException;
    use PingCheng\SlackSlashCommand\Exceptions\InvalidHeadersException;
    
    class SlackController extends Controller
    {
        public function slashCommand() {
            try {
                // simple run CommandManager::run()
                // the manager would check the command list
                // and run the related command
                return CommandManager::run();
            } catch (CommandNotFoundException $e) {
                // would trigger if the command is not found
                return $e->getMessage();
            } catch (InvalidHeadersException $e) {
                // would trigger if the slack verfication is failed to meet
                return $e->getMessage();
            }
        }
    }
  4. Add route to your routes/web.php or routes/api.php

    // You can define your own route
    Route::post('slack/slashcommand', 'SlackController@slashCommand');

Permission control

You can easily control your slash command accessibility

Via Channel ID

class SampleCommand extends SlackSlashCommand
{
    // accepts array, only defined channel ids are allowed to execute this command
    protected $limit_on_channel_ids = ['channel_id_1', 'channel_id_2'];
    
    public function handle() {
        // command handler
    }
}

Via User ID

class SampleCommand extends SlackSlashCommand
{
    // accepts array, only defined user ids are allowed to execute this command
protected $limit_on_user_ids = ['user_id_1', 'user_id_2'];
    
    public function handle() {
        // command handler
    }
}

Questions?

If you have any questions, please email me ping.che@hotmail.com or leave an issue, I would respond as soon as possible 😄