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:

$this->slack

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

$this->_loader->dataSources['slack']

2Configuration

To send notifications on Slack, you must first install the Incoming Webhook application (official application designed by Slack) in your Slack organization, then retrieve the provided Incoming Webhook URL.

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 in several ways:

  • slack://WEBHOOK
  • slack://AUTHOR@WEBHOOK
  • slack://AUTHOR:ICON@WEBHOOK

Avec :

  • WEBHOOK the value of the Incoming Webhook URL retrieved, without the https:// prefix.
    Example: hooks.slack.com/services/TXXXXXXXX/BYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ
  • AUTHOR the name to be displayed as the notification sender.
  • ICON the URL to the image to be used as avatar by the notification sender.
    Example: https%3A%2F%2Fsite.com%2F%nth2Fto%2Ficon.png

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, "footer" text, text that is positioned above the block, as well as an author name, which can be a clickable link and/or include an avatar image.


4Unified calls

4.1Array-like access

// send a simple notification
$this->slack['CHANNEL'] = "Message to send";
// with CHANNEL, which corresponds to the name of the channel you wish to
// send the message (with the pound sign at the beginning)

// send a formatted notification
$this->slack['CHANNEL'] = [
    'text'        => 'Text of the notification, with <https://temma.net|a link>',
    'attachments' => [
        [
            'pretext' => 'Text above the block',
            'author'  => 'Name of the author of the block',
            'icon'    => 'Block author icon URL',
            'link'    => "URL of the link on the author's name",
            '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
$this->slack->set('CHANNEL', "Message to send");

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

// sends a notification, specifying the author's name and icon
$this->slack->set('CHANNEL', "Message", [
    'author' => "Author's name",
    'icon'   => "https://site.com/path/to/icon.png",
]);

5Specific calls

5.1setAuthor()

// definition of the author's name and icon,
// to be used for all subsequent mailings
$this->slack->setAuthor("Author's name", 'https://site.com/path/to/icon.png');