CoolFace
Apppublic

iridescentX/openui

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
webhook.py55 linesDownload Raw Back to utils
1import json2import requests3import logging4 5from config import SRC_LOG_LEVELS, VERSION, WEBUI_FAVICON_URL, WEBUI_NAME6 7log = logging.getLogger(__name__)8log.setLevel(SRC_LOG_LEVELS["WEBHOOK"])9 10 11def post_webhook(url: str, message: str, event_data: dict) -> bool:12    try:13        payload = {}14 15        # Slack and Google Chat Webhooks16        if "https://hooks.slack.com" in url or "https://chat.googleapis.com" in url:17            payload["text"] = message18        # Discord Webhooks19        elif "https://discord.com/api/webhooks" in url:20            payload["content"] = message21        # Microsoft Teams Webhooks22        elif "webhook.office.com" in url:23            action = event_data.get("action", "undefined")24            facts = [25                {"name": name, "value": value}26                for name, value in json.loads(event_data.get("user", {})).items()27            ]28            payload = {29                "@type": "MessageCard",30                "@context": "http://schema.org/extensions",31                "themeColor": "0076D7",32                "summary": message,33                "sections": [34                    {35                        "activityTitle": message,36                        "activitySubtitle": f"{WEBUI_NAME} ({VERSION}) - {action}",37                        "activityImage": WEBUI_FAVICON_URL,38                        "facts": facts,39                        "markdown": True,40                    }41                ],42            }43        # Default Payload44        else:45            payload = {**event_data}46 47        log.debug(f"payload: {payload}")48        r = requests.post(url, json=payload)49        r.raise_for_status()50        log.debug(f"r.text: {r.text}")51        return True52    except Exception as e:53        log.exception(e)54        return False55