-
Notifications
You must be signed in to change notification settings - Fork 1
Add OneBot-11 protocol support #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,6 +170,103 @@ async def close(self) -> None: | |
| self.session = None | ||
|
|
||
|
|
||
| class OneBotNotifier(BaseNotifier): | ||
| """OneBot-11 notifier that sends messages to OneBot-11 servers via HTTP""" | ||
|
|
||
| def __init__(self, config: dict): | ||
| super().__init__(config) | ||
| self.url = config.get("url", "http://127.0.0.1:5700") | ||
| self.access_token = config.get("access_token", "") | ||
| self.to_group_ids = config.get("to_group_ids", []) | ||
| self.to_friend_ids = config.get("to_friend_ids", []) | ||
|
|
||
| if not self.to_group_ids and not self.to_friend_ids: | ||
| logger.warning( | ||
| "OneBotNotifier: No group or friend IDs configured, notifications will not be sent" | ||
| ) | ||
|
|
||
| # Prepare headers once during initialization | ||
| self.headers = {} | ||
| if self.access_token: | ||
| self.headers["Authorization"] = f"Bearer {self.access_token}" | ||
|
|
||
| self.session = None | ||
| logger.info( | ||
| f"OneBot-11 notifier initialized: {self.url} " | ||
| f"(groups: {len(self.to_group_ids)}, friends: {len(self.to_friend_ids)})" | ||
| ) | ||
|
|
||
| async def send(self, markdown_content: str) -> None: | ||
| """ | ||
| Send notification to OneBot-11 server. | ||
|
|
||
| Args: | ||
| markdown_content: The notification content in markdown format | ||
| """ | ||
| if not self.enabled: | ||
| return | ||
|
|
||
| if not self.to_group_ids and not self.to_friend_ids: | ||
| return | ||
|
|
||
| # Check if session is initialized | ||
| if self.session is None: | ||
| # Import here to avoid dependency if not using OneBotNotifier | ||
| import aiohttp | ||
|
|
||
| # Create session with timeout configuration | ||
| timeout = aiohttp.ClientTimeout(total=30) | ||
| self.session = aiohttp.ClientSession(timeout=timeout) | ||
|
|
||
| # Send to all configured groups | ||
| for group_id in self.to_group_ids: | ||
| payload = {"group_id": group_id, "message": markdown_content} | ||
| try: | ||
| async with self.session.post( | ||
| f"{self.url}/send_group_msg", json=payload, headers=self.headers | ||
| ) as response: | ||
| if response.status != 200: | ||
| text = await response.text() | ||
| logger.error( | ||
| f"Failed to send OneBot group message to {group_id}: {response.status}, {text}" | ||
| ) | ||
| else: | ||
| logger.debug(f"OneBot group notification sent to {group_id}") | ||
| except Exception as e: | ||
| logger.error( | ||
| f"Error sending OneBot group notification to {group_id}: {e}", | ||
| exc_info=True, | ||
| ) | ||
|
|
||
| # Send to all configured friends | ||
| for friend_id in self.to_friend_ids: | ||
| payload = {"user_id": friend_id, "message": markdown_content} | ||
| try: | ||
| async with self.session.post( | ||
| f"{self.url}/send_private_msg", json=payload, headers=self.headers | ||
| ) as response: | ||
| if response.status != 200: | ||
| text = await response.text() | ||
| logger.error( | ||
| f"Failed to send OneBot private message to {friend_id}: {response.status}, {text}" | ||
| ) | ||
| else: | ||
| logger.debug( | ||
| f"OneBot private notification sent to {friend_id}" | ||
| ) | ||
| except Exception as e: | ||
| logger.error( | ||
| f"Error sending OneBot private notification to {friend_id}: {e}", | ||
| exc_info=True, | ||
| ) | ||
|
Comment on lines
+222
to
+261
|
||
|
|
||
| async def close(self) -> None: | ||
| """Close the aiohttp session""" | ||
| if self.session is not None: | ||
| await self.session.close() | ||
| self.session = None | ||
|
|
||
|
|
||
| class Notifier: | ||
| """Main notifier class that manages multiple notification providers. This class handles the configuration file and initializes the appropriate notifier instances based on the provided settings.""" | ||
|
|
||
|
|
@@ -197,6 +294,11 @@ def __init__(self, config: dict): | |
| if lark_config.get("enabled", False): | ||
| self.notifiers.append(LarkNotifier(lark_config)) | ||
|
|
||
| # Initialize onebot-11 notifier if configured | ||
| onebot_config = config.get("onebot-11", {}) | ||
| if onebot_config.get("enabled", False): | ||
| self.notifiers.append(OneBotNotifier(onebot_config)) | ||
|
|
||
| logger.info( | ||
| f"Notifier system initialized with {len(self.notifiers)} provider(s)" | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation at line 158 claims "Automatic error handling and retry" but the implementation only includes error handling (logging errors) without any retry logic. Either implement retry functionality or update the documentation to remove the "and retry" claim.