{
  "name": "TechPrana Study Buddy (Telegram + Gemini)",
  "nodes": [
    {
      "parameters": {
        "content": "## 📚 TechPrana Study Buddy (free n8n agent)\n\nA Telegram study tutor powered by Google Gemini's free API tier.\n\n**Setup (about 10 minutes):**\n1. Create a bot with **@BotFather** on Telegram → copy the token.\n2. Get a free API key at **aistudio.google.com** → *Get API key*.\n3. Open the **Telegram Trigger** node → create a *Telegram API* credential with your token. Select the same credential in the 2 other Telegram nodes.\n4. Open **Google Gemini Chat Model** → create a *Google Gemini (PaLM) API* credential with your key.\n5. Save, then click **Publish** (top right) to switch it on. Message your bot **/start**.\n\nCommands: /explain, /eli5, /quiz, /flashcards, /summary, /plan, /revise\n\nMore free agents: **techprana.org/agents**",
        "height": 520,
        "width": 420,
        "color": 6
      },
      "id": "09df344b-be9a-4075-b52e-c1f6025b2fdd",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        -200
      ]
    },
    {
      "parameters": {
        "content": "### ⏰ Optional: daily revision reminder\nThis branch is **off** by default.\n1. Message your bot once, then open the last execution of this workflow → copy *message.chat.id*.\n2. Paste it into **Daily revision prompt** → *chatId*.\n3. Set the time in **Every evening**, enable both nodes (select them → right-click → *Activate*), then **Publish** again.",
        "height": 260,
        "width": 420,
        "color": 4
      },
      "id": "e069a012-3f96-4542-a619-31454291bc4e",
      "name": "Note: daily reminder",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        360
      ]
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "deda0cad-c599-4c72-9752-ba3843bfa8db",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "webhookId": "dc22af72-cd4a-4fd8-8a6c-dbacbc2a8d47"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "3b56a40b-01ee-4c1f-8368-7cf869e95bed",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "29f2e8d5-50f7-43ef-9941-d419237df047",
      "name": "Is it text?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e17e1b96-9abd-4961-9b22-88623fdcd627",
              "name": "chatId",
              "value": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
              "type": "string"
            },
            {
              "id": "290e79dc-804d-4138-9cfd-df6646b978f8",
              "name": "text",
              "value": "={{ $('Telegram Trigger').item.json.message.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "4aee99fd-a1d4-45d6-95bb-190d0721e951",
      "name": "Prepare input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        660,
        -100
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "sendChatAction",
        "chatId": "={{ $json.message.chat.id }}",
        "action": "typing"
      },
      "id": "d1c0e3ac-adb9-4f89-aa81-d517eaeda9d5",
      "name": "Show typing…",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        440,
        -100
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.message.chat.id }}",
        "text": "I can read text messages for now 🙂 Please type or paste your notes or question as text. Send /start to see what I can do.",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "HTML"
        }
      },
      "id": "6920682b-c9ae-49c5-8adb-7ddb322fba5c",
      "name": "Reply: text only",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        440,
        140
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 19
            }
          ]
        }
      },
      "id": "f57ae758-f578-4e1c-a698-9e88c322b4d5",
      "name": "Every evening",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        380
      ],
      "disabled": true
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f3c377c0-d3af-4683-b4ef-bfda1d0d23ad",
              "name": "chatId",
              "value": "PASTE_YOUR_CHAT_ID",
              "type": "string"
            },
            {
              "id": "e544b1fc-d1e0-4625-90b1-6a99028ab50a",
              "name": "text",
              "value": "[DAILY REVISION] Send me one short revision question.",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "c89586a6-3caa-48e2-a878-853f2a644962",
      "name": "Daily revision prompt",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        660,
        380
      ],
      "disabled": true
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.text }}",
        "options": {
          "systemMessage": "You are TechPrana Study Buddy, a friendly, patient tutor that chats with a student on Telegram.\n\nYOUR JOB\nHelp the student understand topics, practise with quizzes and flashcards, summarise their notes, and plan their study time.\n\nCOMMANDS (the student may also just ask normally)\n/start - Greet them in 2-3 lines and list the commands below with one-line descriptions.\n/explain <topic> - Explain the topic simply: a one-line definition, then 3-5 short points, then one everyday example, then \"Want a quick quiz on this? Send /quiz\".\n/eli5 <topic> - Explain like they are 10 years old, using an analogy. Max 120 words.\n/quiz <topic> - Run a 5-question multiple-choice quiz. Ask ONE question at a time with options A-D and wait for their answer. After each answer say right or wrong, give a one-line reason, then ask the next question. At the end give the score out of 5 and the 2 ideas they should revise.\n/flashcards <topic> - Give 8 flashcards in the format \"Q: ... / A: ...\" with short answers.\n/summary - If they paste notes after the command (or in the next message), summarise them into: 5 key points, 3 terms to remember with meanings, and 2 likely exam questions.\n/plan <exam date and subjects> - Make a simple day-by-day study plan up to the exam date, with short daily sessions, revision days and one rest day per week. Ask for the date and subjects if they are missing.\n/revise - Ask them one quick question about something you discussed earlier in this chat. If there is nothing yet, ask what they are studying.\n\nRULES\n- Be accurate. If you are not sure about a fact, say so plainly. Never invent facts, dates, formulas or quotes.\n- For maths and science, show the steps.\n- Do not just hand over answers to homework or tests: explain the method and let the student try first, then confirm or correct.\n- Reply in the same language the student writes in (for example Hindi, Hinglish or English).\n- This is Telegram: write plain text only. Do NOT use Markdown symbols such as **, __, # or backticks. Use emojis, line breaks, numbers and simple dashes for structure.\n- Keep each reply under 3,000 characters. If a topic needs more, give part one and ask \"Shall I continue?\".\n- Be warm and encouraging, but keep it short. No long introductions.\n- Stay on studying and learning. If asked for something harmful, unsafe or unrelated, politely steer back to studying.\n- If the message starts with [DAILY REVISION], it was sent by the daily reminder, not the student: send one short, friendly revision question based on what you discussed recently, or ask what they are studying today if there is no history.",
          "maxIterations": 3
        }
      },
      "id": "4d8803a1-7652-45ea-8065-2c6b53cd6908",
      "name": "Study Buddy agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        900,
        0
      ]
    },
    {
      "parameters": {
        "modelName": "models/gemini-3.5-flash-lite",
        "options": {
          "maxOutputTokens": 2048,
          "temperature": 0.4
        }
      },
      "id": "239c1aee-6e83-4485-89a9-996a23384408",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        840,
        240
      ]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.chatId }}",
        "contextWindowLength": 12
      },
      "id": "13f30e6f-32bb-42c0-ac65-0abbdb1c2137",
      "name": "Chat memory (per student)",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1000,
        240
      ]
    },
    {
      "parameters": {
        "jsCode": "// Telegram allows max 4096 characters per message, so long replies are split.\n// Text is HTML-escaped because the Telegram node always sends a parse mode (HTML here).\nconst src = $('Prepare input').isExecuted ? $('Prepare input').first().json : $('Daily revision prompt').first().json;\nconst chatId = src.chatId;\nconst esc = (t) => t.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\nconst out = [];\nfor (const item of $input.all()) {\n  let text = String(item.json.output ?? '').trim() || 'Sorry, I could not answer that. Please try again.';\n  while (text.length > 0) {\n    let cut = 3500;\n    if (text.length > cut) {\n      const nl = text.lastIndexOf('\\n', cut);\n      if (nl > 1000) cut = nl;\n    }\n    out.push({ json: { chatId, text: esc(text.slice(0, cut)) } });\n    text = text.slice(cut).trim();\n  }\n}\nreturn out;"
      },
      "id": "67e785b7-1fbc-462c-bac9-db77639e64a3",
      "name": "Split long replies",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1260,
        0
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "={{ $json.text }}",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "HTML",
          "disable_web_page_preview": true
        }
      },
      "id": "e2483173-21a8-4336-8860-607ccdebf545",
      "name": "Send reply",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1480,
        0
      ]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Is it text?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is it text?": {
      "main": [
        [
          {
            "node": "Show typing…",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reply: text only",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Show typing…": {
      "main": [
        [
          {
            "node": "Prepare input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare input": {
      "main": [
        [
          {
            "node": "Study Buddy agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Every evening": {
      "main": [
        [
          {
            "node": "Daily revision prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily revision prompt": {
      "main": [
        [
          {
            "node": "Study Buddy agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Study Buddy agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Chat memory (per student)": {
      "ai_memory": [
        [
          {
            "node": "Study Buddy agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Study Buddy agent": {
      "main": [
        [
          {
            "node": "Split long replies",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split long replies": {
      "main": [
        [
          {
            "node": "Send reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}