| 
									
										
										
										
											2024-03-26 15:04:17 +08:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2024-04-01 03:17:29 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-28 06:10:27 +08:00
										 |  |  | import requests | 
					
						
							| 
									
										
										
										
											2025-02-16 16:11:18 +08:00
										 |  |  | from open_webui.config import WEBUI_FAVICON_URL | 
					
						
							| 
									
										
										
										
											2024-09-04 22:54:48 +08:00
										 |  |  | from open_webui.env import SRC_LOG_LEVELS, VERSION | 
					
						
							| 
									
										
										
										
											2024-04-01 03:17:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | log = logging.getLogger(__name__) | 
					
						
							|  |  |  | log.setLevel(SRC_LOG_LEVELS["WEBHOOK"]) | 
					
						
							| 
									
										
										
										
											2024-03-21 09:35:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-31 16:13:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 16:11:18 +08:00
										 |  |  | def post_webhook(name: str, url: str, message: str, event_data: dict) -> bool: | 
					
						
							| 
									
										
										
										
											2024-03-21 09:35:02 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-12-21 14:54:43 +08:00
										 |  |  |         log.debug(f"post_webhook: {url}, {message}, {event_data}") | 
					
						
							| 
									
										
										
										
											2024-03-21 09:47:13 +08:00
										 |  |  |         payload = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 10:25:57 +08:00
										 |  |  |         # Slack and Google Chat Webhooks | 
					
						
							|  |  |  |         if "https://hooks.slack.com" in url or "https://chat.googleapis.com" in url: | 
					
						
							| 
									
										
										
										
											2024-03-21 09:47:13 +08:00
										 |  |  |             payload["text"] = message | 
					
						
							| 
									
										
										
										
											2024-03-27 10:25:57 +08:00
										 |  |  |         # Discord Webhooks | 
					
						
							| 
									
										
										
										
											2024-03-21 09:47:13 +08:00
										 |  |  |         elif "https://discord.com/api/webhooks" in url: | 
					
						
							| 
									
										
										
										
											2024-12-21 15:05:22 +08:00
										 |  |  |             payload["content"] = ( | 
					
						
							|  |  |  |                 message | 
					
						
							| 
									
										
										
										
											2024-12-25 15:53:25 +08:00
										 |  |  |                 if len(message) < 2000 | 
					
						
							| 
									
										
										
										
											2024-12-25 12:17:24 +08:00
										 |  |  |                 else f"{message[: 2000 - 20]}... (truncated)" | 
					
						
							| 
									
										
										
										
											2024-12-21 15:05:22 +08:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-03-27 10:25:57 +08:00
										 |  |  |         # Microsoft Teams Webhooks | 
					
						
							| 
									
										
										
										
											2024-03-26 15:04:17 +08:00
										 |  |  |         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, | 
					
						
							| 
									
										
										
										
											2025-02-16 16:11:18 +08:00
										 |  |  |                         "activitySubtitle": f"{name} ({VERSION}) - {action}", | 
					
						
							| 
									
										
										
										
											2024-03-26 15:45:36 +08:00
										 |  |  |                         "activityImage": WEBUI_FAVICON_URL, | 
					
						
							| 
									
										
										
										
											2024-03-26 15:04:17 +08:00
										 |  |  |                         "facts": facts, | 
					
						
							|  |  |  |                         "markdown": True, | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-03-27 10:25:57 +08:00
										 |  |  |         # Default Payload | 
					
						
							| 
									
										
										
										
											2024-03-21 09:47:13 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             payload = {**event_data} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 03:17:29 +08:00
										 |  |  |         log.debug(f"payload: {payload}") | 
					
						
							| 
									
										
										
										
											2024-03-21 09:47:13 +08:00
										 |  |  |         r = requests.post(url, json=payload) | 
					
						
							| 
									
										
										
										
											2024-03-21 09:35:02 +08:00
										 |  |  |         r.raise_for_status() | 
					
						
							| 
									
										
										
										
											2024-04-01 03:17:29 +08:00
										 |  |  |         log.debug(f"r.text: {r.text}") | 
					
						
							| 
									
										
										
										
											2024-03-21 09:35:02 +08:00
										 |  |  |         return True | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-04-01 03:17:29 +08:00
										 |  |  |         log.exception(e) | 
					
						
							| 
									
										
										
										
											2024-03-31 16:13:39 +08:00
										 |  |  |         return False |