浏览代码

fix ntfy config

zehe 3 月之前
父节点
当前提交
8f67d7b2b5
共有 3 个文件被更改,包括 22 次插入24 次删除
  1. 4 4
      README.md
  2. 10 19
      main.py
  3. 8 1
      test.py

+ 4 - 4
README.md

@@ -391,13 +391,13 @@ The service binds only to localhost (127.0.0.1:1127) and is accessed through you
 2. **Notification failures**:
 2. **Notification failures**:
    ```bash
    ```bash
    # Test dschep/ntfy installation and configuration
    # Test dschep/ntfy installation and configuration
-   NTFY_CONFIG=/etc/emergency-access/ntfy.yml ntfy send "test message"
+   ntfy -c /etc/emergency-access/ntfy.yml send "test message"
    
    
    # Check ntfy configuration
    # Check ntfy configuration
    cat /etc/emergency-access/ntfy.yml
    cat /etc/emergency-access/ntfy.yml
    
    
    # Test specific backend
    # Test specific backend
-   NTFY_CONFIG=/etc/emergency-access/ntfy.yml ntfy -b matrix_sec send "test message"
+   ntfy -c /etc/emergency-access/ntfy.yml -b matrix_sec send "test message"
    ```
    ```
 
 
 3. **File permission errors**:
 3. **File permission errors**:
@@ -417,8 +417,8 @@ Test your setup before deployment:
 sudo -u emergency-access /opt/emergency-access/venv/bin/python /opt/emergency-access/main.py --validate
 sudo -u emergency-access /opt/emergency-access/venv/bin/python /opt/emergency-access/main.py --validate
 
 
 # Test notifications manually with your backend
 # Test notifications manually with your backend
-NTFY_CONFIG=/etc/emergency-access/ntfy.yml ntfy -b matrix_sec send "Test notification"
-NTFY_CONFIG=/etc/emergency-access/ntfy.yml ntfy -b pushover_emergency send "Test emergency notification"
+ntfy -c /etc/emergency-access/ntfy.yml -b matrix_sec send "Test notification"
+ntfy -c /etc/emergency-access/ntfy.yml -b pushover_emergency send "Test emergency notification"
 ```
 ```
 
 
 ## Development
 ## Development

+ 10 - 19
main.py

@@ -68,26 +68,17 @@ def send_ntfy_notification(backends: List[str], message: str, title: str = None)
             # Import ntfy here to avoid import issues during startup
             # Import ntfy here to avoid import issues during startup
             import ntfy
             import ntfy
 
 
-            # Set the ntfy config file path
-            old_config = os.environ.get('NTFY_CONFIG')
-            os.environ['NTFY_CONFIG'] = config.ntfy_config_path
+            # Load the ntfy config file
+            ntfy_config = ntfy.load_config(config.ntfy_config_path)
 
 
-            try:
-                # Send notification using the backend name from our config file
-                if title:
-                    ntfy.notify(message, title=title, backend=backend)
-                else:
-                    ntfy.notify(message, backend=backend)
-
-                successful_backends.append(backend)
-                logger.info(f"Notification sent successfully via {backend}")
-
-            finally:
-                # Restore original config
-                if old_config:
-                    os.environ['NTFY_CONFIG'] = old_config
-                elif 'NTFY_CONFIG' in os.environ:
-                    del os.environ['NTFY_CONFIG']
+            # Send notification using the backend name from our config file
+            if title:
+                ntfy.notify(message, title=title, backend=backend, config=ntfy_config)
+            else:
+                ntfy.notify(message, backend=backend, config=ntfy_config)
+
+            successful_backends.append(backend)
+            logger.info(f"Notification sent successfully via {backend}")
 
 
         except ImportError:
         except ImportError:
             logger.error(f"ntfy package not available for backend {backend}")
             logger.error(f"ntfy package not available for backend {backend}")

+ 8 - 1
test.py

@@ -100,13 +100,20 @@ class EmergencyAccessTester:
         """Test ntfy backend connectivity"""
         """Test ntfy backend connectivity"""
         success = True
         success = True
 
 
-        # Test importing ntfy
+        # Test importing ntfy and loading config
         try:
         try:
             import ntfy
             import ntfy
             self.log_test("ntfy Import", True, "dschep/ntfy package available")
             self.log_test("ntfy Import", True, "dschep/ntfy package available")
+
+            # Test loading the ntfy config file
+            ntfy_config = ntfy.load_config(self.config.ntfy_config_path)
+            self.log_test("ntfy Config Load", True, f"Config loaded from {self.config.ntfy_config_path}")
         except ImportError:
         except ImportError:
             self.log_test("ntfy Import", False, "dschep/ntfy package not installed")
             self.log_test("ntfy Import", False, "dschep/ntfy package not installed")
             return False
             return False
+        except Exception as e:
+            self.log_test("ntfy Config Load", False, f"Failed to load config: {str(e)}")
+            return False
 
 
         # Test key notification backends
         # Test key notification backends
         for backend in self.config.ntfy_backends_key:
         for backend in self.config.ntfy_backends_key: