PHP code example of geniusdev / magento2_slack

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

    

geniusdev / magento2_slack example snippets



namespace YourVendor\YourModule\Model;

use GeniusDev\Slack\Model\SlackClientProvider;
use Magento\Framework\Exception\LocalizedException;

class YourClass
{
    public function __construct(
        private SlackClientProvider $slackClientProvider
    ) {}

    public function notify()
    {
        try {
            $client = $this->slackClientProvider->getClient();
            
            // Simple text message
            $client->sendMessage('Hello from Magento 2!');
            
            // Complex message with Blocks
            $client->sendBlocks([
                [
                    'type' => 'section',
                    'text' => [
                        'type' => 'mrkdwn',
                        'text' => '*New order received!* \n <https://example.com|View Order>'
                    ]
                ]
            ]);
        } catch (LocalizedException $e) {
            // Handle error (e.g., module not configured)
        }
    }
}

use GeniusDev\Slack\Model\Slack\Block\Table as TableBlock;

// ...

public function __construct(
    private SlackClientProvider $slackClientProvider,
    private TableBlock $tableBlock
) {}

public function sendTable()
{
    $headers = ['Order ID', 'Customer'];
    $rows = [
        ['#10001', 'John Doe'],
        ['#10002', 'Jane Smith']
    ];
    
    $table = $this->tableBlock->create($headers, $rows);
    
    $this->slackClientProvider->getClient()->sendBlocks([$table]);
}

### Header Support

You can create header blocks using `GeniusDev\Slack\Model\Slack\Block\Header`.