added Telegram chat topics support
This commit is contained in:
		
							parent
							
								
									b7e56c407c
								
							
						
					
					
						commit
						1f8607521f
					
				
							
								
								
									
										14
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								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)
 | 
					> - Python 3.9 or above on inventory hosts (in repo on Debian | from sources in /usr/local/bin on CentOS)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Download a Role
 | 
					## Download a Role
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
wget -qO- https://git.hmp.today/pavel.muhortov/notification/archive/master.tar.gz | tar -xvz -C .
 | 
					wget -qO- https://git.hmp.today/pavel.muhortov/notification/archive/master.tar.gz | tar -xvz -C .
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Edit Role Variables
 | 
					## Edit Role Variables
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```yaml
 | 
					```yaml
 | 
				
			||||||
# nano ./notification/vars/main.yml
 | 
					# nano ./notification/vars/main.yml
 | 
				
			||||||
sendmail_smtp: smtp.host.zone
 | 
					sendmail_smtp: smtp.host.zone
 | 
				
			||||||
| 
						 | 
					@ -19,11 +21,17 @@ sendmail_port: 587
 | 
				
			||||||
sendmail_from: user@host.zone
 | 
					sendmail_from: user@host.zone
 | 
				
			||||||
sendmail_pass: pass
 | 
					sendmail_pass: pass
 | 
				
			||||||
sendmail_dest: user@host.zone
 | 
					sendmail_dest: user@host.zone
 | 
				
			||||||
sendtelegram_apikey: YOURAPIKEY
 | 
					sendtelegram_api_key: YOURAPIKEY
 | 
				
			||||||
sendtelegram_chatid: 123456789
 | 
					sendtelegram_silence: true
 | 
				
			||||||
 | 
					## single chat
 | 
				
			||||||
 | 
					sendtelegram_chat_id: 123456789
 | 
				
			||||||
 | 
					## chat topics
 | 
				
			||||||
 | 
					sendtelegram_chat_id: -100123456789
 | 
				
			||||||
 | 
					sendtelegram_thrd_id: 123
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Create Playbook Example
 | 
					## Create Playbook Example
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```yaml
 | 
					```yaml
 | 
				
			||||||
tee ./notification.yml > /dev/null <<'EOF'
 | 
					tee ./notification.yml > /dev/null <<'EOF'
 | 
				
			||||||
- name: Install E-Mail/Telegram notification
 | 
					- name: Install E-Mail/Telegram notification
 | 
				
			||||||
| 
						 | 
					@ -35,11 +43,13 @@ EOF
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Run Playbook
 | 
					## Run Playbook
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
ansible-playbook ./notification.yml
 | 
					ansible-playbook ./notification.yml
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Clean
 | 
					## Clean
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
rm -rf ./notification*
 | 
					rm -rf ./notification*
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,9 +1,24 @@
 | 
				
			||||||
#! /bin/sh
 | 
					#! /bin/sh
 | 
				
			||||||
APIURL="$(cat /usr/local/bin/sendtelegram.config | grep "APIURL=" | cut -d= -f2)"
 | 
					API_URL="$(grep "API_URL=" | cut -d= -f2)"
 | 
				
			||||||
CHATID="$(cat /usr/local/bin/sendtelegram.config | grep "CHATID=" | cut -d= -f2)"
 | 
					SILENCE="$(grep "SILENCE=" | cut -d= -f2)"
 | 
				
			||||||
TXTMSG="[Authorization] $(cat /etc/hostname):
 | 
					CHAT_ID="$(grep "CHAT_ID=" | cut -d= -f2)"
 | 
				
			||||||
 | 
					THRD_ID="$(grep "THRD_ID=" | cut -d= -f2)"
 | 
				
			||||||
 | 
					TXT_MSG="[Authorization] $(cat /etc/hostname):
 | 
				
			||||||
