PHP code example of masterro / laravel-slack

1. Go to this page and download the library: Download masterro/laravel-slack 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/ */

    

masterro / laravel-slack example snippets


// config/app.php
'providers' => [
    ...
    Pdffiller\LaravelSlack\LaravelSlackServiceProvider::class,
];

return [
    /*
     * Bot User OAuth Access from Slack App
     */
    'bot-token' => env('SLACK_BOT_TOKEN', null),

    /*
     * Verification token from Slack App
     */
    'verification_token' => env('SLACK_VERIFICATION_TOKEN', null),

    /*
     * Handlers are processed by controller
     */
    'handlers' => [
    ],

    /*
     * Endpoint URL
     */
    'endpoint-url' => 'slack/handle'
];

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
$slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\ChatPostMessage())
           ->setChannel("#ABCDEF") //channel id from web app
           ->setText("asdsadsad");
$slack->sendMessage();

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\ChatUpdate())
           ->setChannel("#ABCDEF")
           ->setTs('1405894322.002768')
           ->setText("updated text");
$slack->sendMessage();

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
$slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\ChatPostMessage())
                   ->setChannel("#ABCDEF")
                   ->addAttachment(\Pdffiller\LaravelSlack\RequestBody\Json\Attachment::create()
                                         ->setText("this is text")
                                         ->setColor('#36a64f')); // default color is #D3D3D3
$slack->sendMessage();

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
$slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\ChatPostMessage())
           ->setChannel("#ABCDEF")
           ->addAttachment(\Pdffiller\LaravelSlack\RequestBody\Json\Attachment::create()
                         ->addFields([                  
                                 [                           
                                     'title' => 'User Id',
                                     'value' => 10
                                     /*, 'short'=true/false*/  // short is true by default, it means that next field is shown on the same line
                                 ],
                                 \Pdffiller\LaravelSlack\RequestBody\Json\AttachmentField::create('Team Id', 10),
                             ]
                         ));
$slack->sendMessage();

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
$slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\ChatPostMessage())
               ->setChannel("#ABCDEF")
               ->addAttachment(\Pdffiller\LaravelSlack\RequestBody\Json\Attachment::create()
                   ->setCallbackId('callback-id-is-used-in-handler')
                   ->setFallback('Fallback text') // Required plain-text summary of the attachment
                   ->setColor('#36a64f')
                   ->addActions([
                           [
                               'name' => 'button-name',
                               'text' => 'Accept',
                               'value' => 1,
                               /*'style' => 'primary/danger',
                               'type'  => 'button'*/
                           ],
                           \Pdffiller\LaravelSlack\RequestBody\Json\AttachmentAction::create('button-name', 'Decline', 0)
                       ]
                   ));
$slack->sendMessage();

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
$slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\FilesUpload())
                ->setChannel("#ABCDEF")
                ->setFilePath("/uploads/one.txt") //path to file in your project or web
                ->setFileName("one.txt); 
$slack->sendMessage();

$slack = resolve(\Pdffiller\LaravelSlack\Services\LaravelSlackPlugin::class);
$slack->buildMessage(new \Pdffiller\LaravelSlack\AvailableMethods\DialogOpen())
                   ->setTriggerId('trigger_id')
                   ->setDialog(\Pdffiller\LaravelSlack\RequestBody\Json\Dialog::create()
                           ->setCallbackId('will-be-used-in-handler')
                           ->setTitle('Title')
                           ->setSubmitLabel('Save')
                           //->setState(json_encode([])) // you can save some data between opening dialog and handling                                                                   it's interaction in the state parameter
                           ->addElement(\Pdffiller\LaravelSlack\RequestBody\Json\DialogElement::create()
                                             ->setName('reason')
                                             ->setLabel('...')
                                             ->setType(DialogElement::TEXTAREA_TYPE))
                   );
$slack->sendMessage();

use Pdffiller\LaravelSlack\Handlers\BaseHandler;

class CustomHandler extends BaseHandler
{    
    // check if this handler should be executed
    public function shouldBeHandled()
    {
        $payload = json_decode($this->request->get('payload'), true);
        $callBackId = Arr::get($payload, 'callback_id');

        return $callBackId === 'some-callback-id';
    }
    
    public function handle()
    {
        $payload = json_decode($this->request->get('payload'), true);
        // ...
    }
}

'handlers' => [
        \App\Slack\Handlers\CustomHandler::class,
        ...
]
bash
php artisan vendor:publish --provider="Pdffiller\LaravelSlack\LaravelSlackServiceProvider"
bash
php artisan migrate