Skip to main content

How to Send a Slack Message from a Batch Script

Integrating messaging alerts into your server scripts transforms silent background processes into proactive, communicative systems. When a backup fails, a disk fills up, or a scheduled task completes, alerting the team immediately in Slack is far more effective than logging to a text file. Using Slack's Incoming Webhooks, a Batch script can easily send messages using curl.exe.

In this guide, we will demonstrate how to post messages to a Slack channel.

The Strategy: Webhooks and curl

  1. Configure an "Incoming Webhook" in your Slack workspace (this gives you a unique URL).
  2. Format your message as a JSON payload ({"text": "Hello world!"}).
  3. Use the native curl.exe command to POST that JSON to the webhook URL.

Setup: Getting the Slack Webhook URL

  1. Go to the Slack API portal and create a new Slack App.
  2. Activate Incoming Webhooks.
  3. Click Add New Webhook to Workspace and select the target channel.
  4. Copy the resulting Webhook URL (e.g., https://hooks.slack.com/services/T000...).

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Define the Webhook URL
set "webhookUrl=https://hooks.slack.com/services/YOUR_WEBHOOK_URL_HERE"

:: 2. Define the Message Variables
set "botName=ServerMonitorBot"
set "emoji=:robot_face:"
set "message=Backup Script Completed Successfully at %TIME%"

:: 3. Construct the JSON Payload
:: Pay attention to escaping double quotes inside the JSON string
set "json={\"username\": \"%botName%\", \"icon_emoji\": \"%emoji%\", \"text\": \"%message%\"}"

echo Sending message to Slack...

:: 4. Send POST Request securely with curl
:: Use curl.exe explicitly to avoid the PowerShell alias conflict
curl.exe -s -o nul -w "%%{http_code}" -X POST -H "Content-type: application/json" -d "%json%" "%webhookUrl%" > "%TEMP%\slack_response.tmp"

:: Capture the exit code immediately
set "curlResult=!errorlevel!"

:: 5. Check curl exit code (0 = request sent successfully)
if !curlResult! neq 0 (
echo [ERROR] curl failed with exit code !curlResult!. Check network connectivity.
pause
exit /b 1
)

:: 6. Check the HTTP status code returned by Slack
set /p "httpCode=" < "%TEMP%\slack_response.tmp"
del "%TEMP%\slack_response.tmp" 2>nul

if "!httpCode!"=="200" (
echo [SUCCESS] Message posted to Slack.
) else (
echo [ERROR] Slack returned HTTP status !httpCode!. Verify webhook URL and payload.
pause
exit /b 1
)

endlocal
pause
exit /b 0

Slack supports blocks and markdown for richer messages. You can include links, bold text, and code blocks directly in the text field.

@echo off
setlocal enabledelayedexpansion

set "webhookUrl=https://hooks.slack.com/services/YOUR_WEBHOOK_URL_HERE"

:: Using markdown syntax: *bold*, _italic_, `code`, <URL|Link text>
:: Use a literal \n for Slack line breaks inside the JSON payload
set "message=*[ALERT]* Disk Space is Critical on Server _DB-PROD-01_\nCheck the logs here: ^<http://monitoring.local/db01^|View Logs^>"

set "payloadFile=%TEMP%\slack_payload.json"

(
echo {
echo "text": "*[ALERT]* Disk Space is Critical on Server _DB-PROD-01_\nCheck the logs here: <http://monitoring.local/db01^|View Logs>"
echo }
) > "%payloadFile%"

curl.exe -s -o nul -w "%%{http_code}" ^
-X POST ^
-H "Content-type: application/json" ^
-d "@%payloadFile%" ^
"%webhookUrl%" > "%TEMP%\slack_response.tmp"

set "curlResult=!errorlevel!"

if !curlResult! neq 0 (
echo [ERROR] curl failed with exit code !curlResult!. Check network connectivity.
pause
exit /b 1
)

set /p "httpCode=" < "%TEMP%\slack_response.tmp"
del "%TEMP%\slack_response.tmp" 2>nul

if "!httpCode!"=="200" (
echo [SUCCESS] Formatted message posted to Slack.
) else (
echo [ERROR] Slack returned HTTP status !httpCode!. Verify webhook URL and payload.
pause
exit /b 1
)

endlocal
pause
exit /b 0

Why Send Slack Messages from Batch?

  1. Immediate Visibility: Instead of relying on email, your operations team sees critical errors instantly on their mobile devices or desktop clients.
  2. Deployment Tracking: Having a build script announce "Version 2.4 deployed to Staging" directly in the #dev-ops channel keeps everyone synchronized.
  3. Low Overhead: Sending a webhook doesn't require setting up SMTP email relays or managing credentials; the URL itself serves as the authorization token.

Important Considerations

  1. Webhook Security: The Webhook URL is essentially a password. If leaked, anyone can post messages to your Slack workspace. Do not upload scripts with live webhook URLs to public repositories (like GitHub).
  2. Escaping Quotes: Batch variables often contain characters that break JSON execution (like backslashes or internal quotes). If your message must contain quotes, you must escape them with a backslash (\") inside the payload.
  3. curl Availability: This relies on Windows 10/11's native curl.exe. On older servers, you must use PowerShell's Invoke-RestMethod instead. Always use the explicit curl.exe filename in Batch scripts to avoid conflicts with PowerShell's curl alias (which maps to Invoke-WebRequest).

Conclusion

Sending Slack messages directly from your Batch scripts bridges the gap between infrastructure automation and human collaboration. By utilizing simple JSON payloads and the native curl command, you transform static administrative tasks into highly communicative processes. This ensures your team is always informed of successes and failures the second they occur.