{
  "name": "AI powered TODOs 🤖",
  "nodes": [
    {
      "parameters": {
        "respondWith": "noData",
        "options": {
          "responseCode": 422
        }
      },
      "id": "a447e4d0-0eea-4730-9196-9fe251cb22b1",
      "name": "Respond with error",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1140,
        740
      ]
    },
    {
      "parameters": {
        "respondWith": "noData",
        "options": {
          "responseCode": 200
        }
      },
      "id": "bae6071c-41b3-4f6e-92d1-eecc2c5ec921",
      "name": "Respond with success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1140,
        460
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "combinationMode": "multiplex",
        "options": {}
      },
      "id": "8f246352-bddb-4e1a-bcda-9dcacdf43d65",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        2180,
        620
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "30473c04-c79d-4a22-bb45-143ac21077ea",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "4b093ce0-ec0d-4912-b960-0bddfdf506eb",
      "name": "Todoist Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        400,
        660
      ],
      "webhookId": "30473c04-c79d-4a22-bb45-143ac21077ea"
    },
    {
      "parameters": {
        "action": "hmac",
        "type": "SHA256",
        "value": "={{ JSON.stringify($json[\"body\"]) }}",
        "dataPropertyName": "computed_hmac",
        "secret": "[TODO: Replace by your Todoist's n8n app client secret]",
        "encoding": "base64"
      },
      "id": "0f10304d-89a3-4a76-ba92-e3fc90d9f160",
      "name": "Compute expected HMAC",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        640,
        660
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json[\"headers\"][\"x-todoist-hmac-sha256\"] }}",
              "value2": "={{ $json[\"computed_hmac\"] }}"
            }
          ]
        }
      },
      "id": "cdc07abb-92f4-4a5d-8189-2365f694795d",
      "name": "Continue on valid HMAC",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        860,
        660
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json[\"body\"][\"event_data\"][\"labels\"].includes(\"ai-help\") }}",
              "value2": true
            }
          ],
          "string": [
            {
              "value1": "={{ $json[\"body\"][\"event_data\"][\"description\"] }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "64d3ce8e-a1aa-4a02-93af-480eaa1be24e",
      "name": "Continue if label present",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1400,
        460
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer [TODO: Replace by your OpenAI's API secret key]"
            }
          ]
        },
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{JSON.stringify({\n  \"model\": \"text-davinci-003\", \n  \"prompt\": $json[\"body\"][\"event_data\"][\"description\"], \n  \"temperature\": 0.7, \n  \"max_tokens\": 512\n})}}",
        "options": {}
      },
      "id": "6b722c76-3c10-4f71-a37c-8acfc77dbced",
      "name": "OpenAI prompt request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1700,
        440
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "comment",
              "value": "={{ $json[\"choices\"][0][\"text\"] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "540ef7ee-672b-459e-90db-a96b7ed44652",
      "name": "Filter comment",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1900,
        440
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "task_id",
              "value": "={{ $json[\"body\"][\"event_data\"][\"id\"] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "201b063c-ce0b-46c7-8073-56ecb9899941",
      "name": "Filter task ID",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1900,
        640
      ]
    },
    {
      "parameters": {
        "action": "generate",
        "dataPropertyName": "uuidgen"
      },
      "id": "b0e4e93b-ff21-4235-92b6-b19c2548b0cc",
      "name": "Generate idempotency key",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        2180,
        820
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "combinationMode": "multiplex",
        "options": {}
      },
      "id": "11718eb2-d2e0-41d8-98ba-5dde0d5fa302",
      "name": "Merge Again",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        2420,
        800
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.todoist.com/rest/v2/comments",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Request-Id",
              "value": "={{ $json[\"uuidgen\"] }}"
            },
            {
              "name": "Authorization",
              "value": "Bearer [TODO: Replace with your Todoist's n8n app access token]"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "task_id",
              "value": "={{ $json[\"task_id\"] }}"
            },
            {
              "name": "content",
              "value": "={{ $json[\"comment\"] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "4cd5a3a9-3b95-41a4-91ed-761125c358ec",
      "name": "Comment on task",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        2660,
        800
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Respond with success": {
      "main": [
        [
          {
            "node": "Continue if label present",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Merge Again",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Todoist Webhook": {
      "main": [
        [
          {
            "node": "Compute expected HMAC",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute expected HMAC": {
      "main": [
        [
          {
            "node": "Continue on valid HMAC",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Continue on valid HMAC": {
      "main": [
        [
          {
            "node": "Respond with success",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter task ID",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond with error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Continue if label present": {
      "main": [
        [
          {
            "node": "OpenAI prompt request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI prompt request": {
      "main": [
        [
          {
            "node": "Filter comment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter comment": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter task ID": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Generate idempotency key": {
      "main": [
        [
          {
            "node": "Merge Again",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Again": {
      "main": [
        [
          {
            "node": "Comment on task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {},
  "versionId": "7c1f3831-1f51-4268-9560-e639e377e0d7",
  "id": 1,
  "meta": {
    "instanceId": "5cb7dfb315d531f155a76e20e6977ce146b8b70b700053b3bb5ef377041c3457"
  },
  "tags": []
}
