# Slack

[Slack](https://slack.com/intl/en-in/) is a popular business communication platform that lets you organize all your business-related chats by specific topics, groups, or direct messaging.

RudderStack supports Slack as a destination where you can send your event data seamlessly.

Find the open source transformer code for this destination in the [GitHub repository](https://github.com/rudderlabs/rudder-transformer/tree/master/v0/destinations/slack).

## Getting started

Before configuring Slack as a destination in RudderStack, verify if the source platform is supported by Slack by referring to the table below:

| **Connection Mode** | **Web**       | **Mobile**    | **Server**    |
| ------------------- | ------------- | ------------- | ------------- |
| **Device mode**     | -             | -             | -             |
| **Cloud** **mode**  | **Supported** | **Supported** | **Supported** |

To know more about the difference between cloud mode and device mode in RudderStack, refer to the [RudderStack Connection Modes](https://rudderstack.com/docs/rudderstack-cloud/rudderstack-connection-modes/) guide.

Once you have confirmed that the platform supports sending events to Slack, perform the steps below:

1. From your [RudderStack dashboard](https://app.rudderstack.com/), add the source. Then, from the list of destinations, select **Slack**.
2. Assign a name to the destination and click on **Next**.

### Connection settings

To successfully configure Slack as a destination, you will need to configure the following settings:

#### Event Channels

![Slack connection settings](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-22272e58b2045fe20a76f8c83c993a54bf97c9c4%2Fslack-settings.png?alt=media)

* **Event Name**: Enter the event name or the regex to match the RudderStack event name.
* **Event Channel**: Enter the name of the Slack channel where the event will be sent. You can specify `#channel_name` or `@user_name`.
* **Regex Matching**: Enable this setting if the event name in the first parameter is a regular expression.

RudderStack adds the global `g` parameter implicitly. Hence, you need not add it with the regex.

#### Identify Template

![Slack connection settings](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-3b49d558c6726cd0cab6215a71d5b10a48b7ceb5%2Fslack-settings4.png?alt=media)

**Identify Template**: Specify the template that you want the `identify` event to be transformed to before it is sent to Slack. The default template is shown below:

```
Identified {{name}} <traits_key1>:<traits_value1> <traits_key2>:<traits_value2> ....
```

Here, the traits key and value are the key-value pairs in the `traits` object of the [`identify`](https://rudderstack.com/docs/rudderstack-api/api-specification/rudderstack-spec/identify/#sample-payload) event payload.

The `name` field in the template is determined from either of the following:

* `traits.name`
* `traits.firstName + traits.lastName`
* `traits.username`
* `properties.email`
* `traits.email`
* `User userId`
* `Anonymous user anonymousId`

Note that only the traits listed in the **Whitelisted Traits** section are considered to be a part of the identify template. The rest of them are sent to Slack.

![Whitelisted traits](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-fbeacc31e58328863119fc93638af1d6c698212f%2Ftraits-slack.png?alt=media)

#### Event Templates

![Event templates settings](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-37a5f81ae17a6317eaaddf80d8eb41d927c025ff%2Fslack-settings2.png?alt=media)

* **Event Name**: Enter the event name or the regex to match the RudderStack event name.
* **Event Template**: Specify the template for the above event names matching the regex. The default template is as shown:

```
{{name}} did {{event}}
```

Here, `name` is determined in the same way as described in the [Identify Template](#identify-template) section above.

* **Regex Matching**: Enable this setting if the event name is a regex in the first parameter.

#### Webhook URL

![Slack connection settings](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-bd169de3785f52fe946cbffd67605f00aa3cd108%2Fslack-settings3.png?alt=media)

**Webhook URL:** Enter your Slack's [incoming webhook URL](https://my.slack.com/services/new/incoming-webhook/).

To obtain the webhook URL, follow the below steps:

1. Click on your Slack workspace name, then go to **Administration** > **Manage Apps**.
2. Search for **Incoming webhooks** in the Slack app directory and add it to Slack by clicking on **Add to Slack**.
3. Select a channel where you want to post the messages forwarded by the webhook and click on **Add Incoming webhooks integration**.

![Slack connection settings](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-03f30558b437ca006e838477a3af5b2856703f80%2Fchoose-channel-slack.png?alt=media)

1. Finally, copy the webhook URL and enter it in the RudderStack dashboard.

![Slack connection settings](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-1c76b5613761975ea359c09508217d6a9b626a9c%2Fwebhook-url-slack.png?alt=media)

## Handlebar expressions

RudderStack supports [Handlebar expression](https://handlebarsjs.com/guide/expressions.html) by leveraging the `handlebars.js` library for including the variables into your dynamic Slack messages.

### Accessible Variables

The following table lists the variables you can select in your template messages:

| Handlebar Expression                     | Output                                                         |
| ---------------------------------------- | -------------------------------------------------------------- |
| `{{name}}`                               | Identity of the user in the priority listed above.             |
| `{{event}}`                              | Event name, that is, `product viewed` or `user login`.         |
| `{{<key_1>}}` or`{{properties.<key_1>}}` | Any of the property values from the event's properties object. |
| `{{propertiesList}}`                     | JSON stringified version of your properties object.            |
| `{{traits}}`                             | JSON stringified version of your traits object.                |
| `{{traitsList.<key_1>}}`                 | Any of the trait values from the event's traits object.        |

### Escaped content

The values returned by the `{{variable}}` are HTML-escaped. For example, if a `variable` has a `&` in its value then it will be returned as `&amp;`. To avoid this behavior, you can use the **triple curly braces** instead of double, like `{{{variable}}}` instead of `{{variable}}`. This will stop the handlebars from escaping a value if there is one, as shown in the following example:

| Expression       | Variable         | Result               |
| ---------------- | ---------------- | -------------------- |
| `{{variable}}`   | `"Marcia & Jan"` | `"Marcia &amp; Jan"` |
| `{{{variable}}}` | `"Marcia & Jan"` | `"Marcia & Jan"`     |

Refer to the [Handlebar document](https://handlebarsjs.com/guide/expressions.html#html-escaping) for more information on the escaped content.

## Identify

The [`identify`](https://rudderstack.com/docs/rudderstack-api/api-specification/rudderstack-spec/identify/) call lets you identify a visiting user and associate them to their actions. It also lets you record the traits about them like their name, email address, etc.

A sample `identify` call for a template, like `Identified {{name}} with {{traits}}`, is as shown:

```javascript
rudderanalytics.identify("1hKOmRA4el9Zt1WSfVJIVo4GRlm", {
  name: "Alex Keener",
  email: "alex@example.com",
  country: "USA",
});
```

As a result, you will see this message `Identified Alex Keener with name: Alex Keener email: alex@example.com country: USA` in your configured Slack channel.

## Track

The [`track`](https://rudderstack.com/docs/rudderstack-api/api-specification/rudderstack-spec/track/) call lets you record the user events, that is, the actions your users perform, along with any properties associated with these actions.

A sample `track` call for a template, like `{{name}} performed {{event}} with {{properties.key1}} {{properties.key2}}` and the following track call, after being identified as above:

```javascript
rudderanalytics.track("test_event", {
  "key1": "test_val1",
  "key2": "test_val2",
  "key3": "test_val3"
})
```

As a result, you will see the message `Alex Keener performed test_event with test_val1 test_val2 test_val3` in your configured Slack channel.

## Contact us

For queries on any of the sections covered in this guide, you can [contact us](mailto:%20docs@rudderstack.com) or start a conversation in our [Slack](https://rudderstack.com/join-rudderstack-slack-community) community.
