diff --git a/README.md b/README.md index cc97b07..9fb8ab8 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,13 @@ Install E-Mail/Telegram notifications > - Python 3.9 or above on inventory hosts (in repo on Debian | from sources in /usr/local/bin on CentOS) ## Download a Role + ```bash wget -qO- https://git.hmp.today/pavel.muhortov/notification/archive/master.tar.gz | tar -xvz -C . ``` ## Edit Role Variables + ```yaml # nano ./notification/vars/main.yml sendmail_smtp: smtp.host.zone @@ -19,11 +21,17 @@ sendmail_port: 587 sendmail_from: user@host.zone sendmail_pass: pass sendmail_dest: user@host.zone -sendtelegram_apikey: YOURAPIKEY -sendtelegram_chatid: 123456789 +sendtelegram_api_key: YOURAPIKEY +sendtelegram_silence: true +## single chat +sendtelegram_chat_id: 123456789 +## chat topics +sendtelegram_chat_id: -100123456789 +sendtelegram_thrd_id: 123 ``` ## Create Playbook Example + ```yaml tee ./notification.yml > /dev/null <<'EOF' - name: Install E-Mail/Telegram notification @@ -35,11 +43,13 @@ EOF ``` ## Run Playbook + ```bash ansible-playbook ./notification.yml ``` ## Clean + ```bash rm -rf ./notification* ``` diff --git a/templates/sendtelegram-login.sh b/templates/sendtelegram-login.sh index 09cf560..72ee182 100644 --- a/templates/sendtelegram-login.sh +++ b/templates/sendtelegram-login.sh @@ -1,9 +1,24 @@ #! /bin/sh -APIURL="$(cat /usr/local/bin/sendtelegram.config | grep "APIURL=" | cut -d= -f2)" -CHATID="$(cat /usr/local/bin/sendtelegram.config | grep "CHATID=" | cut -d= -f2)" -TXTMSG="[Authorization] $(cat /etc/hostname): +API_URL="$(grep "API_URL=" | cut -d= -f2)" +SILENCE="$(grep "SILENCE=" | cut -d= -f2)" +CHAT_ID="$(grep "CHAT_ID=" | cut -d= -f2)" +THRD_ID="$(grep "THRD_ID=" | cut -d= -f2)" +TXT_MSG="[Authorization] $(cat /etc/hostname): $(w -h)" ( - curl -s -X POST $APIURL -d chat_id=$CHATID -d text="$TXTMSG" >> /dev/null 2>&1 & + if [ -z "${THRD_ID}" ]; then + curl -s -o /dev/null -X POST \ + -d chat_id="${CHAT_ID}" \ + -d text="${TXT_MSG}" \ + -d disable_notification="${SILENCE}" \ + "${API_URL}" + else + curl -s -o /dev/null -X POST \ + -d chat_id="${CHAT_ID}" \ + -d text="$TXT_MSG" \ + -d disable_notification="${SILENCE}" \ + -d message_thread_id="$THRD_ID" \ + "${API_URL}" + fi ) diff --git a/templates/sendtelegram-reboot.sh b/templates/sendtelegram-reboot.sh index 7cf71b5..e06b76b 100644 --- a/templates/sendtelegram-reboot.sh +++ b/templates/sendtelegram-reboot.sh @@ -2,40 +2,55 @@ # chkconfig: 2345 20 80 ### BEGIN INIT INFO -# Provides: sendtelegram-reboot -# Required-Start: $all -# Required-Stop: -# Default-Start: 2 3 4 5 -# Default-Stop: -# Short-Description: Sending a message on startup +# Provides: sendtelegram-reboot +# Required-Start: $all +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Sending a message on startup ### END INIT INFO start() { - APIURL="$(cat /usr/local/bin/sendtelegram.config | grep "APIURL=" | cut -d= -f2)" - CHATID="$(cat /usr/local/bin/sendtelegram.config | grep "CHATID=" | cut -d= -f2)" - TXTMSG="[System States] $(cat /etc/hostname): computer has been rebooted" + API_URL="$(grep "API_URL=" | cut -d= -f2)" + SILENCE="$(grep "SILENCE=" | cut -d= -f2)" + CHAT_ID="$(grep "CHAT_ID=" | cut -d= -f2)" + THRD_ID="$(grep "THRD_ID=" | cut -d= -f2)" + TXT_MSG="[System States] $(cat /etc/hostname): computer has been rebooted" - sleep 15 - curl -s -X POST $APIURL -d chat_id=$CHATID -d text="$TXTMSG" >> /dev/null 2>&1 & + sleep 15 + if [ -z "${THRD_ID}" ]; then + curl -s -o /dev/null -X POST \ + -d chat_id="${CHAT_ID}" \ + -d text="${TXT_MSG}" \ + -d disable_notification="${SILENCE}" \ + "${API_URL}" + else + curl -s -o /dev/null -X POST \ + -d chat_id="${CHAT_ID}" \ + -d text="$TXT_MSG" \ + -d disable_notification="${SILENCE}" \ + -d message_thread_id="$THRD_ID" \ + "${API_URL}" + fi } stop() { - pkill -f curl -s -X POST $APIURL + pkill -f curl -s -o /dev/null -X POST -d chat_id="${CHAT_ID}" } -case "$1" in - start) - start - ;; - stop) - stop - ;; - restart) - stop - start - ;; - *) - echo "Usage: $0 {start|stop|restart}" +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + *) + printf "%s\n" "Usage: $0 {start|stop|restart}" esac exit 0 diff --git a/templates/sendtelegram.config b/templates/sendtelegram.config index 6370ef7..c90d0f1 100644 --- a/templates/sendtelegram.config +++ b/templates/sendtelegram.config @@ -1,3 +1,5 @@ -APIKEY={{sendtelegram_apikey}} -APIURL=https://api.telegram.org/bot{{sendtelegram_apikey}}/sendMessage -CHATID={{sendtelegram_chatid}} \ No newline at end of file +API_KEY={{sendtelegram_api_key}} +API_URL=https://api.telegram.org/bot{{sendtelegram_api_key}}/sendMessage +SILENCE={{sendtelegram_silence}} +CHAT_ID={{sendtelegram_chat_id}} +THRD_ID={{sendtelegram_thrd_id}} \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index 5d9e950..efcfa7e 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -4,5 +4,7 @@ sendmail_port: 587 sendmail_from: user@host.zone sendmail_pass: pass sendmail_dest: user@host.zone -sendtelegram_apikey: YOURAPIKEY -sendtelegram_chatid: 123456789 \ No newline at end of file +sendtelegram_api_key: YOURAPIKEY +sendtelegram_silence: true +sendtelegram_chat_id: -100123456789 +sendtelegram_thrd_id: 123 \ No newline at end of file