Python Integration
Send notifications using the requests library.
Basic Usage
import requests
WEBHOOK_URL = 'YOUR_WEBHOOK_URL'
def send_notification(title, body=None, priority='normal'):
payload = {'title': title, 'priority': priority}
if body:
payload['body'] = body
response = requests.post(WEBHOOK_URL, json=payload)
return response.json()
# Usage
send_notification('Script Finished', 'Processed 1,000 records')
send_notification('Error', 'Task failed with exit code 1', 'high')With Error Handling
import requests
import os
def notify(title, body=None, priority='normal', actions=None):
try:
payload = {
'title': title,
'priority': priority
}
if body:
payload['body'] = body
if actions:
payload['actions'] = actions
response = requests.post(
os.environ['PING_WEBHOOK_URL'],
json=payload,
timeout=10
)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
print(f'Failed to send notification: {e}')
return NoneWith Actions
notify(
title='Build Complete',
body='All tests passed',
actions=[
{'type': 'url', 'label': 'View Build', 'value': 'https://github.com/org/repo/actions'}
]
)Context Manager for Scripts
import requests
import os
import sys
import traceback
from contextlib import contextmanager
@contextmanager
def ping_on_complete(success_title, failure_title, webhook_url=None):
"""Send a Ping notification when the script completes."""
url = webhook_url or os.environ.get('PING_WEBHOOK_URL')
try:
yield
requests.post(url, json={'title': success_title}, timeout=5)
except Exception as e:
requests.post(url, json={
'title': failure_title,
'body': f'{type(e).__name__}: {e}',
'priority': 'high'
}, timeout=5)
raise
# Usage
with ping_on_complete('Script completed', 'Script failed'):
# Your script logic here
process_data()Decorator for Functions
import functools
import requests
import os
def notify_on_error(title='Function Error'):
"""Decorator to send notification on function errors."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
requests.post(
os.environ['PING_WEBHOOK_URL'],
json={
'title': title,
'body': f'{func.__name__}: {e}',
'priority': 'high'
},
timeout=5
)
raise
return wrapper
return decorator
# Usage
@notify_on_error('Data processing failed')
def process_data():
# Your code here
pass