Skip to content

Instantly share code, notes, and snippets.

@Und3rf10w
Created July 23, 2025 23:29
Show Gist options
  • Save Und3rf10w/59a11ad0a93db2f1b32218f401ee1c9d to your computer and use it in GitHub Desktop.
Save Und3rf10w/59a11ad0a93db2f1b32218f401ee1c9d to your computer and use it in GitHub Desktop.
Small python lib to send a hookshot notification
import os
from typing import Literal, Optional
import requests
def send_hookshot_notification(
message: str,
message_content_type: Literal['text', 'html'] = 'text',
url: Optional[str] = None
):
"""Sends a JSON notification payload to a Matrix webhook.
The webhook URL can be passed directly or read from the
MATRIX_HOOKSHOT_URL environment variable.
Args:
message: The message to send.
message_content_type: The content type of the message, either 'text' or 'html'.
Defaults to 'text'.
url: The webhook URL to send the notification to. If not provided,
the function will try to use the MATRIX_HOOKSHOT_URL
environment variable.
Raises:
ValueError: If the URL is not provided as an argument and the
MATRIX_HOOKSHOT_URL environment variable is not set.
"""
# If the URL is not passed as an argument, try to get it from the environment variable.
webhook_url = url or os.getenv("MATRIX_HOOKSHOT_URL")
if webhook_url is None:
raise AttributeError("URL must be provided")
headers = {'Content-Type': 'application/json'}
payload = {"text": f"{message}"}
try:
# Send the POST request with JSON data
response = requests.post(webhook_url, headers=headers, json=payload)
# Raise an HTTPError for bad responses (4xx or 5xx)
response.raise_for_status()
print(f"Notification sent successfully to {url}")
print(f"Status Code: {response.status_code}")
print(f"Response Body: {response.text}")
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
print(
f"Response Body: {response.text if response else 'No response body'}")
except requests.exceptions.ConnectionError as conn_err:
print(f"Connection error occurred: {conn_err}")
except requests.exceptions.Timeout as timeout_err:
print(f"Timeout error occurred: {timeout_err}")
except requests.exceptions.RequestException as req_err:
print(f"An unexpected error occurred: {req_err}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment