Skip to content

feat: improve slack notifications with thread and templates #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions workflows/slack/versions/0.0.3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Slack

## Summary

A template that enables you to send messages to a Slack channel using a webhook URL.

## Templates

1. [send-message](https://github.com/codefresh-io/argo-hub/blob/main/workflows/slack/versions/0.0.3/docs/send-message.md)
2. [post-to-channel](docs/post-to-channel.md)

## Security

Minimal required permissions

[Full rbac permissions list](https://github.com/codefresh-io/argo-hub/blob/main/workflows/slack/versions/0.0.3/rbac.yaml)
219 changes: 219 additions & 0 deletions workflows/slack/versions/0.0.3/docs/post-to-channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# post-to-channel

## Summary

Send a message to a Slack channel using its name (or an email address for DM) with a Slack Token. This template supports simple messages, custom templates with attachments, and interactive elements like buttons. It also outputs the thread timestamp for use in subsequent workflow steps.

## Inputs/Outputs

### Inputs

- `SLACK_CHANNEL` (required): Slack channel name (e.g., '#team-channel'). It also accepts user email address or user ID for direct messages
- `SLACK_TOKEN` (required): The secret name containing the Slack bot token. Default is 'slack-token'
- `SLACK_MESSAGE` (optional): The message text to send. Use `<@ID>` to tag users. See [Slack formatting guide](https://api.slack.com/reference/surfaces/formatting) for details
- `SLACK_THREAD_TS` (optional): Thread timestamp to reply in an existing thread
- `SLACK_MESSAGE_TS` (optional): Message timestamp to edit an existing message. When provided, the message will be updated instead of posting a new one
- `SLACK_TEMPLATE_BODY` (optional): JSON string for custom Slack message template/attachment
- `SLACK_TEMPLATE_ACTIONS` (optional): JSON string for interactive elements (buttons, etc.)
- This replaces the actions in the template body
- `SLACK_TEMPLATE_FIELDS` (optional): JSON string for additional fields in the template
- This replaces the fields in the template body
- `LOG_LEVEL` (optional): Logging level (info, debug, warn, error). Default is 'info'

### Outputs

- `message_ts`: The timestamp of the posted message, which can be used as a thread identifier for replies.
- `channel_id`: The ID of the channel where the message was posted, useful for subsequent operations like editing messages.

## Examples

### Simple Message

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: post-to-channel-simple-
spec:
entrypoint: main
templates:
- name: main
steps:
- - name: send-message
templateRef:
name: argo-hub.slack.0.0.3
template: post-to-channel
arguments:
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'Hello from Argo Workflows!'
- name: SLACK_TOKEN
value: slack-token
```

### Direct Message to User

```yaml
parameters:
- name: SLACK_CHANNEL
value: '[email protected]'
- name: SLACK_MESSAGE
value: 'Hi there! This is a direct message.'
- name: SLACK_TOKEN
value: slack-token
```

### Reply in Thread

```yaml
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'This is a reply in the thread'
- name: SLACK_THREAD_TS
value: '1234567890.123456'
- name: SLACK_TOKEN
value: slack-token
```

### Custom Template with Fields

```yaml
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'Deployment status update'
- name: SLACK_TEMPLATE_BODY
value: |
{
"text": "Custom message",
"color": "warning"
}
- name: SLACK_TEMPLATE_FIELDS
value: |
[
{"title": "Status", "value": "Success", "short": true},
{"title": "Environment", "value": "Production", "short": true}
]
- name: SLACK_TOKEN
value: slack-token
```

### Advanced Template with Interactive Actions

```yaml
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'Deployment completed'
- name: SLACK_TEMPLATE_BODY
value: |
{
"text": "Deployment finished",
"color": "good"
}
- name: SLACK_TEMPLATE_ACTIONS
value: |
[
{
"type": "button",
"text": "View Logs",
"url": "https://example.com/logs"
}
]
- name: SLACK_TEMPLATE_FIELDS
value: |
[
{"title": "Environment", "value": "Production", "short": true},
{"title": "Version", "value": "1.2.3", "short": true}
]
- name: SLACK_TOKEN
value: slack-token
```

### Using Thread Timestamp Output

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: post-to-channel-with-reply-
spec:
entrypoint: main
templates:
- name: main
steps:
- - name: initial-message
templateRef:
name: argo-hub.slack.0.0.3
template: post-to-channel
arguments:
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'Starting deployment...'
- name: SLACK_TOKEN
value: slack-token
- - name: reply-message
templateRef:
name: argo-hub.slack.0.0.3
template: post-to-channel
arguments:
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'Deployment completed successfully!'
- name: SLACK_THREAD_TS
value: '{{steps.initial-message.outputs.parameters.message_ts}}'
- name: SLACK_TOKEN
value: slack-token
```

### Editing an Existing Message

> Channel ID is required when editing a message

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: post-to-channel-edit-
spec:
entrypoint: main
templates:
- name: main
steps:
- - name: initial-message
templateRef:
name: argo-hub.slack.0.0.3
template: post-to-channel
arguments:
parameters:
- name: SLACK_CHANNEL
value: 'team-channel'
- name: SLACK_MESSAGE
value: 'Deployment in progress...'
- name: SLACK_TOKEN
value: slack-token
- - name: update-message
templateRef:
name: argo-hub.slack.0.0.3
template: post-to-channel
arguments:
parameters:
- name: SLACK_CHANNEL
value: '{{steps.initial-message.outputs.parameters.channel_id}}'
- name: SLACK_MESSAGE
value: 'Deployment completed successfully! ✅'
- name: SLACK_MESSAGE_TS
value: '{{steps.initial-message.outputs.parameters.message_ts}}'
- name: SLACK_TOKEN
value: slack-token
```
92 changes: 92 additions & 0 deletions workflows/slack/versions/0.0.3/docs/send-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# send-message

## Summary
Send a message to a Slack channel using a hook url

## Inputs/Outputs

### Inputs

### Modes
This template supports three modes, selected via the `MODE` environment variable:
- **simple** (default): Send plain text or custom attachments.
- **template**: Send a custom Slack message using a JSON template body, with optional actions and fields.
- **default-template**: Send a pre-defined Codefresh-style notification with repository and branch info (requires SCM environment variables).

### Inputs
- `SLACK_HOOK_URL` (required): Slack webhook URL
- `SLACK_TEXT` (required for simple/default-template, optional for template): Message text
- `SLACK_THREAD_TS` (optional): Thread timestamp to reply in a thread
- `SLACK_ATTACHMENTS` (optional, available only for simple mode): JSON string for Slack attachments
- `SLACK_USER_NAME` (optional): Username to display
- `SLACK_ICON_EMOJI` (optional): Emoji icon for the message
- `MODE` (optional): Selects the mode (`simple`, `template`, `default-template`). Defaults to `simple`
- `SLACK_TEMPLATE_BODY` (required, available only for template mode): JSON string for the main template
- `SLACK_TEMPLATE_ACTIONS` (optional, available only for template mode): JSON string for Slack actions
- `SLACK_TEMPLATE_FIELDS` (optional, available only for template mode): JSON string for additional fields

> SCM-related variables (required for default-template mode) are automatically provided by Codefresh: `CF_COMMIT_AUTHOR`, `CF_REPO_NAME`, `CF_BRANCH_VERSION_NORMALIZED`, `CF_BRANCH`

> Although the `SLACK_THREAD_TS` is available to send messages in a thread, this workflow does not output the thread timestamp for further use. If you need to capture the thread timestamp when sending a message, consider using the `post-to-channel` template instead, which provides the thread timestamp as an output.


### Outputs

No outputs. This template only sends a message and does not return any values.

## Examples

### task Example
```
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: slack-send-message-
spec:
entrypoint: main
templates:
- name: main
dag:
tasks:
- name: send-message
templateRef:
name: argo-hub.slack.0.0.3
template: send-message
arguments:
parameters:
- name: SLACK_HOOK_URL
value: 'https://hooks.slack.com/services/XXX/YYY/ZZZ'
- name: SLACK_TEXT
value: 'Hello from Codefresh!'
# Optional: send as a reply in a thread
- name: SLACK_THREAD_TS
value: '1234567890.123456'
```

### Advanced Usage Examples

#### Send with custom attachments (simple mode)
```
parameters:
- name: SLACK_HOOK_URL
value: 'https://hooks.slack.com/services/XXX/YYY/ZZZ'
- name: SLACK_TEXT
value: 'Deployment finished.'
- name: SLACK_ATTACHMENTS
value: '[{"color": "good", "text": "All systems go!"}]'
```

#### Send with custom template (template mode)
```
parameters:
- name: SLACK_HOOK_URL
value: 'https://hooks.slack.com/services/XXX/YYY/ZZZ'
- name: MODE
value: 'template'
- name: SLACK_TEMPLATE_BODY
value: '{"text": "Custom message", "color": "warning"}'
- name: SLACK_TEMPLATE_ACTIONS
value: '[{"type": "button", "text": "View"}]'
- name: SLACK_TEMPLATE_FIELDS
value: '[{"title": "Status", "value": "Success"}]'
```
56 changes: 56 additions & 0 deletions workflows/slack/versions/0.0.3/images/post-to-channel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.REST
.idea
.envrc*
# Logs
*.log
jsconfig.json
.vscode

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage/*.html

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript

.env
.coverrun
.coverdata
.nyc_output
.debug
reports
cf-dev-ctrl
.DS_Store
cf-api.sublime-project
cf-api.sublime-workspace
.coveralls.yml
coverage
.idea
*.tar.gz

/nbproject/private/

/server/version.json
/errors/docs/
/errors/docs/errors.html
.vscode

allure-results
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.14.0b4-alpine3.22

COPY requirements.txt /requirements.txt
RUN pip3 install -r /requirements.txt

COPY lib/slack.py /slack/slack.py
ENTRYPOINT [ "python3", "/slack/slack.py" ]
Loading