Browse Source

Merge pull request #1305 from changchiyou/feat/Teams_Incoming_Webhook

enhance: support Teams Incoming Webhook
Timothy Jaeryang Baek 1 year ago
parent
commit
e5408995d8
2 changed files with 31 additions and 4 deletions
  1. 2 1
      backend/config.py
  2. 29 3
      backend/utils/webhook.py

+ 2 - 1
backend/config.py

@@ -26,6 +26,7 @@ except ImportError:
     log.warning("dotenv not installed, skipping...")
 
 WEBUI_NAME = "Open WebUI"
+WEBUI_FAVICON_URL = "https://openwebui.com/favicon.png"
 shutil.copyfile("../build/favicon.png", "./static/favicon.png")
 
 ####################################
@@ -141,7 +142,7 @@ if CUSTOM_NAME:
         data = r.json()
         if r.ok:
             if "logo" in data:
-                url = (
+                WEBUI_FAVICON_URL = url = (
                     f"https://api.openwebui.com{data['logo']}"
                     if data["logo"][0] == "/"
                     else data["logo"]

+ 29 - 3
backend/utils/webhook.py

@@ -1,14 +1,40 @@
+import json
 import requests
-
+from config import VERSION, WEBUI_FAVICON_URL, WEBUI_NAME
 
 def post_webhook(url: str, message: str, event_data: dict) -> bool:
     try:
         payload = {}
 
-        if "https://hooks.slack.com" in url:
+        # Slack and Google Chat Webhooks
+        if "https://hooks.slack.com" in url or "https://chat.googleapis.com" in url:
             payload["text"] = message
+        # Discord Webhooks
         elif "https://discord.com/api/webhooks" in url:
             payload["content"] = message
+        # Microsoft Teams Webhooks
+        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"{WEBUI_NAME} ({VERSION}) - {action}",
+                        "activityImage": WEBUI_FAVICON_URL,
+                        "facts": facts,
+                        "markdown": True,
+                    }
+                ],
+            }
+        # Default Payload
         else:
             payload = {**event_data}
 
@@ -17,4 +43,4 @@ def post_webhook(url: str, message: str, event_data: dict) -> bool:
         return True
     except Exception as e:
         print(e)
-        return False
+        return False