Pfsense telegram notification on openvpn access: Difference between revisions

From Fvettore-WIKI
No edit summary
No edit summary
 
Line 31: Line 31:
  script-security 2
  script-security 2
</pre>
</pre>
[[Category:OpenVPN]][[Category:Telegram]][[Category:Pfsense]]

Latest revision as of 09:02, 31 July 2025

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