Data source: Slack


1Presentation

Slack is a collaborative communication service, enabling users to exchange information either through ad-hoc communications or within communication channels.

It is possible to send messages within a communication channel, to notify users in an automated way.

If you have correctly configured the Slack connection parameters, Temma automatically creates an object of type \Temma\Datasources\Slack, with which you can send notifications. By convention, we'll assume that you've named this connection slack in the etc/temma.php file (see configuration documentation).

In the controllers, the Slack connection is then available by writing:

$slack = $this->slack;

In other objects managed by the dependency injection component, the Pushover connection is accessible by writing:

$slack = $loader->dataSources->slack;
$slack = $loader->dataSources['slack'];

2Configuration

To send notifications on Slack, you must first create a Slack application and enable webhooks. You can then create one or more webhooks for that application, with each webhook linked to a channel in which it can post messages.

In the etc/temma.php file (see configuration documentation), you declare the DSN (Data Source Name) used to connect to Slack.

The DSN used to connect to Slack can be written as slack://WEBHOOK

With WEBHOOK the value of the webhook URL, without the https:// prefix.
Example: hooks.slack.com/services/TXXXXXXXX/BYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ


3Notification content

Notifications can contain text, which accepts a derivative of Markdown syntax.

Notifications can also have "attachments", i.e. blocks that can themselves contain text, an image, a "footer" text, and a text that is positioned above the block.


4Unified calls

4.1Array-like access

// send a simple notification
$slack[''] = "Message to send";

// send a formatted notification
$slack[''] = [
    'text'        => 'Text of the notification, with <https://temma.net|a link>',
    'attachments' => [
        [
            'pretext' => 'Text above the block',
            'text'    => "*bold* _italic_ ~strikedout~ `code`",
            'image'   => "https://site.com/path/to/image.jpg",
            'footer'  => "Text of the block footer",
        ],
        'Text block',
        "Another text-only block",
        [
            'image' => "https://site.com/path/to/image2.jpg",
        ],
    ],
];

4.2Advanced method

// send a simple notification
$slack->set('', "Message to send");

// send a formatted notification
$slack->set('', [
    'text'        => 'Main text',
    'attachments' => [
        'First block',
        'Second block',
    ],
]);