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.
-
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
-
Add service provider to
config/app.php
PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider::class,
-
Publish config file
php artisan vendor:publish --provider="PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider" --tag=config
-
Define your
.env
SLACK_SIGNING_SECRET=*YOUR SLACK APP SIGNING SECRET*
-
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, ]); }); } }
-
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'), ];
-
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(); } } }
-
Add route to your
routes/web.php
orroutes/api.php
// You can define your own route Route::post('slack/slashcommand', 'SlackController@slashCommand');
You can easily control your slash command accessibility
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
}
}
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
}
}
If you have any questions, please email me [email protected] or leave an issue, I would respond as soon as possible 😄