pingcheng/slack-slash-command

Laravel Slack 划线命令插件

0.0.6 2018-12-29 13:02 UTC

This package is auto-updated.

Last update: 2024-09-26 01:40:38 UTC


README

Coverage Status

简介

Slack Slash Command 是一个帮助开发者将划线命令集成到 Laravel 应用中的 Laravel 扩展包。有关 Slack 划线命令的更多信息,请访问:https://api.slack.com/slash-commands

安装

  1. Slach Slash Command 推荐使用 composer 处理包控制,使用以下命令将此包添加到您的项目中

    composer require pingcheng/slack-slash-command
  2. 将服务提供者添加到 config/app.php

    PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider::class,
    
  3. 发布配置文件

    php artisan vendor:publish --provider="PingCheng\SlackSlashCommand\SlackSlashCommandServiceProvider" --tag=config
  4. 定义您的 .env

    SLACK_SIGNING_SECRET=*YOUR SLACK APP SIGNING SECRET*

您的第一个划线命令

  1. 创建一个继承自 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. 编辑配置文件 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. 为您的 Slack 划线命令创建控制器

    <?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. 将路由添加到您的 routes/web.phproutes/api.php

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

权限控制

您可以通过以下方式轻松控制您的划线命令的访问权限

通过频道 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
    }
}

通过用户 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
    }
}

有问题?

如果您有任何问题,请通过电子邮件发送给我 ping.che@hotmail.com 或提交一个问题,我会尽快回复 😄