refac: async webhook request

This commit is contained in:
Timothy Jaeryang Baek 2025-08-15 00:07:02 +04:00
parent daa4b3284f
commit f1c28455ad
5 changed files with 13 additions and 10 deletions

View file

@ -625,7 +625,7 @@ async def signup(request: Request, response: Response, form_data: SignupForm):
) )
if request.app.state.config.WEBHOOK_URL: if request.app.state.config.WEBHOOK_URL:
post_webhook( await post_webhook(
request.app.state.WEBUI_NAME, request.app.state.WEBUI_NAME,
request.app.state.config.WEBHOOK_URL, request.app.state.config.WEBHOOK_URL,
WEBHOOK_MESSAGES.USER_SIGNUP(user.name), WEBHOOK_MESSAGES.USER_SIGNUP(user.name),

View file

@ -209,7 +209,7 @@ async def send_notification(name, webui_url, channel, message, active_user_ids):
) )
if webhook_url: if webhook_url:
post_webhook( await post_webhook(
name, name,
webhook_url, webhook_url,
f"#{channel.name} - {webui_url}/channels/{channel.id}\n\n{message.content}", f"#{channel.name} - {webui_url}/channels/{channel.id}\n\n{message.content}",

View file

@ -1323,7 +1323,7 @@ async def process_chat_response(
if not get_active_status_by_user_id(user.id): if not get_active_status_by_user_id(user.id):
webhook_url = Users.get_user_webhook_url_by_id(user.id) webhook_url = Users.get_user_webhook_url_by_id(user.id)
if webhook_url: if webhook_url:
post_webhook( await post_webhook(
request.app.state.WEBUI_NAME, request.app.state.WEBUI_NAME,
webhook_url, webhook_url,
f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}", 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): if not get_active_status_by_user_id(user.id):
webhook_url = Users.get_user_webhook_url_by_id(user.id) webhook_url = Users.get_user_webhook_url_by_id(user.id)
if webhook_url: if webhook_url:
post_webhook( await post_webhook(
request.app.state.WEBUI_NAME, request.app.state.WEBUI_NAME,
webhook_url, webhook_url,
f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}", f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",

View file

@ -502,7 +502,7 @@ class OAuthManager:
) )
if auth_manager_config.WEBHOOK_URL: if auth_manager_config.WEBHOOK_URL:
post_webhook( await post_webhook(
WEBUI_NAME, WEBUI_NAME,
auth_manager_config.WEBHOOK_URL, auth_manager_config.WEBHOOK_URL,
WEBHOOK_MESSAGES.USER_SIGNUP(user.name), WEBHOOK_MESSAGES.USER_SIGNUP(user.name),

View file

@ -1,7 +1,7 @@
import json import json
import logging import logging
import aiohttp
import requests
from open_webui.config import WEBUI_FAVICON_URL from open_webui.config import WEBUI_FAVICON_URL
from open_webui.env import SRC_LOG_LEVELS, VERSION from open_webui.env import SRC_LOG_LEVELS, VERSION
@ -9,7 +9,7 @@ log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["WEBHOOK"]) 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: try:
log.debug(f"post_webhook: {url}, {message}, {event_data}") log.debug(f"post_webhook: {url}, {message}, {event_data}")
payload = {} payload = {}
@ -51,9 +51,12 @@ def post_webhook(name: str, url: str, message: str, event_data: dict) -> bool:
payload = {**event_data} payload = {**event_data}
log.debug(f"payload: {payload}") log.debug(f"payload: {payload}")
r = requests.post(url, json=payload) async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload) as r:
r_text = await r.text()
r.raise_for_status() r.raise_for_status()
log.debug(f"r.text: {r.text}") log.debug(f"r.text: {r_text}")
return True return True
except Exception as e: except Exception as e:
log.exception(e) log.exception(e)