{
  "name": "Brain \u2014 Append (Telegram \u2192 brain-note)",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -600,
        0
      ],
      "id": "node-telegram-trigger",
      "name": "Telegram Trigger",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.message.from.id }}",
              "rightValue": "__TELEGRAM_CHAT_ID__",
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -490,
        0
      ],
      "id": "node-if-authorized",
      "name": "Is from me?"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.message.voice }}",
              "rightValue": "",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -380,
        0
      ],
      "id": "node-if-voice",
      "name": "Is voice?"
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.message.voice.file_id }}"
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -160,
        -120
      ],
      "id": "node-tg-getfile",
      "name": "Telegram Get File",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "audio",
        "operation": "transcribe",
        "options": {
          "language": "ar"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        60,
        -120
      ],
      "id": "node-whisper",
      "name": "Whisper Transcribe",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "asg-text-voice",
              "name": "text",
              "value": "={{ $json.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        280,
        -120
      ],
      "id": "node-set-voice",
      "name": "Set text (voice)"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "asg-text-text",
              "name": "text",
              "value": "={{ $('Telegram Trigger').item.json.message.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        60,
        120
      ],
      "id": "node-set-text",
      "name": "Set text (text)"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.text }}",
              "rightValue": "/",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        500,
        0
      ],
      "id": "node-if-slash",
      "name": "Is slash command?"
    },
    {
      "parameters": {
        "jsCode": "const text = $input.item.json.text || '';\nconst m = text.trim().match(/^\\/(\\w+)(?:\\s+([\\s\\S]*))?$/);\nconst cmd = (m ? m[1] : 'help').toLowerCase();\nconst args = (m && m[2]) ? m[2].trim() : '';\n\nlet cli;\nif (['today', 'yesterday', 'week', 'help'].includes(cmd)) {\n  cli = `recall ${cmd}`;\n} else if (cmd === 'search') {\n  if (!args) {\n    cli = 'recall help';\n  } else {\n    const b64 = Buffer.from(args, 'utf-8').toString('base64');\n    cli = `search --b64 ${b64}`;\n  }\n} else if (cmd === 'undo') {\n  cli = 'undo';\n} else {\n  cli = `recall ${cmd}`;\n}\n\nreturn { json: { cli } };"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        -120
      ],
      "id": "node-code-cmd",
      "name": "Extract command"
    },
    {
      "parameters": {
        "resource": "command",
        "command": "=cd /Users/khalifah/Projects/brain-code && ./.venv/bin/brain-note {{ $json.cli }}"
      },
      "type": "n8n-nodes-base.ssh",
      "typeVersion": 1,
      "position": [
        940,
        -120
      ],
      "id": "node-ssh-recall",
      "name": "SSH brain-note slash",
      "credentials": {
        "sshPassword": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const text = $input.item.json.text || '';\nconst b64 = Buffer.from(text, 'utf-8').toString('base64');\nreturn { json: { text, b64text: b64 } };"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        120
      ],
      "id": "node-code-b64",
      "name": "Encode b64"
    },
    {
      "parameters": {
        "resource": "command",
        "command": "=cd /Users/khalifah/Projects/brain-code && echo \"{{ $json.b64text }}\" | base64 -d | ./.venv/bin/brain-note capture --stdin"
      },
      "type": "n8n-nodes-base.ssh",
      "typeVersion": 1,
      "position": [
        940,
        0
      ],
      "id": "node-ssh",
      "name": "SSH brain-note capture",
      "credentials": {
        "sshPassword": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.stdout || $json.stderr || 'no output' }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1160,
        0
      ],
      "id": "node-tg-reply",
      "name": "Telegram Reply",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Is from me?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is from me?": {
      "main": [
        [
          {
            "node": "Is voice?",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Is voice?": {
      "main": [
        [
          {
            "node": "Telegram Get File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set text (text)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Get File": {
      "main": [
        [
          {
            "node": "Whisper Transcribe",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Whisper Transcribe": {
      "main": [
        [
          {
            "node": "Set text (voice)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set text (voice)": {
      "main": [
        [
          {
            "node": "Encode b64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set text (text)": {
      "main": [
        [
          {
            "node": "Is slash command?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is slash command?": {
      "main": [
        [
          {
            "node": "Extract command",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Encode b64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract command": {
      "main": [
        [
          {
            "node": "SSH brain-note slash",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SSH brain-note slash": {
      "main": [
        [
          {
            "node": "Telegram Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Encode b64": {
      "main": [
        [
          {
            "node": "SSH brain-note capture",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SSH brain-note capture": {
      "main": [
        [
          {
            "node": "Telegram Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}