open-webui/backend/utils/webhook.py

45 lines
1.4 KiB
Python
Raw Normal View History

import json
2024-03-21 02:35:02 +01:00
import requests
from config import VERSION
2024-03-21 02:35:02 +01:00
2024-03-21 02:47:13 +01:00
def post_webhook(url: str, message: str, event_data: dict) -> bool:
2024-03-21 02:35:02 +01:00
try:
2024-03-21 02:47:13 +01:00
payload = {}
if "https://hooks.slack.com" in url:
payload["text"] = message
elif "https://discord.com/api/webhooks" in url:
payload["content"] = message
elif "webhook.office.com" in url:
action = event_data.get("action", "undefined")
facts = [
{"name": name, "value": value}
for name, value in json.loads(event_data.get("user", {})).items()
]
payload = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": message,
"sections": [
{
"activityTitle": message,
"activitySubtitle": f"Open WebUI ({VERSION}) - {action}",
"activityImage": "https://openwebui.com/favicon.png",
"facts": facts,
"markdown": True,
}
],
}
2024-03-21 02:47:13 +01:00
else:
payload = {**event_data}
r = requests.post(url, json=payload)
2024-03-21 02:35:02 +01:00
r.raise_for_status()
return True
except Exception as e:
print(e)
return False