Pfsense telegram notification on openvpn access

From Fvettore-WIKI
Revision as of 09:34, 21 July 2025 by Administrator (talk | contribs) (Created page with "== To receive Telegram notifications for OpenVPN connections/disconnections == In the following example: *we have two bot user chatIds (Fabrizio and Banion), respectively 173072004 and 5704842034.</br> *the BOT API key is: 1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I (not a real value, example only)</br> </br> You need to create a bash script named ''openvpn-telegram.sh'' in the pfsense root and give it execution permissions <pre> #!/bin/sh MYDATE=$(/bin/date +'%Y/%m...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

To receive Telegram notifications for OpenVPN connections/disconnections

In the following example:

  • we have two bot user chatIds (Fabrizio and Banion), respectively 173072004 and 5704842034.
  • the BOT API key is: 1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I (not a real value, example only)


You need to create a bash script named openvpn-telegram.sh in the pfsense root and give it execution permissions

 #!/bin/sh MYDATE=$(/bin/date +'%Y/%m/%d %H:%M:%S') if [ "$script_type" = "client-connect" ]; then /usr/local/bin/curl -d "chat_id=173072004" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address $ifconfig_pool_remote_ip." "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" /usr/local/bin/curl -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address $ifconfig_pool_remote_ip." "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" elif [ "$script_type" = "client-disconnect" ]; then /usr/local/bin/curl -d "chat_id=173072004" -d "text=$MYDATE - VPN connection disconnected. Username $common_name " "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" /usr/local/bin/curl -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection disconnected. Username $common_name " "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" fi exit 0 

In the advanced configuration of each server in the custom options field:

 client-connect /root/openvpn-telegram.sh client-disconnect /root/openvpn-telegram.sh script-security 2