From f1c28455ad668ed9a1edfa3e6cf9405ddadaab6f Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 15 Aug 2025 00:07:02 +0400 Subject: [PATCH] refac: async webhook request --- backend/open_webui/routers/auths.py | 2 +- backend/open_webui/routers/channels.py | 2 +- backend/open_webui/utils/middleware.py | 4 ++-- backend/open_webui/utils/oauth.py | 2 +- backend/open_webui/utils/webhook.py | 13 ++++++++----- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/backend/open_webui/routers/auths.py b/backend/open_webui/routers/auths.py index e157e5527d..78a576d202 100644 --- a/backend/open_webui/routers/auths.py +++ b/backend/open_webui/routers/auths.py @@ -625,7 +625,7 @@ async def signup(request: Request, response: Response, form_data: SignupForm): ) if request.app.state.config.WEBHOOK_URL: - post_webhook( + await post_webhook( request.app.state.WEBUI_NAME, request.app.state.config.WEBHOOK_URL, WEBHOOK_MESSAGES.USER_SIGNUP(user.name), diff --git a/backend/open_webui/routers/channels.py b/backend/open_webui/routers/channels.py index e4390e23f6..cf3603c6ff 100644 --- a/backend/open_webui/routers/channels.py +++ b/backend/open_webui/routers/channels.py @@ -209,7 +209,7 @@ async def send_notification(name, webui_url, channel, message, active_user_ids): ) if webhook_url: - post_webhook( + await post_webhook( name, webhook_url, f"#{channel.name} - {webui_url}/channels/{channel.id}\n\n{message.content}", diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index e0c38c4a03..03704cd6be 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1323,7 +1323,7 @@ async def process_chat_response( if not get_active_status_by_user_id(user.id): webhook_url = Users.get_user_webhook_url_by_id(user.id) if webhook_url: - post_webhook( + await post_webhook( request.app.state.WEBUI_NAME, webhook_url, f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}", @@ -2523,7 +2523,7 @@ async def process_chat_response( if not get_active_status_by_user_id(user.id): webhook_url = Users.get_user_webhook_url_by_id(user.id) if webhook_url: - post_webhook( + await post_webhook( request.app.state.WEBUI_NAME, webhook_url, f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}", diff --git a/backend/open_webui/utils/oauth.py b/backend/open_webui/utils/oauth.py index 67ae13bc9b..fc68105770 100644 --- a/backend/open_webui/utils/oauth.py +++ b/backend/open_webui/utils/oauth.py @@ -502,7 +502,7 @@ class OAuthManager: ) if auth_manager_config.WEBHOOK_URL: - post_webhook( + await post_webhook( WEBUI_NAME, auth_manager_config.WEBHOOK_URL, WEBHOOK_MESSAGES.USER_SIGNUP(user.name), diff --git a/backend/open_webui/utils/webhook.py b/backend/open_webui/utils/webhook.py index bf0b334d82..7ea29f3988 100644 --- a/backend/open_webui/utils/webhook.py +++ b/backend/open_webui/utils/webhook.py @@ -1,7 +1,7 @@ import json import logging +import aiohttp -import requests from open_webui.config import WEBUI_FAVICON_URL from open_webui.env import SRC_LOG_LEVELS, VERSION @@ -9,7 +9,7 @@ log = logging.getLogger(__name__) log.setLevel(SRC_LOG_LEVELS["WEBHOOK"]) -def post_webhook(name: str, url: str, message: str, event_data: dict) -> bool: +async def post_webhook(name: str, url: str, message: str, event_data: dict) -> bool: try: log.debug(f"post_webhook: {url}, {message}, {event_data}") payload = {} @@ -51,9 +51,12 @@ def post_webhook(name: str, url: str, message: str, event_data: dict) -> bool: payload = {**event_data} log.debug(f"payload: {payload}") - r = requests.post(url, json=payload) - r.raise_for_status() - log.debug(f"r.text: {r.text}") + async with aiohttp.ClientSession() as session: + async with session.post(url, json=payload) as r: + r_text = await r.text() + r.raise_for_status() + log.debug(f"r.text: {r_text}") + return True except Exception as e: log.exception(e)