{
  "id": "TYC7D93mHaQkqp7z",
  "name": "telegram_bot_for_collecting_user_feedback",
  "tags": [],
  "nodes": [
    {
      "id": "b798d99a-0ab0-4a53-b093-930ea49f9994",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1488,
        -160
      ],
      "parameters": {
        "width": 480,
        "height": 720,
        "content": "## telegram_bot_for_collecting_user_feedback\n\n### How it works\n\n1. The workflow triggers upon receiving a message via Telegram. 2. It fetches the user session from Supabase to check existing records. 3. The logic engine manages session updates and inserts to track user progress. 4. It evaluates user input to determine if specific feedback needs to be logged. 5. Finally, it sends a reply message back to the user via Telegram.\n\n### Setup steps\n\n- - [ ] Connect your Telegram Bot credentials to the trigger and reply nodes.\n- - [ ] Configure the Supabase credentials for all database nodes.\n- - [ ] Ensure the Supabase table schemas match the expected input/output fields.\n\n### Customization\n\nYou can modify the Supabase query parameters to handle different user session lifecycles or integrate additional AI processing steps."
      },
      "typeVersion": 1
    },
    {
      "id": "006852f8-0ca1-40c8-9467-2ff95fc73224",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -928,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 848,
        "height": 304,
        "content": "## Receive and process session data\n\nCaptures the incoming Telegram message, retrieves the corresponding session from Supabase, and processes the raw data."
      },
      "typeVersion": 1
    },
    {
      "id": "fecabcb4-b1e3-4c32-b4f3-28358b3ff182",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        -160
      ],
      "parameters": {
        "color": 7,
        "width": 480,
        "height": 560,
        "content": "## Manage session state and flow\n\nHandles logic flow based on existing records, including inserting new sessions and tracking run completion."
      },
      "typeVersion": 1
    },
    {
      "id": "a22e9cb4-f742-4758-b9fa-3fc214740809",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        464,
        -144
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 320,
        "content": "## Evaluate input and log history\n\nEvaluates incoming user input and logs reading history to Supabase."
      },
      "typeVersion": 1
    },
    {
      "id": "0b8b0377-0aa6-48b3-a0ef-81ecfccdbef7",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        928,
        -64
      ],
      "parameters": {
        "color": 7,
        "width": 240,
        "height": 352,
        "content": "## Send telegram reply to user\n\nSends the final response message back to the Telegram user."
      },
      "typeVersion": 1
    },
    {
      "id": "e971f4f2-0d7d-4f28-a8cb-ebec3c8c30ba",
      "name": "When Telegram Message Received",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -880,
        128
      ],
      "parameters": {
        "updates": [
          "message",
          "callback_query"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "2dadc5a3-dc81-4bef-af44-bbac5a5bcd04",
      "name": "Get Session from Supabase",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -656,
        128
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "keyValue": "={{ $json.message?.chat?.id?.toString() || $json.callback_query?.message?.chat?.id?.toString() }}"
            }
          ]
        },
        "tableId": "sessions",
        "operation": "get"
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "caf7082e-9e9e-42ef-97f1-ac31ca0972b7",
      "name": "Evaluate Feedback Logic",
      "type": "n8n-nodes-base.code",
      "position": [
        -448,
        128
      ],
      "parameters": {
        "jsCode": "const triggerData = $('When Telegram Message Received').first().json;\nlet dbData = $('Get Session from Supabase').first().json || {};\n\nif (Array.isArray(dbData)) {\n  dbData = dbData[0] || {};\n}\n\nlet chatId = \"\";\nlet msg = \"\";\nlet isCallback = false;\n\nif (triggerData.callback_query) {\n  chatId = triggerData.callback_query.message?.chat?.id?.toString();\n  msg = triggerData.callback_query.data || \"\";\n  isCallback = true;\n} else {\n  chatId = triggerData.message?.chat?.id?.toString();\n  msg = triggerData.message?.text || \"\";\n}\n\nif (!chatId) return { json: { stop: true } };\n\nlet currentState = dbData.state || \"START\";\nlet mode = dbData.mode || \"\";\nlet p1 = dbData.p1_data || \"\";\nlet p2 = dbData.p2_data || \"\";\n\nlet reply = \"\";\nlet nextState = \"\";\nlet showButtons = false;\nlet isComplete = false; \n\nconst recordExists = !!(dbData && dbData.chat_id);\n\nif (msg.toLowerCase().includes(\"/start\")) {\n  currentState = \"START\";\n}\n\nswitch (currentState) {\n  case \"START\":\n    reply = \"Welcome! Choose: \ud83d\udd2e <b>Chart</b> or \ud83d\udc9e <b>Synastry</b>?\";\n    nextState = \"AWAITING_CHOICE\";\n    showButtons = true;\n    p1 = \"\";\n    p2 = \"\";\n    mode = \"\";\n    break;\n\n  case \"AWAITING_CHOICE\":\n    if (msg.toLowerCase().includes(\"natal\") || msg.toLowerCase().includes(\"chart\") || msg.toLowerCase().includes(\"btn_natal\")) {\n      mode = \"natal\";\n      reply = \"\ud83d\udd2e <b>Personal Chart Mode</b>\\nSend your birth data (City, DD/MM/YYYY HH:mm):\";\n      nextState = \"AWAITING_NATAL_DATA\";\n    } else {\n      mode = \"synastry\";\n      reply = \"\ud83d\udc9e <b>Synastry Mode</b>\\nSend Person 1 data (City, DD/MM/YYYY HH:mm):\";\n      nextState = \"AWAITING_P1\";\n    }\n    showButtons = false;\n    break;\n\n  case \"AWAITING_NATAL_DATA\":\n    p1 = msg;\n    reply = `\u2728 <b>Reading stars for your chart:</b>\\n${p1}... \u2728`;\n    nextState = \"START\";\n    showButtons = false;\n    isComplete = true;\n    break;\n\n  case \"AWAITING_P1\":\n    p1 = msg;\n    reply = \"\u2705 Got Person 1 details.\\n\\n\ud83d\udc49 Now send <b>Person 2 data</b> (City, DD/MM/YYYY HH:mm):\";\n    nextState = \"AWAITING_P2\";\n    showButtons = false;\n    break;\n\n  case \"AWAITING_P2\":\n    p2 = msg;\n    reply = `\ud83c\udf0c <b>Comparing souls</b> \ud83c\udf0c\\n\\n\ud83d\udc64 <b>P1:</b> ${p1}\\n\ud83d\udc64 <b>P2:</b> ${p2}\\n\\nAnalyzing cosmic matrices... \u2728`;\n    nextState = \"START\";\n    showButtons = false;\n    isComplete = true;\n    break;\n}\n\nreturn {\n  json: {\n    chatId,\n    reply,\n    nextState,\n    mode,\n    p1_data: p1,\n    p2_data: p2,\n    showButtons,\n    recordExists,\n    isComplete,\n    callbackQueryId: isCallback ? triggerData.callback_query.id : null\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "f5658180-ef3d-43c8-b16e-cf9dbde033be",
      "name": "Check If Record Exists",
      "type": "n8n-nodes-base.if",
      "position": [
        -224,
        128
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "check_exists",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.recordExists }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "e887c975-9a33-4c0d-baef-3c059867ef4a",
      "name": "Update Session in Supabase",
      "type": "n8n-nodes-base.supabase",
      "position": [
        0,
        0
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "keyValue": "={{ $json.chatId }}",
              "condition": "eq"
            }
          ]
        },
        "tableId": "sessions",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "state",
              "fieldValue": "={{ $json.nextState }}"
            },
            {
              "fieldId": "mode",
              "fieldValue": "={{ $json.mode }}"
            },
            {
              "fieldId": "p1_data",
              "fieldValue": "={{ $json.p1_data }}"
            },
            {
              "fieldId": "p2_data",
              "fieldValue": "={{ $json.p2_data }}"
            }
          ]
        },
        "operation": "update"
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2b7ec32c-3d7b-4685-b557-4b5011ec6ab2",
      "name": "Insert Session into Supabase",
      "type": "n8n-nodes-base.supabase",
      "position": [
        0,
        240
      ],
      "parameters": {
        "tableId": "sessions",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "chat_id",
              "fieldValue": "={{ $json.chatId }}"
            },
            {
              "fieldId": "state",
              "fieldValue": "={{ $json.nextState }}"
            },
            {
              "fieldId": "mode",
              "fieldValue": "={{ $json.mode }}"
            },
            {
              "fieldId": "p1_data",
              "fieldValue": "={{ $json.p1_data }}"
            },
            {
              "fieldId": "p2_data",
              "fieldValue": "={{ $json.p2_data }}"
            }
          ]
        }
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7c719819-a5e7-46a6-a9e6-819159131340",
      "name": "Send Telegram Reply",
      "type": "n8n-nodes-base.telegram",
      "position": [
        976,
        128
      ],
      "parameters": {
        "text": "={{ $('Evaluate Feedback Logic').item.json.reply }}",
        "chatId": "={{ $('Evaluate Feedback Logic').item.json.chatId }}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e8d24aa9-a5da-4447-a431-b1290a57586f",
      "name": "Check If Run Complete",
      "type": "n8n-nodes-base.if",
      "position": [
        288,
        240
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "is_complete",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $('Check If Record Exists').item.json.isComplete }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "68b88e1e-ed76-4d23-ae32-fac1578d0d2b",
      "name": "Log Reading History in Supabase",
      "type": "n8n-nodes-base.supabase",
      "position": [
        752,
        -16
      ],
      "parameters": {
        "tableId": "astrology_readings",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "chat_id",
              "fieldValue": "={{ $json.chat_id }}"
            },
            {
              "fieldId": "mode",
              "fieldValue": "={{ $json.mode }}"
            },
            {
              "fieldId": "p1_data",
              "fieldValue": "={{ $json.p1_data }}"
            },
            {
              "fieldId": "p2_data",
              "fieldValue": "={{ $json.p2_data }}"
            }
          ]
        }
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "add0d731-f79d-4cc8-9367-52f598f33fda",
      "name": "Check for User Input",
      "type": "n8n-nodes-base.if",
      "position": [
        512,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "is_complete",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.p1_data }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "c9c35c7d-5b2e-4822-ac42-4d04dfb91769",
  "connections": {
    "Check for User Input": {
      "main": [
        [
          {
            "node": "Log Reading History in Supabase",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Telegram Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Run Complete": {
      "main": [
        [
          {
            "node": "Send Telegram Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Check for User Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Record Exists": {
      "main": [
        [
          {
            "node": "Update Session in Supabase",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Insert Session into Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evaluate Feedback Logic": {
      "main": [
        [
          {
            "node": "Check If Record Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Session from Supabase": {
      "main": [
        [
          {
            "node": "Evaluate Feedback Logic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Session in Supabase": {
      "main": [
        [
          {
            "node": "Check for User Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Session into Supabase": {
      "main": [
        [
          {
            "node": "Check If Run Complete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Telegram Message Received": {
      "main": [
        [
          {
            "node": "Get Session from Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Reading History in Supabase": {
      "main": [
        [
          {
            "node": "Send Telegram Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}