$(w -h)"
 | 
					$(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
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,40 +2,55 @@
 | 
				
			||||||
# chkconfig: 2345 20 80
 | 
					# chkconfig: 2345 20 80
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### BEGIN INIT INFO
 | 
					### BEGIN INIT INFO
 | 
				
			||||||
# Provides:		sendtelegram-reboot
 | 
					# Provides:          sendtelegram-reboot
 | 
				
			||||||
# Required-Start:	$all
 | 
					# Required-Start:    $all
 | 
				
			||||||
# Required-Stop:	
 | 
					# Required-Stop:
 | 
				
			||||||
# Default-Start:	2 3 4 5
 | 
					# Default-Start:     2 3 4 5
 | 
				
			||||||
# Default-Stop:		
 | 
					# Default-Stop:
 | 
				
			||||||
# Short-Description:	Sending a message on startup
 | 
					# Short-Description: Sending a message on startup
 | 
				
			||||||
### END INIT INFO
 | 
					### END INIT INFO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
start() {
 | 
					start() {
 | 
				
			||||||
    APIURL="$(cat /usr/local/bin/sendtelegram.config | grep "APIURL=" | cut -d= -f2)"
 | 
					  API_URL="$(grep "API_URL=" | cut -d= -f2)"
 | 
				
			||||||
    CHATID="$(cat /usr/local/bin/sendtelegram.config | grep "CHATID=" | cut -d= -f2)"
 | 
					  SILENCE="$(grep "SILENCE=" | cut -d= -f2)"
 | 
				
			||||||
    TXTMSG="[System States] $(cat /etc/hostname): computer has been rebooted"
 | 
					  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
 | 
					  sleep 15
 | 
				
			||||||
    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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
stop() {
 | 
					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 
 | 
					case "$1" in
 | 
				
			||||||
    start)
 | 
					  start)
 | 
				
			||||||
       start
 | 
					    start
 | 
				
			||||||
       ;;
 | 
					    ;;
 | 
				
			||||||
    stop)
 | 
					  stop)
 | 
				
			||||||
       stop
 | 
					    stop
 | 
				
			||||||
       ;;
 | 
					    ;;
 | 
				
			||||||
    restart)
 | 
					  restart)
 | 
				
			||||||
       stop
 | 
					    stop
 | 
				
			||||||
       start
 | 
					    start
 | 
				
			||||||
       ;;
 | 
					    ;;
 | 
				
			||||||
    *)
 | 
					  *)
 | 
				
			||||||
       echo "Usage: $0 {start|stop|restart}"
 | 
					  printf "%s\n" "Usage: $0 {start|stop|restart}"
 | 
				
			||||||
esac
 | 
					esac
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exit 0
 | 
					exit 0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
APIKEY={{sendtelegram_apikey}}
 | 
					API_KEY={{sendtelegram_api_key}}
 | 
				
			||||||
APIURL=https://api.telegram.org/bot{{sendtelegram_apikey}}/sendMessage
 | 
					API_URL=https://api.telegram.org/bot{{sendtelegram_api_key}}/sendMessage
 | 
				
			||||||
CHATID={{sendtelegram_chatid}}
 | 
					SILENCE={{sendtelegram_silence}}
 | 
				
			||||||
 | 
					CHAT_ID={{sendtelegram_chat_id}}
 | 
				
			||||||
 | 
					THRD_ID={{sendtelegram_thrd_id}}
 | 
				
			||||||
| 
						 | 
					@ -4,5 +4,7 @@ sendmail_port: 587
 | 
				
			||||||
sendmail_from: user@host.zone
 | 
					sendmail_from: user@host.zone
 | 
				
			||||||
sendmail_pass: pass
 | 
					sendmail_pass: pass
 | 
				
			||||||
sendmail_dest: user@host.zone
 | 
					sendmail_dest: user@host.zone
 | 
				
			||||||
sendtelegram_apikey: YOURAPIKEY
 | 
					sendtelegram_api_key: YOURAPIKEY
 | 
				
			||||||
sendtelegram_chatid: 123456789
 | 
					sendtelegram_silence: true
 | 
				
			||||||
 | 
					sendtelegram_chat_id: -100123456789
 | 
				
			||||||
 | 
					sendtelegram_thrd_id: 123
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user