{
  "name": "Groom AB Marketplace Bot",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message",
          "callback_query"
        ],
        "additionalFields": {}
      },
      "id": "1",
      "name": "Telegram Trigger - GroomAB Bot",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const body = $input.first().json;\nconst msg = body.message;\nconst cb = body.callback_query;\n\nif (cb) {\n  return [{ json: { type: 'callback', data: cb.data, from: cb.from, message: cb.message, callback_query_id: cb.id } }];\n}\n\nif (msg) {\n  return [{ json: { type: 'message', text: msg.text || '', from: msg.from, chat_id: msg.chat.id, message_id: msg.message_id, photo: msg.photo || null } }];\n}\n\nreturn [{ json: { type: 'unknown' } }];"
      },
      "id": "2",
      "name": "Parse Update",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.type }}",
              "rightValue": "callback",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "3",
      "name": "Is Callback?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const data = $json.data;\nconst parts = data.split('_');\nconst action = parts[0];\nconst submissionId = parts.slice(1).join('_');\nreturn [{ json: { action, submissionId, from: $json.from, callback_query_id: $json.callback_query_id } }];"
      },
      "id": "4",
      "name": "Parse Callback",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        180
      ]
    },
    {
      "parameters": {
        "jsCode": "const text = $json.text || '';\nconst chatId = $json.chat_id;\nconst from = $json.from;\n\n// Session key based on user id\nconst userId = from.id;\n\n// Return parsed message info\nreturn [{ json: { text, chatId, userId, from, photo: $json.photo } }];"
      },
      "id": "5",
      "name": "Parse Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        420
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false
          },
          "conditions": [
            {
              "id": "s1",
              "leftValue": "={{ $json.text }}",
              "rightValue": "/start",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "s2",
              "leftValue": "={{ $json.text }}",
              "rightValue": "/submit",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "or"
        }
      },
      "id": "6",
      "name": "Is Start or Submit?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1120,
        420
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "Welcome to *Groom AB Engineering Construction Marketplace!*\n\nPlease choose what you want to do:\n\n*1.* Sell an item\n*2.* Rent out an item\n\nReply with *1* for Sell or *2* for Rent to begin.",
        "additionalFields": {
          "parse_mode": "Markdown"
        },
        "credentials": {
          "telegramApi": {
            "id": "groomab",
            "name": "GroomAB Bot"
          }
        }
      },
      "id": "7",
      "name": "Send Welcome",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Submissions",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Submission_ID": "={{ $json.submissionId }}",
            "Status": "Approved",
            "Approved_Date": "={{ new Date().toISOString() }}"
          },
          "matchingColumns": [
            "Submission_ID"
          ],
          "schema": []
        },
        "options": {}
      },
      "id": "8",
      "name": "Update Sheet - Approved",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1120,
        180
      ]
    },
    {
      "parameters": {
        "chatId": "-1001160650703",
        "text": "={{ $json.post_text }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        },
        "credentials": {
          "telegramApi": {
            "id": "groomam",
            "name": "GroomAM Bot"
          }
        }
      },
      "id": "9",
      "name": "Post to Group",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1340,
        180
      ]
    }
  ],
  "connections": {
    "Telegram Trigger - GroomAB Bot": {
      "main": [
        [
          {
            "node": "Parse Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Update": {
      "main": [
        [
          {
            "node": "Is Callback?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Callback?": {
      "main": [
        [
          {
            "node": "Parse Callback",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Parse Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Callback": {
      "main": [
        [
          {
            "node": "Update Sheet - Approved",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Sheet - Approved": {
      "main": [
        [
          {
            "node": "Post to Group",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Message": {
      "main": [
        [
          {
            "node": "Is Start or Submit?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Start or Submit?": {
      "main": [
        [
          {
            "node": "Send Welcome",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}