Managing workflow approvals manually can be time-consuming and error-prone. Whether you're automating deployments, data pipelines, or internal operations, adding an efficient approval system ensures better control and safety. Automating approvals using shell scripts and Slack provides a streamlined way to handle requests and confirmations in real time, directly where your team already operates.
In this guide, we’ll walk through how to create a shell scripting workflow that connects with Slack for seamless approvals.
Why Combine Shell Scripts with Slack for Approvals?
Shell scripts are a go-to tool for engineers due to their simplicity and flexibility. On the other hand, Slack is a powerful communication hub in modern software workflows. Pairing the two can automate routine processes and add a human approval step without disrupting team productivity.
Key benefits include:
- Increased Speed: Eliminate lag in approval processes.
- Transparency: Centralized notifications and history in Slack.
- Automation: Reduce manual overhead for approvals with pre-configured shell scripts.
Prerequisites
Before diving in, ensure the following:
- A Slack workspace where you have administrative permissions to create or manage Slack apps.
- Basic familiarity with shell scripting.
- Access to Slack bot tokens (via the Slack API) for authentication.
Building Workflow Approvals in Shell Scripting
Step 1: Create a Slack App
- Visit the Slack API dashboard and click "Create New App."
- Select "From scratch"and provide a name for your app.
- Enable the following:
- Bot capabilities.
- Permissions for sending messages (
chat:write) and reading channels (channels:read or chat:read).
Once configured, generate a bot token, which will be used in your script to send messages and receive replies.
Step 2: Structure the Shell Script
Here’s a breakdown of the workflow:
- Send an Approval Request: Post a message to a Slack channel with a Yes/No button interface.
- Wait for a Response: Listen for interactions from Slack’s API to capture the approval or rejection.
- Execute Based on Input: Proceed with the approval step or abort the workflow.
Practical Script Example
Below is a basic example to send a message with buttons to a Slack channel using curl, commonly available in shell environments:
#!/bin/bash
# Define variables
SLACK_TOKEN="xoxb-your-slack-bot-token"
SLACK_CHANNEL="#approvals"
USER_ID="U12345678"# Optional: Direct message to a specific user
# Function to send a message with buttons
send_slack_message() {
curl -X POST -H "Authorization: Bearer $SLACK_TOKEN"\
-H "Content-Type: application/json"\
-d '{
"channel": "'"$SLACK_CHANNEL"'",
"text": "Deployment Request: Approve or Reject?",
"attachments": [
{
"text": "",
"callback_id": "approval_action",
"actions": [
{
"name": "approve",
"text": "Approve",
"type": "button",
"value": "approve"
},
{
"name": "reject",
"text": "Reject",
"type": "button",
"value": "reject"
}
]
}
]
}' https://slack.com/api/chat.postMessage
}
# Call the function
send_slack_message
This script sends a message to a Slack channel with Approve/Reject buttons. Expand it further to handle responses from Slack using /events or /interactivity endpoint setups on your server.
Step 3: Handle Approval Responses
Slack requires you to provide a public endpoint for actions triggered by interactive messages (e.g., button clicks). Use any backend server (Node.js, Flask, etc.) or a lightweight option like ngrok to test locally.
Below is a high-level workflow:
- Capture the payload from Slack when a button is clicked.
- Validate the user who clicked the action, if needed.
- Parse the action (
Approve or Reject) and decide the next steps.
Step 4: Secure Your Script
Always secure Slack tokens and endpoints. Avoid hardcoding secrets in shell scripts. Use environment variables or secret management tools. Additionally, verify signatures from Slack to ensure messages are legitimate.
Taking It Further with hoop.dev
Efficient workflows require tools that are both flexible and scalable. What if, instead of building the entire system manually, you could connect a Slack-based approval workflow to any part of your automation in just a few clicks? Enter hoop.dev.
hoop.dev can help you:
- Automate approvals in Slack without dealing with the complexities of bot tokens or callback registrations.
- Set up secure workflows integrated with your scripts in minutes.
- Stay informed with real-time notifications and a robust audit trail.
See how to connect shell scripts to Slack workflows effortlessly here.