added Telegram chat topics support

This commit is contained in:
Pavel Muhortov 2023-12-19 22:21:05 +03:00
parent b7e56c407c
commit 1f8607521f
5 changed files with 80 additions and 36 deletions

View File

@ -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*
```

View File

@ -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
)

View File

@ -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

View File

@ -1,3 +1,5 @@
APIKEY={{sendtelegram_apikey}}
APIURL=https://api.telegram.org/bot{{sendtelegram_apikey}}/sendMessage
CHATID={{sendtelegram_chatid}}
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}}

View File

@ -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
sendtelegram_api_key: YOURAPIKEY
sendtelegram_silence: true
sendtelegram_chat_id: -100123456789
sendtelegram_thrd_id: 123