Skip to content

Commit

Permalink
feat: ntfy tool
Browse files Browse the repository at this point in the history
  • Loading branch information
dartpain committed Mar 9, 2025
1 parent 4fd6c52 commit 8b9b744
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
127 changes: 127 additions & 0 deletions application/agents/tools/ntfy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import requests
from application.agents.tools.base import Tool

class NtfyTool(Tool):
"""
Ntfy Tool
A tool for sending notifications to ntfy topics on a specified server.
"""

def __init__(self, config):
"""
Initialize the NtfyTool with configuration.
Args:
config (dict): Configuration dictionary containing the access token.
"""
self.config = config
self.token = config.get("token", "")

def execute_action(self, action_name, **kwargs):
"""
Execute the specified action with given parameters.
Args:
action_name (str): Name of the action to execute.
**kwargs: Parameters for the action, including server_url.
Returns:
dict: Result of the action with status code and message.
Raises:
ValueError: If the action name is unknown.
"""
actions = {

Check warning on line 34 in application/agents/tools/ntfy.py

View check run for this annotation

Codecov / codecov/patch

application/agents/tools/ntfy.py#L34

Added line #L34 was not covered by tests
"ntfy_send_message": self._send_message,
}
if action_name in actions:
return actions[action_name](**kwargs)

Check warning on line 38 in application/agents/tools/ntfy.py

View check run for this annotation

Codecov / codecov/patch

application/agents/tools/ntfy.py#L37-L38

Added lines #L37 - L38 were not covered by tests
else:
raise ValueError(f"Unknown action: {action_name}")

Check warning on line 40 in application/agents/tools/ntfy.py

View check run for this annotation

Codecov / codecov/patch

application/agents/tools/ntfy.py#L40

Added line #L40 was not covered by tests

def _send_message(self, server_url, message, topic, title=None, priority=None):
"""
Send a message to an ntfy topic on the specified server.
Args:
server_url (str): Base URL of the ntfy server (e.g., https://ntfy.sh).
message (str): The message text to send.
topic (str): The topic to send the message to.
title (str, optional): Title of the notification.
priority (int, optional): Priority of the notification (1-5).
Returns:
dict: Response with status code and a confirmation message.
Raises:
ValueError: If priority is not an integer between 1 and 5.
"""
url = f"{server_url.rstrip('/')}/{topic}"
headers = {}
if title:
headers["X-Title"] = title
if priority:
try:
priority = int(priority)
except (ValueError, TypeError):
raise ValueError("Priority must be convertible to an integer")
if priority < 1 or priority > 5:
raise ValueError("Priority must be an integer between 1 and 5")
headers["X-Priority"] = str(priority)
if self.token:
headers["Authorization"] = f"Basic {self.token}"
data = message.encode("utf-8")
response = requests.post(url, headers=headers, data=data)
return {"status_code": response.status_code, "message": "Message sent"}

Check warning on line 75 in application/agents/tools/ntfy.py

View check run for this annotation

Codecov / codecov/patch

application/agents/tools/ntfy.py#L59-L75

Added lines #L59 - L75 were not covered by tests

def get_actions_metadata(self):
"""
Provide metadata about available actions.
Returns:
list: List of dictionaries describing each action.
"""
return [

Check warning on line 84 in application/agents/tools/ntfy.py

View check run for this annotation

Codecov / codecov/patch

application/agents/tools/ntfy.py#L84

Added line #L84 was not covered by tests
{
"name": "ntfy_send_message",
"description": "Send a notification to an ntfy topic",
"parameters": {
"type": "object",
"properties": {
"server_url": {
"type": "string",
"description": "Base URL of the ntfy server",
},
"message": {
"type": "string",
"description": "Text to send in the notification",
},
"topic": {
"type": "string",
"description": "Topic to send the notification to",
},
"title": {
"type": "string",
"description": "Title of the notification (optional)",
},
"priority": {
"type": "integer",
"description": "Priority of the notification (1-5, optional)",
},
},
"required": ["server_url", "message", "topic"],
"additionalProperties": False,
},
},
]

def get_config_requirements(self):
"""
Specify the configuration requirements.
Returns:
dict: Dictionary describing required config parameters.
"""
return {

Check warning on line 125 in application/agents/tools/ntfy.py

View check run for this annotation

Codecov / codecov/patch

application/agents/tools/ntfy.py#L125

Added line #L125 was not covered by tests
"token": {"type": "string", "description": "Access token for authentication"},
}
8 changes: 8 additions & 0 deletions frontend/public/toolIcons/tool_ntfy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8b9b744

Please sign in to comment.