I’ve made this batch script for sending telegram messages on failure of a backup job. You just need to fill in your TELEGRAM_BOT_ID, TELEGRAM_BOT_APIKEY and TELEGRAM_CHAT_ID.
duplicati_post_telegram.cmd:
@echo off
setlocal enabledelayedexpansion
REM
REM Script Configuration.
SET SERVICE_NAME=Duplicati
REM
REM Runtime Variables.
SET SCRIPT_ENV_FULLFN="%~dpn0_env.cmd"
SET LOGFILE="%TEMP%\%~n0.log"
REM
REM Validate env vars we got from Duplicati.
IF NOT DEFINED DUPLICATI__backup_name SET DUPLICATI__backup_name=API_ERR
IF NOT DEFINED DUPLICATI__PARSED_RESULT SET DUPLICATI__PARSED_RESULT=API_ERR
IF NOT DEFINED DUPLICATI__EVENTNAME SET DUPLICATI__EVENTNAME=API_ERR
IF NOT DEFINED DUPLICATI__OPERATIONNAME SET DUPLICATI__OPERATIONNAME=API_ERR
REM
REM Prepare chat message.
SET "CHAT_MESSAGE=%SERVICE_NAME%: \"%DUPLICATI__backup_name%\" %DUPLICATI__PARSED_RESULT% %DUPLICATI__EVENTNAME% %DUPLICATI__OPERATIONNAME%"
REM
REM Do not send chat message on SUCCESS.
IF /I "%DUPLICATI__PARSED_RESULT%" == "Success" call :logAdd "[INFO] Suppressed sending chat message [%CHAT_MESSAGE:\='%]" & goto :eof
call :logAdd "[INFO] Sending chat message [%CHAT_MESSAGE:\='%] ..."
REM
REM Get command-line parameters
IF EXIST %SCRIPT_ENV_FULLFN% call %SCRIPT_ENV_FULLFN%
REM
IF NOT DEFINED TELEGRAM_BOT_ID SET TELEGRAM_BOT_ID=%1
IF DEFINED TELEGRAM_BOT_ID SET TELEGRAM_BOT_ID=%TELEGRAM_BOT_ID:"=%
REM
IF NOT DEFINED TELEGRAM_BOT_APIKEY SET TELEGRAM_BOT_APIKEY=%2
IF DEFINED TELEGRAM_BOT_APIKEY SET TELEGRAM_BOT_APIKEY=%TELEGRAM_BOT_APIKEY:"=%
REM
IF NOT DEFINED TELEGRAM_CHAT_ID SET TELEGRAM_CHAT_ID=%3
IF DEFINED TELEGRAM_CHAT_ID SET TELEGRAM_CHAT_ID=%TELEGRAM_CHAT_ID:"=%
REM
REM Validate command-line parameters.
IF NOT DEFINED TELEGRAM_BOT_ID call :logAdd "[ERROR] Parameter #1 TELEGRAM_BOT_ID is missing." & goto :eof
IF NOT DEFINED TELEGRAM_BOT_APIKEY call :logAdd "[ERROR] Parameter #2 TELEGRAM_BOT_APIKEY is missing." & goto :eof
IF NOT DEFINED TELEGRAM_CHAT_ID call :logAdd "[ERROR] Parameter #3 TELEGRAM_CHAT_ID is missing." & goto :eof
REM
REM Send chat message.
powershell -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "(Invoke-WebRequest -uri 'https://api.telegram.org/bot%TELEGRAM_BOT_ID%:%TELEGRAM_BOT_APIKEY%/sendMessage?chat_id=%TELEGRAM_CHAT_ID%&text=%CHAT_MESSAGE%').Content" 2>> %LOGFILE:.log=.err% | findstr /i /c:"\"ok\":true" && call :logAdd "[INFO] Successfully sent telegram notification." & goto :eof
call :logAdd "[ERROR] Failed to send telegram notification."
REM
goto :eof
:logAdd
REM Syntax:
REM logAdd [TEXT]
SET LOG_TEXT=%1
SET LOG_TEXT=%LOG_TEXT:"=%
SET LOG_DATETIMESTAMP=%DATE:~-4%-%DATE:~-7,-5%-%DATE:~-10,-8%_%time:~-11,2%:%time:~-8,2%:%time:~-5,2%
SET LOG_DATETIMESTAMP=%LOG_DATETIMESTAMP: =0%
echo %LOG_DATETIMESTAMP%: %LOG_TEXT%
echo %LOG_DATETIMESTAMP%: %LOG_TEXT% >> "%LOGFILE%"
goto :eof
duplicati_post_telegram_env.cmd:
@echo off
REM
REM Script Configuration.
SET TELEGRAM_BOT_ID=XXX
SET TELEGRAM_BOT_APIKEY=YYY
SET TELEGRAM_CHAT_ID=ZZZ
REM
goto :eof
the two cms files need to be placed in the same directory. You can rename them as long as you understood the relation given in the batch script between them.
E.g.
main.cmd
main_env.cmd
The _env script is just holding your personal config params. You could also put the _env SET lines into the main cmd file to have it all in one file. Replace the line
IF EXIST %SCRIPT_ENV_FULLFN% call %SCRIPT_ENV_FULLFN%
Welcome to the forum @aleksej.kuznecow and thanks for contributing the addition.
It’s wonderful to see the community helping to build what is truly a community effort.