Explorar el Código

refac: async webhook request

Timothy Jaeryang Baek hace 1 mes
padre
commit
f1c28455ad

+ 1 - 1
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),

+ 1 - 1
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}",

+ 2 - 2
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}",

+ 1 - 1
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),

+ 8 - 5
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)