Python is used across a wide range of environments — system administration, DevOps, web applications, data processing. When a critical event occurs in these environments — a service crashes, an error threshold is exceeded, a process fails — the notification usually goes to email or Slack. If nobody is watching at night, that is not enough.
Calling the Alertalk webhook from Python is a matter of a few lines of code. If the requests library is installed, you can start immediately.
Basic Webhook Call
import requests
WEBHOOK_URL = "https://tr.alertalk.net/webhook/receive"
WEBHOOK_KEY = "your-alertalk-key-here"
def send_voice_alert(phone, message):
headers = {
"X-Webhook-Key": WEBHOOK_KEY,
"Content-Type": "application/json"
}
payload = {
"phone_number": phone,
"error_message": message
}
response = requests.post(WEBHOOK_URL, json=payload, headers=headers)
return response.status_code
This function can be added anywhere in your project. Call it when a critical condition is met.
Server Reachability Check
A script that checks whether a server or service is reachable and triggers a phone call if it is not:
import requests
import socket
WEBHOOK_URL = "https://tr.alertalk.net/webhook/receive"
WEBHOOK_KEY = "your-alertalk-key-here"
PHONE = "5XXXXXXXXX"
def send_voice_alert(message):
headers = {
"X-Webhook-Key": WEBHOOK_KEY,
"Content-Type": "application/json"
}
payload = {
"phone_number": PHONE,
"error_message": message
}
requests.post(WEBHOOK_URL, json=payload, headers=headers)
def check_server(host, port=80, timeout=5):
try:
sock = socket.create_connection((host, port), timeout=timeout)
sock.close()
return True
except (socket.timeout, ConnectionRefusedError, OSError):
return False
servers = [
{"host": "192.168.1.10", "port": 80, "name": "Web Server"},
{"host": "192.168.1.20", "port": 3306, "name": "Database Server"},
{"host": "192.168.1.30", "port": 443, "name": "API Server"},
]
for server in servers:
if not check_server(server["host"], server["port"]):
send_voice_alert(
f"{server['name']} is unreachable. "
f"IP: {server['host']}, Port: {server['port']}"
)
Catching Errors in Web Applications
Connecting critical exceptions to voice alerts in Flask or Django applications:
import requests
import traceback
WEBHOOK_URL = "https://tr.alertalk.net/webhook/receive"
WEBHOOK_KEY = "your-alertalk-key-here"
PHONE = "5XXXXXXXXX"
def send_voice_alert(message):
headers = {
"X-Webhook-Key": WEBHOOK_KEY,
"Content-Type": "application/json"
}
payload = {
"phone_number": PHONE,
"error_message": message
}
try:
requests.post(WEBHOOK_URL, json=payload, headers=headers, timeout=10)
except Exception:
pass
def critical_operation():
try:
# Your critical operation here
raise ValueError("Database connection lost")
except Exception as e:
send_voice_alert(f"Critical error: {str(e)}")
raise
critical_operation()
CPU and Memory Monitoring
Monitoring system resources with psutil and sending an alert when a threshold is exceeded:
import requests
import psutil
import socket
WEBHOOK_URL = "https://tr.alertalk.net/webhook/receive"
WEBHOOK_KEY = "your-alertalk-key-here"
PHONE = "5XXXXXXXXX"
CPU_THRESHOLD = 90
MEM_THRESHOLD = 85
HOSTNAME = socket.gethostname()
def send_voice_alert(message):
headers = {
"X-Webhook-Key": WEBHOOK_KEY,
"Content-Type": "application/json"
}
payload = {
"phone_number": PHONE,
"error_message": message
}
requests.post(WEBHOOK_URL, json=payload, headers=headers)
cpu = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory().percent
if cpu >= CPU_THRESHOLD:
send_voice_alert(
f"CPU alert: CPU usage on {HOSTNAME} is at {cpu} percent."
)
if memory >= MEM_THRESHOLD:
send_voice_alert(
f"Memory alert: Memory usage on {HOSTNAME} is at {memory} percent."
)
Running Automatically with Cron
Add the script to cron to run every 5 minutes:
*/5 * * * * /usr/bin/python3 /opt/scripts/system_alert.py >> /var/log/system_alert.log 2>&1
Python Alert Scenarios
| Scenario | Library | Difficulty |
|---|---|---|
| HTTP service check | requests | Easy |
| Port reachability | socket | Easy |
| CPU / memory monitoring | psutil | Easy |
| Database connection | psycopg2 / pymysql | Medium |
| Log file monitoring | watchdog | Medium |
| Application error catching | try/except | Easy |
Who Is This For?
Ideal for developers, DevOps engineers and system administrators who work with Python. A few lines of code added to an existing Python script is all it takes to set up voice alerting. Particularly useful for custom monitoring scenarios or application-level alerts where a full monitoring stack is not justified.
Setup Guide
For creating an Alertalk account and getting your webhook key: