Duplicati and Telegram

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

It will look like this in Telegram Web:

image

And here is my Telegram Notifier bash script for Debian Linux. Don’t forget to chmod +x it before setting it to “run-script-after” in Duplicati.

#!/bin/bash
#
# Script Configuration.
DEBUG_MODE="0"
SCRIPT_FULLFN="$(basename "${0}")"
LOGFILE="/tmp/${SCRIPT_FULLFN%.*}.log"
LOG_MAX_LINES="1000"
SERVICE_NAME="Duplicati"
#
#
# -----------------
# --- Functions ---
# -----------------
logAdd ()
{
	TMP_DATETIME="$(date '+%Y-%m-%d [%H-%M-%S]')"
	TMP_LOGSTREAM="$(tail -n ${LOG_MAX_LINES} ${LOGFILE} 2>/dev/null)"
	echo "${TMP_LOGSTREAM}" > "$LOGFILE"
	if [ "$1" == "-q" ]; then
		#
		# Quiet mode.
		#
		echo "${TMP_DATETIME} ${@:2}" >> "${LOGFILE}"
	else
		#
		# Loud mode.
		#
		echo "${TMP_DATETIME} $*" | tee -a "${LOGFILE}"
	fi
	return
}
sendTelegramNotification ()
{
	#
	# Usage:			sendTelegramNotification "<PN_TEXT>"
	# Example:			sendTelegramNotification "Test push message"
	#
	# Returns:
	#
	# - "0" on FAILURE.
	# - "1" on SUCCESS.
	#
	# Consts.
	CURL_BIN="curl"
	CURL_TIMEOUT="3"
	# 
	# Telegram Bot
	STN_TELEGRAM_BOT_ID="TO_FILL"
	STN_TELEGRAM_BOT_APIKEY="TO_FILL"
	# 
	# 	cf
	STN_TELEGRAM_CHAT_ID="TO_FILL"
	#
	# Variables.
	#
	STN_TEXT="$1"
	STN_TEXT="${STN_TEXT//\"/\\\"}"
	#
	# Log message.
	#
	if [ "${DEBUG_MODE}" == "1" ]; then
		logAdd -q "${MY_SERVICE_NAME}::sendTelegramNotification() fired."
	fi
	#
	# Send push message to Telegram Bot Chat.
	if ( eval ${CURL_BIN} -q \
			--insecure \
			--max-time \""${CURL_TIMEOUT}\"" \
			 "\"https://api.telegram.org/bot${STN_TELEGRAM_BOT_ID}:${STN_TELEGRAM_BOT_APIKEY}/sendMessage?chat_id=${STN_TELEGRAM_CHAT_ID}&text=${STN_TEXT}\"" \
			 2> /dev/null \| grep -Fiq "\"ok\\\":true\"" ); then
		#
		# Log message.
		logAdd -q "${MY_SERVICE_NAME}::sendTelegramNotification: ... SUCCESS."
		# Return SUCCESS.
		#
		echo -n "1"
	else
		#
		# Log message.
		logAdd -q "${MY_SERVICE_NAME}::sendTelegramNotification: ... FAILED."
		#
		# Return FAILURE.
		echo -n "0"
	fi
	#
	return
}
# -----------------------------------------------------
# --------------- END OF FUNCTION BLOCK ---------------
# -----------------------------------------------------
#
# Script Main.
#
# Validate env vars we got from Duplicati.
if  [ -z "${DUPLICATI__backup_name}" ]; then
	export DUPLICATI__backup_name="API_ERR"
fi
#
if  [ -z "${DUPLICATI__PARSED_RESULT}" ]; then
	export DUPLICATI__PARSED_RESULT="API_ERR"
fi
#
if  [ -z "${DUPLICATI__EVENTNAME}" ]; then
	export DUPLICATI__EVENTNAME="API_ERR"
fi
#
if  [ -z "${DUPLICATI__OPERATIONNAME}" ]; then
	export DUPLICATI__OPERATIONNAME="API_ERR"
fi
#
if [ "${DEBUG_MODE}" == "1" ]; then
	env | grep "DUPLICATI__"
fi
#
# Prepare chat message.
CHAT_MESSAGE="${SERVICE_NAME}: \"${DUPLICATI__backup_name}\" ${DUPLICATI__PARSED_RESULT} ${DUPLICATI__EVENTNAME} ${DUPLICATI__OPERATIONNAME}"
#
# Do not send chat message on SUCCESS.
if [ "${DUPLICATI__PARSED_RESULT}" == "Success" ]; then
	logAdd "[INFO] Suppressed sending chat message [${CHAT_MESSAGE}]."
	exit 0
fi
logAdd "[INFO] Sending chat message [${CHAT_MESSAGE}] ..."
#
# Send chat message.
SEND_RESULT="$(sendTelegramNotification "${CHAT_MESSAGE}")"
if [ "${SEND_RESULT}" == "0" ]; then
	logAdd "[ERROR] Failed to send telegram notification."
	exit 99
fi
#
logAdd "[INFO] Successfully sent telegram notification."
exit 0

Hello Catfriend1!
Are two separated files? with ‘cmd’ extension? May I change the name of script files??

Hello @jonathancaldeira

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%

by the contents of the _env.cmd to do so.

1 Like

I was slow to understand that it only sends messages in case of failure, so I set it to send in all cases.
Thank you for your help!

1 Like

@bd8392 thanks for the instruction. Some additions:

  apprise:
    image: caronc/apprise:latest
    container_name: apprise
    ports:
      - 8000:8000
    volumes:
      - /var/lib/apprise/config:/config
    privileged: true

And you should grant the correct access rights for the config folder according to https://hub.docker.com/r/caronc/apprise

1 Like

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.

1 Like