Files
dotfiles/python/signal_alert.py
T
Richie fb58bac89d
treefmt / nix fmt (pull_request) Successful in 3s
build_systems / build-portal-1 (pull_request) Successful in 22s
build_systems / build-bob (pull_request) Successful in 45s
build_systems / build-brain (pull_request) Successful in 48s
build_systems / build-jeeves (pull_request) Successful in 50s
build_systems / build-rhapsody-in-green (pull_request) Successful in 55s
test ebook search / test-ebook-search (pull_request) Successful in 1m7s
treefmt / nix fmt (push) Successful in 2s
build_systems / build-portal-1 (push) Successful in 17s
build_systems / build-bob (push) Successful in 29s
build_systems / build-brain (push) Successful in 29s
build_systems / build-jeeves (push) Successful in 34s
build_systems / build-rhapsody-in-green (push) Successful in 38s
test ebook search / test-ebook-search (push) Successful in 1m6s
refactor(signal): replace Apprise with httpx
2026-09-19 22:40:45 -04:00

51 lines
1.4 KiB
Python

"""signal_alert."""
from __future__ import annotations
import logging
from os import getenv
import httpx
logger = logging.getLogger(__name__)
SIGNAL_API_URL = "http://localhost:8989/v2/send"
SIGNAL_API_TIMEOUT = 4.0
def signal_alert(body: str, title: str = "") -> None:
"""Send a signal alert.
Args:
body (str): The body of the alert.
title (str, optional): The title of the alert. Defaults to "".
"""
from_phone = getenv("SIGNAL_ALERT_FROM_PHONE")
to_phone = getenv("SIGNAL_ALERT_TO_PHONE")
if not from_phone or not to_phone:
logger.info("SIGNAL_ALERT_FROM_PHONE or SIGNAL_ALERT_TO_PHONE not set")
return
# Apprise's Signal integration did not support titles, so preserve that behavior.
if title:
logger.debug("Signal does not support notification titles; ignoring title")
try:
response = httpx.post(
SIGNAL_API_URL,
json={
"message": body,
"number": from_phone,
"recipients": [to_phone],
"text_mode": "normal",
},
timeout=SIGNAL_API_TIMEOUT,
follow_redirects=True,
)
except httpx.HTTPError:
logger.exception("Unable to contact the Signal API")
return
if response.status_code not in {httpx.codes.OK, httpx.codes.CREATED}:
logger.error("Signal API returned HTTP status %d", response.status_code)