How to Send a Telegram Message from a Batch Script
Telegram offers one of the most developer-friendly bot APIs available. Unlike enterprise tools that require complex OAuth flows, sending a notification to a Telegram group or personal chat simply requires a Bot Token, a Chat ID, and a simple HTTP GET or POST request. By using the native Windows curl.exe command, your Batch script can instantly ping your phone or desktop with critical alerts.
In this guide, we will demonstrate how to send a Telegram message directly from your script.
The Strategy: The Telegram Bot API
- Create a Telegram Bot using the "BotFather" to get a Bot Token.
- Start a chat with your bot to get your Chat ID.
- Use
curl.exeto hit thesendMessageAPI endpoint with your token, chat ID, and message text.
Setup: Getting Your Credentials
- Open Telegram, search for
@BotFather, and send/newbot. - Follow the prompts. At the end, you will receive an API Token (e.g.,
123456789:ABCdefGhIjkL...). - Send a message to your new bot (e.g., "Hello bot").
- Open a browser and go to
https://api.telegram.org/bot<YourTokenHere>/getUpdatesto find your"chat":{"id":12345678...}.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Define your credentials
set "botToken=YOUR_BOT_TOKEN_HERE"
set "chatId=YOUR_CHAT_ID_HERE"
:: 2. Define your message
set "message=Alert: Server backup completed successfully!"
:: 3. Construct the JSON payload
:: Using JSON POST avoids URL-encoding issues with spaces and special characters
set "json={\"chat_id\": \"%chatId%\", \"text\": \"%message%\"}"
echo Sending message via Telegram...
:: 4. Send the request using curl.exe
:: 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%" "https://api.telegram.org/bot%botToken%/sendMessage" > "%TEMP%\telegram_response.tmp"
:: Capture the exit code immediately
set "curlResult=!errorlevel!"
:: 5. Validate the result
if !curlResult! neq 0 (
echo [ERROR] curl failed with exit code !curlResult!. Check network connectivity.
del "%TEMP%\telegram_response.tmp" 2>nul
pause
exit /b 1
)
:: Read the HTTP status code returned by curl
set /p "httpCode=" < "%TEMP%\telegram_response.tmp"
del "%TEMP%\telegram_response.tmp" 2>nul
if "!httpCode!"=="200" (
echo [SUCCESS] Message sent via Telegram.
) else (
echo [ERROR] Telegram API returned HTTP status !httpCode!. Verify your bot token and chat ID.
pause
exit /b 1
)
endlocal
pause
exit /b 0
Advanced Formatting (HTML)
Telegram supports HTML formatting for bolder alerts. You must pass parse_mode set to HTML and ensure your HTML tags are included in the message text. Using JSON POST avoids the complexity of URL-encoding HTML characters in query strings.
@echo off
setlocal enabledelayedexpansion
set "botToken=YOUR_BOT_TOKEN_HERE"
set "chatId=YOUR_CHAT_ID_HERE"
:: HTML formatted message (bold and italic)
set "message=<b>[CRITICAL]</b> Disk space is <i>low</i> on SRV-DB-01."
:: Construct JSON payload with parse_mode
set "json={\"chat_id\": \"%chatId%\", \"parse_mode\": \"HTML\", \"text\": \"%message%\"}"
echo Sending formatted alert via Telegram...
:: 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%" "https://api.telegram.org/bot%botToken%/sendMessage" > "%TEMP%\telegram_response.tmp"
:: Capture the exit code immediately
set "curlResult=!errorlevel!"
if !curlResult! neq 0 (
echo [ERROR] curl failed with exit code !curlResult!. Check network connectivity.
del "%TEMP%\telegram_response.tmp" 2>nul
pause
exit /b 1
)
set /p "httpCode=" < "%TEMP%\telegram_response.tmp"
del "%TEMP%\telegram_response.tmp" 2>nul
if "!httpCode!"=="200" (
echo [SUCCESS] Formatted alert sent via Telegram.
) else (
echo [ERROR] Telegram API returned HTTP status !httpCode!. Verify your bot token and chat ID.
pause
exit /b 1
)
endlocal
pause
exit /b 0
Why Send Telegram Messages?
- Personal Alerts: If you manage a home lab or personal server, getting a script alert directly to your phone via Telegram is totally free and instant.
- No Dedicated Infrastructure: You don't need a mail server or a corporate Slack account. Telegram works over standard HTTPS from any internet-connected machine.
- Group Notifications: By putting the bot in a Telegram Group and using the Group's Chat ID, you can notify an entire IT team simultaneously.
Important Considerations
- URL Encoding vs. JSON POST: The approach of passing the message directly in the URL query string requires spaces to be converted to
%20and special characters to be URL-encoded, which is fragile and error-prone in Batch. Passing the message as a JSON payload (-H "Content-Type: application/json" -d "{...}") is much safer and handles spaces, line breaks, and special characters naturally. - curl Availability: This script uses the native
curl.exeincluded in Windows 10/11. For older servers, a PowerShellInvoke-WebRequestequivalent is required. Always usecurl.exe(the full filename) rather thancurlto avoid conflicts with the PowerShell alias that mapscurltoInvoke-WebRequest. - Security: Your Bot Token allows anyone to send messages as your bot. Do not commit scripts with live tokens to public repositories.
Conclusion
Integrating Telegram into your Batch scripts provides a lightweight, globally accessible notification system. With minimal setup and a single curl.exe command, your automation tools can break out of the local console and push real-time, formatted updates directly to your pockets.