PHP code example of pingcheng / slack-slash-command

1. Go to this page and download the library: Download pingcheng/slack-slash-command library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pingcheng / slack-slash-command example snippets


   
   
   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,
                       ]);
               });
       }
   }
   

   
   
   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'),
   ];
   

   
   
   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();
           }
       }
   }
   

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

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
    }
}
config/app.php
bash
   php artisan vendor:publish --provider="PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider" --tag=config
   
config/slackslashcommand.php
routes/web.php
routes/api.php