{
  "id": "a6cWu1OYhz9tedTW",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Play RPG in Telegream with Groq Dungeon Master",
  "tags": [],
  "nodes": [
    {
      "id": "7b59c66c-f7e9-441b-ba17-2536876fbe5f",
      "name": "Groq Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        -400,
        144
      ],
      "parameters": {
        "model": "groq/compound-mini",
        "options": {}
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c49f905b-3550-4cf1-869c-bc6cc5fe8d5d",
      "name": "Dungeon Master",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -400,
        -144
      ],
      "parameters": {
        "text": "=Player action:\n{{ $json.text }}\n",
        "options": {
          "systemMessage": "You are a Dungeon Master running a persistent Dungeons & Dragons 3.5 campaign.\n\nThis is a stateful game. Campaign memory is persisted outside of you.\nYou must explicitly output all state changes so they can be saved.\n\nCAMPAIGN ASSUMPTIONS\n- There is exactly one ongoing campaign.\n- Current campaign state is provided as context.\n- Treat provided state as authoritative.\n\nRULE SYSTEM\n- Strictly follow Dungeons & Dragons 3.5 rules.\n- Respect all class features, feats, skills, spells, HP, AC, conditions, inventory, time, and location.\n- If an action is impossible, explain why and make no state changes.\n\nDICE AUTHORIZATION AND EXECUTION (STRICT)\n- Dice rolls require explicit player authorization.\n- Authorization phrases include: \u201cI roll\u201d, \u201cRoll it\u201d, \u201cRoll now\u201d, \u201cI attempt and roll\u201d.\n\n- ONCE AUTHORIZED:\n  - You MUST roll the dice yourself.\n  - You MUST NOT ask the player to provide roll results.\n  - You MUST generate the roll and resolve the outcome.\n\n- The player is NEVER expected to supply dice results unless they explicitly say they are rolling manually.\n\n- If a roll is required but NOT authorized:\n  - Ask for authorization only.\n  - Do NOT mention modifiers or totals yet.\n\nROLL EXECUTION RULES\n- When authorized, you may roll the required dice yourself.\n- Clearly show each roll using standard notation (e.g. 1d20 + 5).\n- Do not fudge or hide rolls.\n- Apply the result strictly according to D&D 3.5 rules.\n\nSTATE UPDATE REQUIREMENT\n- State changes must still be produced exactly and completely.\n- State updates are for machine consumption, not narration.\n\nNARRATION STYLE\n- Second-person, clear, grounded prose.\n- No meta commentary.\n- Spoken-language friendly (suitable for text-to-speech).\n\nOUTPUT RULES (UPDATED \u2014 STRICT)\n\n- Output PLAIN TEXT ONLY.\n- Do NOT output JSON, markdown, tags, or section headers.\n- Do NOT include mechanics, rules explanations, dice notation, or state data in the narration.\n- Do NOT mention rolls unless explicitly authorized and already resolved.\n- State updates must NOT be narrated.\n\nCONTENT REQUIREMENTS\n- First, describe the current situation in 1\u20132 short paragraphs.\n- Then list available actions as numbered choices.\n\nCHOICES RULES\n- Provide 2\u20134 reasonable next actions.\n- If a roll is required but not yet authorized:\n  - Ask only for roll authorization.\n  - Do NOT include choices.\n\nFORMAT (EXACT \u2014 NO DEVIATION)\n\n<scene description paragraph(s)>\n\nYour options:\n1. ...\n2. ...\n3. ...\n4. ...\n\nBEHAVIORAL CONSTRAINTS\n- Never roll dice without explicit player authorization.\n- Never update state before rolls are resolved.\n- Never embed state changes in prose.\n- Never reference prompts, memory systems, or internal reasoning.\n"
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "4774287b-912d-48ce-9e43-24e4e3fe7351",
      "name": "The track of your adventure",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -304,
        16
      ],
      "parameters": {
        "sessionKey": "global_session",
        "sessionIdType": "customKey",
        "contextWindowLength": 30
      },
      "typeVersion": 1.3
    },
    {
      "id": "702c2639-f2a8-4a5d-bf77-0154bc44dbe3",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1696,
        -288
      ],
      "parameters": {
        "width": 480,
        "height": 656,
        "content": "**Dungeons & Goblins AI Dungeon Master (with voice in Telegram)**\n\nThis workflow turns n8n into a persistent Dungeons & Goblins Dungeon Master powered by a Groq model and long-term memory that can be used via Telegram voice messages.\n\n**How it works:**\n\n1. You send voice message to Telegram Bot with instructions about the game and the template uses Groq STT and TTS to generate the actions.\n\n2. The AI agent receives the full campaign state from memory.\n\n3. When an action requires a roll, the DM pauses and waits for your authorization.\n\n\n**Requirements:**\n\n- Groq API token\n\n- Telegram Bot token\n\n\n**Notes:**\n\n- Dice are rolled by the AI only when the player authorizes it\n\n- All state changes are explicitly serialized for reliable persistence\n\n- Designed for long-running campaigns with consistent rules enforcement"
      },
      "typeVersion": 1
    },
    {
      "id": "e9e3d484-e2d5-409e-b596-1dafa7094724",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        -288
      ],
      "parameters": {
        "color": 7,
        "width": 312,
        "height": 652,
        "content": "**3. Agent processes your input using saved data**"
      },
      "typeVersion": 1
    },
    {
      "id": "e55932d0-5f8d-49ef-aef8-2cd5d3a3a98e",
      "name": "Send an audio file",
      "type": "n8n-nodes-base.telegram",
      "position": [
        112,
        -144
      ],
      "parameters": {
        "chatId": "={{ $('Receive Voice Message').item.json.message.chat.id }}",
        "operation": "sendAudio",
        "binaryData": true,
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c2bdde76-802b-4c67-9491-3b785638940e",
      "name": "Paste your Groq API key",
      "type": "n8n-nodes-base.set",
      "position": [
        -784,
        -144
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "{\n  \"groq_api_key\": \"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\"\n}\n"
      },
      "typeVersion": 3.4
    },
    {
      "id": "3c9c2e21-0923-4c32-a386-ec7ad16d58d8",
      "name": "Receive Voice Message",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1152,
        -144
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c0e6b798-8600-49a9-9a69-7ddf03a06dde",
      "name": "Download Audio File",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -976,
        -144
      ],
      "parameters": {
        "fileId": "={{ $('Receive Voice Message').item.json.message.voice.file_id }}",
        "download": false,
        "resource": "file"
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "aa5b954a-b6b9-4a75-983e-0e457511ecb1",
      "name": "Speech to Text",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -608,
        -144
      ],
      "parameters": {
        "url": "https://api.groq.com/openai/v1/audio/transcriptions",
        "method": "POST",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "=https://api.telegram.org/file/bot8550262198:AAEzQuHlPjx1XumgN88ebMsUggyGOuDgCbY/{{ $('Download Audio File').item.json.result.file_path }}"
            },
            {
              "name": "model",
              "value": "whisper-large-v3"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $json.groq_api_key }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "cf13f298-b87e-4684-a83e-b5bf67824f7e",
      "name": "Text to Speech",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -64,
        -144
      ],
      "parameters": {
        "url": "https://api.groq.com/openai/v1/audio/speech",
        "method": "POST",
        "options": {
          "response": {
            "response": {}
          }
        },
        "jsonBody": "={\n    \"model\": \"playai-tts\",\n    \"input\": \"{{ $json.output.replace(/\\n/g, '\\\\n') }}\",\n    \"voice\": \"Fritz-PlayAI\",\n    \"response_format\": \"wav\"\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=bearer {{ $('Paste your Groq API key').item.json.groq_api_key }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "f94d6a18-089c-44ab-94bb-3149a2e9263d",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1200,
        -288
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 652,
        "content": "**1. Bot receives voice message and downloads it**"
      },
      "typeVersion": 1
    },
    {
      "id": "24cf687c-a971-49a8-8bab-4a631430b548",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        -288
      ],
      "parameters": {
        "color": 7,
        "width": 376,
        "height": 652,
        "content": "**2. Using your entered API key Groq parses speech to text**"
      },
      "typeVersion": 1
    },
    {
      "id": "2d892931-48c8-43af-a031-3652b73274d9",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -128,
        -288
      ],
      "parameters": {
        "color": 7,
        "width": 376,
        "height": 652,
        "content": "**4. Using your entered API key Groq generates speech from text and sends it via the Bot**"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "17c138df-755f-4ff2-98e7-b1a8e5f9780c",
  "connections": {
    "Dungeon Master": {
      "main": [
        [
          {
            "node": "Text to Speech",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Speech to Text": {
      "main": [
        [
          {
            "node": "Dungeon Master",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Text to Speech": {
      "main": [
        [
          {
            "node": "Send an audio file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Dungeon Master",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Download Audio File": {
      "main": [
        [
          {
            "node": "Paste your Groq API key",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receive Voice Message": {
      "main": [
        [
          {
            "node": "Download Audio File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Paste your Groq API key": {
      "main": [
        [
          {
            "node": "Speech to Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "The track of your adventure": {
      "ai_memory": [
        [
          {
            "node": "Dungeon Master",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    }
  }
}