{
  "id": "A0Db66VzkRkoTQtL",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Query and Monitor Shopify Orders via Telegram Bot Commands",
  "tags": [
    {
      "id": "hlEHEpOdSg5lhIqi",
      "name": "Template",
      "createdAt": "2025-06-05T15:35:52.689Z",
      "updatedAt": "2025-06-05T15:35:52.689Z"
    }
  ],
  "nodes": [
    {
      "id": "0ec29eee-814b-43a6-8ed6-608306de9dd7",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -760,
        280
      ],
      "parameters": {
        "updates": [
          "message",
          "*"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "29ea564c-f456-4779-b5f4-cd4c26e6738f",
      "name": "get Orders",
      "type": "n8n-nodes-base.shopify",
      "position": [
        0,
        0
      ],
      "parameters": {
        "options": {
          "status": "open"
        },
        "operation": "getAll",
        "returnAll": true,
        "authentication": "accessToken"
      },
      "credentials": {
        "shopifyAccessTokenApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": false,
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "5a6bf5b1-0249-4773-aae2-9ac3fdd3c8a2",
      "name": "Orders",
      "type": "n8n-nodes-base.code",
      "position": [
        580,
        -120
      ],
      "parameters": {
        "jsCode": "const chatId = $('Telegram Trigger').first().json.message.chat.id;\n\nreturn [{\n  json: {\n    chat_id: chatId,\n    text: \"All Open Orders\",\n    reply_markup: {\n      inline_keyboard: items.map(order => [{\n        text: `${order.json.name} ($${order.json.total_price})`,\n        callback_data: `/order_${order.json.id}`\n      }])\n    }\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "cfa6e297-1462-480b-9e48-c3d50a63cadc",
      "name": "No Order",
      "type": "n8n-nodes-base.code",
      "position": [
        580,
        80
      ],
      "parameters": {
        "jsCode": "const chatId = $('Telegram Trigger').first().json.message.chat.id;\n\nreturn [{\n  json: {\n    chat_id: chatId,\n    text: \"No Order\",\n   \n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "fcca715b-6f12-4baa-bfaa-7878fcf61b69",
      "name": "get Order",
      "type": "n8n-nodes-base.shopify",
      "position": [
        200,
        420
      ],
      "parameters": {
        "options": {},
        "orderId": "={{ $json.orderId }}",
        "operation": "get",
        "authentication": "accessToken"
      },
      "credentials": {
        "shopifyAccessTokenApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": false,
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "5463162c-7a0b-4cbc-b9f1-1f6e7fa03ee5",
      "name": "Clean Up Order",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        420
      ],
      "parameters": {
        "jsCode": "const items = $json.line_items || [];\n\nlet message = '\ud83e\uddfe *'+ $input.first().json.name+'*\\n\\n';\n\nitems.forEach(item => {\n  const variant = item.variant_title ? ` ${item.variant_title}` : '';\n  message += `\u2022 ${item.title}${variant} x ${item.quantity} \u2014 $${item.price}\\n`;\n});\n\nreturn [{\n  json: {\n    text: message,\n    chat_id: $('Get Order ID').first().json.chat_id,  // pass this from previous step\n  \n    message_id: $('Get Order ID').first().json.message_id,\n    \n    parse_mode: \"Markdown\",\n    reply_markup: {\n      inline_keyboard: []\n    }\n  \n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "375a5e49-cc8e-43b2-a8c3-ecdbcaff5fbf",
      "name": "Check If There's Any Order",
      "type": "n8n-nodes-base.if",
      "position": [
        220,
        0
      ],
      "parameters": {
        "options": {
          "ignoreCase": false
        },
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "7cba3954-60cd-44ac-ad53-509ae2fcb6d2",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json }}",
              "rightValue": "asdf"
            },
            {
              "id": "a2964354-a639-4621-a895-00ad314f397f",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2,
      "alwaysOutputData": false
    },
    {
      "id": "6139c466-6c0f-4faa-ad28-3fff661343e6",
      "name": "Get All Orders/Get An Order Detail",
      "type": "n8n-nodes-base.switch",
      "position": [
        -500,
        280
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Get Order Detail",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": false,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "15856982-a1b8-4ec6-8fe1-4c4dea6c27cc",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "/orders"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Get An Order",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": false,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "b64cbec5-2e9f-4466-9753-4a3dd77fe381",
                    "operator": {
                      "type": "string",
                      "operation": "startsWith"
                    },
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "/order_"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "ignoreCase": true
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "624cd9da-e921-4849-8ecd-5b4cfd281e08",
      "name": "Get Order ID",
      "type": "n8n-nodes-base.code",
      "position": [
        -120,
        420
      ],
      "parameters": {
        "jsCode": "const callbackData = $json.callback_query.data; // e.g., /order_5832437071968ACSbot\n\nconst id = callbackData.match(/^\\/order_(\\d+)/)?.[1];\n\nreturn [{\n  json: {\n    orderId: id,\n    chat_id: $json.callback_query.message.chat.id,\n    message_id: $json.callback_query.message.message_id\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "22148fd6-8503-4486-b1b1-2c1a947cddab",
      "name": "Send Orders to Telegram",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        900,
        -20
      ],
      "parameters": {
        "url": "https://api.telegram.org/bot<Access Token>/sendMessage",
        "options": {},
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "chat_id",
              "value": "={{ $json.chat_id }}"
            },
            {
              "name": "text",
              "value": "={{ $json.text }}"
            },
            {
              "name": "reply_markup",
              "value": "={{ JSON.stringify($json.reply_markup) }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "330bc89d-4f23-4401-9f18-e0d9d9743b06",
      "name": "Send Order Details",
      "type": "n8n-nodes-base.telegram",
      "position": [
        900,
        420
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "chatId": "={{ $json.chat_id }}",
        "messageId": "={{ $json.message_id }}",
        "operation": "editMessageText",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "d2e1cf3c-68b9-4064-9ba9-351c2c8b2bdf",
  "connections": {
    "Orders": {
      "main": [
        [
          {
            "node": "Send Orders to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Order": {
      "main": [
        [
          {
            "node": "Send Orders to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get Order": {
      "main": [
        [
          {
            "node": "Clean Up Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get Orders": {
      "main": [
        [
          {
            "node": "Check If There's Any Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Order ID": {
      "main": [
        [
          {
            "node": "get Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean Up Order": {
      "main": [
        [
          {
            "node": "Send Order Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Get All Orders/Get An Order Detail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If There's Any Order": {
      "main": [
        [
          {
            "node": "Orders",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get All Orders/Get An Order Detail": {
      "main": [
        [
          {
            "node": "get Orders",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Order ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}