PHP code example of slack-php / slack-app-framework
1. Go to this page and download the library: Download slack-php/slack-app-framework 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/ */
slack-php / slack-app-framework example snippets
use SlackPhp\Framework\App;
use SlackPhp\Framework\Context;
App::new()
->command('cool', function (Context $ctx) {
$ctx->ack(':thumbsup: That is so cool!');
})
->run();
declare(strict_types=1);
use SlackPhp\BlockKit\Surfaces\{Message, Modal};
use SlackPhp\Framework\{App, Context, Route};
// Helper for creating a modal with the "hello-form" for choosing a greeting.
$createModal = function (): Modal {
return Modal::new()
->title('Choose a Greeting')
->submit('Submit')
->callbackId('hello-form')
->notifyOnClose(true)
->tap(function (Modal $modal) {
$modal->newInput('greeting-block')
->label('Which Greeting?')
->newSelectMenu('greeting')
->forExternalOptions()
->placeholder('Choose a greeting...');
});
};
App::new()
// Handles the `/hello` slash command.
->command('hello', function (Context $ctx) {
$ctx->ack(Message::new()->tap(function (Message $msg) {
$msg->newSection()
->mrkdwnText(':wave: Hello world!')
->newButtonAccessory('open-form')
->text('Choose a Greeting');
}));
})
// Handles the "open-form" button click.
->blockAction('open-form', function (Context $ctx) use ($createModal) {
$ctx->modals()->open($createModal());
})
// Handles when the "greeting" select menu needs its options.
->blockSuggestion('greeting', function (Context $ctx) {
$ctx->options(['Hello', 'Howdy', 'Good Morning', 'Hey']);
})
// Handles when the "hello-form" modal is submitted.
->viewSubmission('hello-form', function (Context $ctx) {
$state = $ctx->payload()->getState();
$greeting = $state->get('greeting-block.greeting.selected_option.value');
$ctx->view()->update(":wave: {$greeting} world!");
})
// Handles when the "hello-form" modal is closed without submitting.
->viewClosed('hello-form', function (Context $ctx) {
$ctx->logger()->notice('User closed hello-form modal early.');
})
// Handles when the "hello-global" global shortcut is triggered from the lightning menu.
->globalShortcut('hello-global', function (Context $ctx) use ($createModal) {
$ctx->modals()->open($createModal());
})
// Handles when the "hello-message" message shortcut is triggered from a message context menu.
->messageShortcut('hello-message', function (Context $ctx) {
$user = $ctx->fmt()->user($ctx->payload()->get('message.user'));
$ctx->say(":wave: Hello {$user}!", null, $ctx->payload()->get('message.ts'));
})
// Handles when the Hello World app "home" is accessed.
->event('app_home_opened', function (Context $ctx) {
$user = $ctx->fmt()->user($ctx->payload()->get('event.user'));
$ctx->home(":wave: Hello {$user}!");
})
// Handles when any public message contains the word "hello".
->event('message', Route::filter(
['event.channel_type' => 'channel', 'event.text' => 'regex:/^.*hello.*$/i'],
function (Context $ctx) {
$user = $ctx->fmt()->user($ctx->payload()->get('event.user'));
$ctx->say(":wave: Hello {$user}!");
})
)
// Run that app to process the incoming Slack request.
->run();