From 5eac5c54f8577c50e35a7ce1c64fc70f25fccd07 Mon Sep 17 00:00:00 2001 From: changchiyou Date: Tue, 26 Mar 2024 15:04:17 +0800 Subject: [PATCH 1/3] feat: Teams MessageCard refer to https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using\?tabs\=cURL\#example-of-connector-message --- backend/utils/webhook.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backend/utils/webhook.py b/backend/utils/webhook.py index 1bc5a6048..51157a4e4 100644 --- a/backend/utils/webhook.py +++ b/backend/utils/webhook.py @@ -1,4 +1,7 @@ +import json + import requests +from config import VERSION def post_webhook(url: str, message: str, event_data: dict) -> bool: @@ -9,6 +12,27 @@ def post_webhook(url: str, message: str, event_data: dict) -> bool: 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, + } + ], + } else: payload = {**event_data} From 427ca4e3f5ac7e83484bfd2e86c11d16d7382dfe Mon Sep 17 00:00:00 2001 From: changchiyou Date: Tue, 26 Mar 2024 15:45:36 +0800 Subject: [PATCH 2/3] refactor: dynamically swap `activitySubtitle` and `activityImage` if `CUSTOM_NAME` has been set --- backend/config.py | 26 ++++++++++++-------------- backend/utils/webhook.py | 6 +++--- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/backend/config.py b/backend/config.py index 67edd3f4f..613b747a0 100644 --- a/backend/config.py +++ b/backend/config.py @@ -1,29 +1,27 @@ -import os -import chromadb -from chromadb import Settings -from base64 import b64encode -from bs4 import BeautifulSoup - -from pathlib import Path import json -import yaml +import os +import shutil +from base64 import b64encode +from pathlib import Path +from secrets import token_bytes +import chromadb import markdown import requests -import shutil - -from secrets import token_bytes +import yaml +from bs4 import BeautifulSoup +from chromadb import Settings from constants import ERROR_MESSAGES - try: - from dotenv import load_dotenv, find_dotenv + from dotenv import find_dotenv, load_dotenv load_dotenv(find_dotenv("../.env")) except ImportError: print("dotenv not installed, skipping...") WEBUI_NAME = "Open WebUI" +WEBUI_FAVICON_URL = "https://openwebui.com/favicon.png" shutil.copyfile("../build/favicon.png", "./static/favicon.png") #################################### @@ -111,7 +109,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"] diff --git a/backend/utils/webhook.py b/backend/utils/webhook.py index 51157a4e4..4cfa41640 100644 --- a/backend/utils/webhook.py +++ b/backend/utils/webhook.py @@ -1,7 +1,7 @@ import json import requests -from config import VERSION +from config import VERSION, WEBUI_FAVICON_URL, WEBUI_NAME def post_webhook(url: str, message: str, event_data: dict) -> bool: @@ -26,8 +26,8 @@ def post_webhook(url: str, message: str, event_data: dict) -> bool: "sections": [ { "activityTitle": message, - "activitySubtitle": f"Open WebUI ({VERSION}) - {action}", - "activityImage": "https://openwebui.com/favicon.png", + "activitySubtitle": f"{WEBUI_NAME} ({VERSION}) - {action}", + "activityImage": WEBUI_FAVICON_URL, "facts": facts, "markdown": True, } From d69dfb7c8b2183a633bd849839e056e9317d4803 Mon Sep 17 00:00:00 2001 From: changchiyou Date: Wed, 27 Mar 2024 10:25:57 +0800 Subject: [PATCH 3/3] feat: support google chat webhook https://github.com/open-webui/open-webui/pull/1305/\#issuecomment-2021151237 --- backend/utils/webhook.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/utils/webhook.py b/backend/utils/webhook.py index 4cfa41640..ed626d03e 100644 --- a/backend/utils/webhook.py +++ b/backend/utils/webhook.py @@ -1,17 +1,18 @@ 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 = [ @@ -33,6 +34,7 @@ def post_webhook(url: str, message: str, event_data: dict) -> bool: } ], } + # Default Payload else: payload = {**event_data} @@ -41,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 \ No newline at end of file