github.com/argoproj/argo-events@v1.9.1/docs/sensors/triggers/slack-trigger.md (about)

     1  # Slack Trigger
     2  
     3  The Slack trigger is used to send a custom message to a desired Slack channel in a Slack workspace. The intended use is for notifications for a build pipeline, but can be used for any notification scenario.
     4  
     5  ## Prerequisite
     6  
     7  1. Deploy the eventbus in the namespace.
     8  
     9  1. Make sure to have a Slack workspace setup you wish to send a message to.
    10  
    11  2. Create a webhook event-source.
    12  
    13          kubectl -n argo-events apply -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/event-sources/webhook.yaml
    14  
    15  3. Set up port-forwarding to expose the http server. We will
    16     use port-forwarding here.
    17  
    18          kubectl port-forward -n argo-events <event-source-pod-name> 12000:12000
    19  
    20  ## Create a Slack App
    21  
    22  We need to create a Slack App which will send messages to your Slack Workspace. We will add OAuth Permissions and add the OAuth token to the k8s cluster via a secret.
    23  
    24  1. Create a Slack app by clicking `Create New App` at the [Slack API Page](https://api.slack.com/apps). Name your app and choose your intended Slack Workspace.
    25  
    26  2. Navigate to your app, then to `Features > OAuth & Permissions`.
    27  
    28  3. Scroll down to `Scopes` and add the scopes `channels:join`, `channels:read`, `groups:read` and `chat:write` to the _Bot Token Scopes_.
    29  
    30  4. Scroll to the top of the `OAuth & Permissions` page and click `Install App to Workspace` and follow the install Wizard.
    31  
    32  5. You should land back on the `OAuth & Permissions` page. Copy your app's OAuth Access Token. This will allow the trigger to act on behalf of your newly created Slack app.
    33  
    34  6. Create a kubernetes secret with the OAuth token in your cluster.
    35  
    36          kubectl create secret generic slack-secret --from-literal=token=$SLACK_OAUTH_TOKEN
    37  
    38  ## Slack Trigger
    39  
    40  We will set up a basic slack trigger and send a default message, and then a dynamic custom message.
    41  
    42  1. Create a sensor with Slack trigger. We will discuss the trigger details in the following sections.
    43  
    44          kubectl -n argo-events apply -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/sensors/slack-trigger.yaml
    45  
    46  2. Send a http request to the event-source-pod to fire the Slack trigger.
    47  
    48          curl -d '{"text":"Hello, World!"}' -H "Content-Type: application/json" -X POST http://localhost:12000/example
    49  
    50        **Note**: The default slack-trigger will send the message "hello world" to the #general channel. You may change the default message and channel in slack-trigger.yaml under triggers.slack.channel and triggers.slack.message.
    51  
    52  3. Alternatively, you can dynamically determine the channel and message based on parameterization of your event.
    53  
    54          curl -d '{"channel":"random","message":"test message"}' -H "Content-Type: application/json" -X POST http://localhost:12000/example
    55  
    56  4. Great! But, how did the sensor use the event to customize the message and channel from the http request? We will see that in next section.
    57  
    58  ## Parameterization
    59  
    60  The slack trigger parameters have the following structure,
    61  
    62          parameters:
    63            - src:
    64                dependencyName: test-dep
    65                dataKey: body.channel
    66              dest: slack.channel
    67            - src:
    68                dependencyName: test-dep
    69                contextKey: body.message
    70              dest: slack.message
    71  
    72  The `src` is the source of event. It contains,
    73  
    74    1. `dependencyName`: name of the event dependency to extract the event from.
    75    2. `dataKey`: to extract a particular key-value from event's data.
    76    3. `contextKey`: to extract a particular key-value from event' context.
    77  
    78  The `dest` is the destination key within the result payload.
    79  
    80  So, the above trigger parameters will generate a request payload as,
    81  
    82          {
    83              "channel": "channel_to_send_message",
    84              "message": "message_to_send_to_channel"
    85          }
    86  
    87  **_Note_**: If you define both the `contextKey` and `dataKey` within a parameter item, then
    88  the `dataKey` takes the precedence.
    89  
    90  You can create any parameter structure you want. To get more info on how to
    91  generate complex event payloads, take a look at [this library](https://github.com/tidwall/sjson).
    92  
    93  ## Other Capabilities
    94  
    95  #### Configuring the sender of the Slack message: 
    96  
    97           - template:
    98                name: slack-trigger
    99                slack:
   100                  sender:
   101                    username: "Cool Robot"
   102                    icon: ":robot_face:" # emoji or url, e.g. https://example.com/image.png
   103  
   104  #### Sending messages to Slack threads:
   105  
   106           - template:
   107                name: slack-trigger
   108                slack:
   109                  thread:
   110                    messageAggregationKey: "abcdefg" # aggregate message by some key to send them to the same Slack thread
   111                    broadcastMessageToChannel: true # also broadcast the message from the thread to the channel
   112  
   113  #### Sending attachments using [Slack Attachments API](https://api.slack.com/reference/messaging/attachments):
   114  
   115           - template:
   116                name: slack-trigger
   117                slack:
   118                  message: "hello world!"
   119                  attachments: |
   120                    [{
   121                      "title": "Attachment1!",
   122                      "title_link": "https://argoproj.github.io/argo-events/sensors/triggers/slack-trigger/",
   123                      "color": "#18be52",
   124                      "fields": [{
   125                        "title": "Hello1",
   126                        "value": "Hello World1",
   127                        "short": true
   128                      }, {
   129                        "title": "Hello2",
   130                        "value": "Hello World2",
   131                        "short": true
   132                      }]
   133                    }, {
   134                      "title": "Attachment2!",
   135                      "title_link": "https://argoproj.github.io/argo-events/sensors/triggers/slack-trigger/",
   136                      "color": "#18be52",
   137                      "fields": [{
   138                        "title": "Hello1",
   139                        "value": "Hello World1",
   140                        "short": true
   141                      }, {
   142                        "title": "Hello2",
   143                        "value": "Hello World2",
   144                        "short": true
   145                      }]
   146                    }]
   147  
   148  #### Sending blocks using [Slack Blocks API](https://api.slack.com/reference/block-kit/blocks):
   149  
   150           - template:
   151                name: slack-trigger
   152                slack:
   153                  blocks: |
   154                    [{
   155                      "type": "actions",
   156                      "block_id": "actionblock789",
   157                      "elements": [{
   158                          "type": "datepicker",
   159                          "action_id": "datepicker123",
   160                          "initial_date": "1990-04-28",
   161                          "placeholder": {
   162                            "type": "plain_text",
   163                            "text": "Select a date"
   164                          }
   165                        },
   166                        {
   167                          "type": "overflow",
   168                          "options": [{
   169                              "text": {
   170                                "type": "plain_text",
   171                                "text": "*this is plain_text text*"
   172                              },
   173                              "value": "value-0"
   174                            },
   175                            {
   176                              "text": {
   177                                "type": "plain_text",
   178                                "text": "*this is plain_text text*"
   179                              },
   180                              "value": "value-1"
   181                            },
   182                            {
   183                              "text": {
   184                                "type": "plain_text",
   185                                "text": "*this is plain_text text*"
   186                              },
   187                              "value": "value-2"
   188                            },
   189                            {
   190                              "text": {
   191                                "type": "plain_text",
   192                                "text": "*this is plain_text text*"
   193                              },
   194                              "value": "value-3"
   195                            },
   196                            {
   197                              "text": {
   198                                "type": "plain_text",
   199                                "text": "*this is plain_text text*"
   200                              },
   201                              "value": "value-4"
   202                            }
   203                          ],
   204                          "action_id": "overflow"
   205                        },
   206                        {
   207                          "type": "button",
   208                          "text": {
   209                            "type": "plain_text",
   210                            "text": "Click Me"
   211                          },
   212                          "value": "click_me_123",
   213                          "action_id": "button"
   214                        }
   215                      ]
   216                    }]
   217  
   218  The complete specification of Slack trigger is available [here](https://github.com/argoproj/argo-events/blob/master/api/sensor.md#slacktrigger).