File size: 4,127 Bytes
df8c722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2aba87
 
 
 
 
df8c722
 
5531fc6
df8c722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2aba87
 
 
 
 
 
 
 
df8c722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
from dotenv import load_dotenv
from agency_swarm.tools import BaseTool
from pydantic import Field
from typing import Dict, List, Optional, Any, Union

load_dotenv()

notion_integration_secret = os.getenv("NOTION_INTEGRATION_SECRET")


class UpdateTaskTool(BaseTool):
    """
    Tool for updating an existing task (page) in Notion.
    This tool allows modifying task properties such as title, status, priority, due date, and assignments.
    Note that this tool only updates properties, not the content blocks of the page.
    """

    # Add example_field with a default value to satisfy BaseTool validation
    example_field: str = Field(
        default="notion_update_task",
        description="Identifier for this tool. Can be left at its default value.",
    )

    page_id: str = Field(
        ...,
        description="The ID of the Notion page (task) to update. This is a required field.",
    )

    title: str = Field(
        default=None,
        description="The new title of the task.",
    )

    task_description: str = Field(
        default=None,
        description="The new text description of the task.",
    )

    status: str = Field(
        default=None,
        description="New status of the task. Options: Backlog, In Progress, Testing, Completed.",
    )

    priority: str = Field(
        default=None,
        description="New priority of the task. Options: High, Medium, Low.",
    )

    due_date: str = Field(
        default=None,
        description="New due date of the task in YYYY-MM-DD format. Use 'null' to remove the date.",
    )

    def run(self):
        """
        Update an existing Notion page (task) with the specified properties.

        Returns:
            dict: The JSON response from the Notion API containing the updated page.
        """
        import requests

        # Set up the API endpoint
        url = f"https://api.notion.com/v1/pages/{self.page_id}"

        # Set up the headers
        headers = {
            "Authorization": f"Bearer {os.getenv('NOTION_INTEGRATION_SECRET')}",
            "Notion-Version": "2022-06-28",
            "Content-Type": "application/json",
        }

        # Prepare the request body
        data = {"properties": self._build_properties()}

        # Make the request (PATCH method for updates)
        response = requests.patch(url, headers=headers, json=data)

        # Return the JSON response
        return response.json()

    def _build_properties(self) -> Dict[str, Any]:
        """
        Build the properties object based on provided parameters.
        Only includes properties that were specified for update.

        Returns:
            dict: A dictionary containing the page properties to update in Notion API format.
        """
        properties = {}

        # Add title if provided
        if self.title is not None:
            properties["Task Name"] = {
                "title": [{"type": "text", "text": {"content": self.title}}]
            }

        # Add task description if provided
        if self.task_description is not None:
            properties["Task Description"] = {
                "rich_text": [
                    {"type": "text", "text": {"content": self.task_description}}
                ]
            }

        # Add status if provided
        if self.status is not None:
            properties["Status"] = {"status": {"name": self.status}}

        # Add priority if provided
        if self.priority is not None:
            properties["Priority"] = {"select": {"name": self.priority}}

        # Add due date if provided
        if self.due_date is not None:
            # Check if we're clearing the date
            if self.due_date.lower() == "null":
                properties["Due Date"] = {"date": None}
            else:
                properties["Due Date"] = {"date": {"start": self.due_date}}

        # Add assigned people if provided
        # if self.assigned_to is not None:
        #     properties["Assigned to"] = {
        #         "people": [{"id": user_id} for user_id in self.assigned_to]
        #     }

        return properties