| 
									
										
										
										
											2024-03-26 15:04:17 +08:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2024-03-21 09:35:02 +08:00
										 |  |  | import requests | 
					
						
							| 
									
										
										
										
											2024-04-01 03:17:29 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from config import SRC_LOG_LEVELS, VERSION, WEBUI_FAVICON_URL, WEBUI_NAME | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 09:47:13 +08:00
										 |  |  | def post_webhook(url: str, message: str, event_data: dict) -> bool: | 
					
						
							| 
									
										
										
										
											2024-03-21 09:35:02 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							|  |  |  |             payload["content"] = message | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2024-03-26 15:45:36 +08:00
										 |  |  |                         "activitySubtitle": f"{WEBUI_NAME} ({VERSION}) - {action}", | 
					
						
							|  |  |  |                         "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 |