{
  "id": "X7pMjJBOk8H2tj1W",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI Audio Assistant",
  "tags": [],
  "nodes": [
    {
      "id": "ccbc26b7-4e64-467f-844f-287bb51d201b",
      "name": "Transcribe with Deepgram",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1040,
        -420
      ],
      "parameters": {
        "url": "https://api.deepgram.com/v1/listen?model=nova-2&language=en&smart_format=true",
        "method": "POST",
        "options": {
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        },
        "sendBody": true,
        "contentType": "binaryData",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Token {{YOUR DEEP GRAM TOKEN HERE}}"
            },
            {
              "name": "Content-Type",
              "value": "audio/ogg"
            }
          ]
        },
        "inputDataFieldName": "data"
      },
      "typeVersion": 3
    },
    {
      "id": "42cb24ce-0ec0-4e29-961f-6aa3271e57c0",
      "name": "Download Voice File",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        680,
        -420
      ],
      "parameters": {
        "url": "=https://api.telegram.org/file/bot{{ $json.telegramToken }}/{{ $json.result.file_path }}",
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "cf10e5a6-08ce-4d6f-bda2-079e4d0cc51f",
      "name": "Get File Path",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        760,
        -280
      ],
      "parameters": {
        "url": "=https://api.telegram.org/bot{{ $json.telegramToken }}/getFile?file_id={{ $json.voice_file_id }}",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "220308ea-8c89-4353-9b41-976feada6d30",
      "name": "Prepare Voice Message Data",
      "type": "n8n-nodes-base.set",
      "position": [
        460,
        -280
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "e0deb0df-a5f8-4d13-8af6-92d0e1f0ad74",
              "name": "user_id",
              "type": "number",
              "value": "={{ $json.message.from.id }}"
            },
            {
              "id": "d5b76796-10f1-41ef-bff6-1602fb434ad8",
              "name": "chat_id",
              "type": "number",
              "value": "={{ $json.message.chat.id }}"
            },
            {
              "id": "59bda82b-6c18-40fd-b120-ed078a5d4c96",
              "name": "voice_file_id",
              "type": "string",
              "value": "={{ $json.message.voice.file_id }}"
            },
            {
              "id": "9412f498-5e52-4ea9-81a3-9d76379406c0",
              "name": "timestamp",
              "type": "string",
              "value": "={{ new Date().toISOString() }}"
            },
            {
              "id": "telegramToken",
              "name": "telegramToken",
              "type": "string",
              "value": "Token {{YOUR TELEGRAM TOKEN HERE}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "8c1732d1-b1d6-492e-a1c8-ed73000fc2ac",
      "name": "Prepare Text Message Data",
      "type": "n8n-nodes-base.set",
      "position": [
        260,
        100
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "text-user-id",
              "name": "user_id",
              "type": "number",
              "value": "={{ $json.message.from.id }}"
            },
            {
              "id": "text-chat-id",
              "name": "chat_id",
              "type": "number",
              "value": "={{ $json.message.chat.id }}"
            },
            {
              "id": "text-message-text",
              "name": "message_text",
              "type": "string",
              "value": "={{ $json.message.text }}"
            },
            {
              "id": "text-message-id",
              "name": "message_id",
              "type": "number",
              "value": "={{ $json.message.message_id }}"
            },
            {
              "id": "text-timestamp",
              "name": "timestamp",
              "type": "string",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "419734f9-3f9f-46de-a721-8e0ffb71ca0c",
      "name": "Knowledge base",
      "type": "n8n-nodes-base.supabaseTool",
      "position": [
        980,
        320
      ],
      "parameters": {
        "tableId": "knowledge_base",
        "operation": "getAll"
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "3ecb0359-7f32-4cb8-a8a7-692b3430df11",
      "name": "Telegram Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        780,
        320
      ],
      "parameters": {
        "sessionKey": "telegram_{{ $json.user_id }}",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "c0bba125-8492-4bde-a8e3-7fc1733b6d9d",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        580,
        300
      ],
      "parameters": {
        "options": {
          "maxTokens": 200,
          "temperature": 0.3
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "c0d3076c-ae1f-4fa9-85f5-1ecbcc67a91f",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        760,
        100
      ],
      "parameters": {
        "text": "={{ $json.body?.results?.channels?.[0]?.alternatives?.[0]?.transcript || $json.message_text || $json.message?.text || 'Hello' }}",
        "options": {
          "systemMessage": "You are a helpful and professional AI assistant for voice and text interactions. Your role is to:\n\n1. Understand customer inquiries and provide accurate, helpful responses\n2. Use the knowledge base tool to find relevant information when needed\n3. Maintain a friendly, professional tone\n4. Provide clear, concise responses suitable for both voice and text delivery\n5. Always aim to provide value and resolve customer concerns\n\nImportant guidelines:\n- Be empathetic and understanding\n- Provide clear, actionable responses\n- Keep responses concise for voice delivery (under 200 words)\n- If information is not in the knowledge base, say so honestly\n- For complex technical issues, recommend escalation to human support\n\nCompany context: You represent a professional service business focused on customer satisfaction and quality support."
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "763b8a6a-0a33-48a9-a18f-5098fe35e5cc",
      "name": "Has Transcript?",
      "type": "n8n-nodes-base.if",
      "position": [
        1080,
        -160
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.body && $json.body.results && $json.body.results.channels && $json.body.results.channels[0] && $json.body.results.channels[0].alternatives && $json.body.results.channels[0].alternatives[0] && $json.body.results.channels[0].alternatives[0].transcript }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8f702d09-8595-4d79-bc7d-442f0a41052e",
      "name": "Is Voice Message?",
      "type": "n8n-nodes-base.if",
      "position": [
        240,
        -140
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "876dc746-b9cb-4b7f-a0e2-1542f67fbe05",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ Boolean($json.message.voice) }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a1747da7-7e04-4766-b9bd-b3d838f956dc",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -20,
        -140
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "69cdbf6f-ee97-494a-9f3c-a99ddc917093",
      "name": "Generate Audio Response",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1420,
        -40
      ],
      "parameters": {
        "url": "https://api.deepgram.com/v1/speak",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendQuery": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "text",
              "value": "={{ $json.output }}"
            }
          ]
        },
        "queryParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "aura-asteria-en"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Token {{YOUR DEEP GRAM TOKEN HERE}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "e67b288f-1946-430b-9a45-0e62283c1d6b",
      "name": "Send Audio Response",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1680,
        -80
      ],
      "parameters": {
        "chatId": "={{ $('Prepare Voice Message Data').item.json.chat_id }}",
        "operation": "sendAudio",
        "binaryData": true,
        "additionalFields": {
          "caption": "\ud83c\udfa4 Voice response from AI Assistant"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8e409205-cad6-41a9-81cf-177c054a7b12",
      "name": "Workflow Guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -580,
        -500
      ],
      "parameters": {
        "color": 6,
        "width": 360,
        "height": 1160,
        "content": "## \ud83c\udf99\ufe0f VoiceFlow AI: Telegram + Deepgram + OpenAI + Supabase Audio Assistant\n\n**Who is this for?**\nBusinesses, customer service teams, content creators, and organizations who want to provide intelligent voice-based interactions through Telegram. Perfect for accessibility-focused services, multilingual support, or hands-free customer assistance.\n\n**What problem does this solve?**\n- Enables natural voice conversations with AI\n- Breaks down language and accessibility barriers  \n- Provides instant voice responses to customer queries\n- Reduces typing requirements for users\n- Offers 24/7 voice-based customer support\n- Maintains conversation context across voice interactions\n- Handles both voice and text messages seamlessly\n\n**What this workflow does:**\n1. Receives voice OR text messages via Telegram bot\n2. For voice: Transcribes audio using Deepgram's speech-to-text API\n3. Processes queries through AI agent with knowledge base access\n4. Generates intelligent responses based on conversation context\n5. For voice input: Converts AI response to natural speech using Deepgram TTS\n6. Sends appropriate response (audio for voice, text for text) back to user\n7. Maintains conversation memory for contextual interactions\n\n**Setup Instructions:**\n1. Get Telegram bot token from @BotFather\n2. Sign up for Deepgram API (STT + TTS services)\n3. Configure OpenAI API key for AI responses\n4. Set up Supabase knowledge base table\n5. Update API credentials in workflow nodes\n6. Test with both voice and text messages\n\n**Key Features:**\n- Dual input support (voice and text)\n- Intelligent response routing\n- Voice-to-voice conversation capability\n- Knowledge base integration\n- Conversation memory\n- Professional AI responses"
      },
      "typeVersion": 1
    },
    {
      "id": "f0600a4a-5671-4cbf-9769-b7a3fcb64474",
      "name": "Was Voice Input?",
      "type": "n8n-nodes-base.if",
      "position": [
        1120,
        100
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "voice-input-check",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ Boolean($('Telegram Trigger').item.json.message.voice) }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "d0d0d41a-fa55-4e6b-8b7f-c52904af284e",
      "name": "Send Text Response",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1460,
        180
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "={{ $('Prepare Text Message Data').item.json.chat_id }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "59537604-b414-4298-9408-8dfbcdf8e213",
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Was Voice Input?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get File Path": {
      "main": [
        [
          {
            "node": "Download Voice File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Knowledge base": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Has Transcript?": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Is Voice Message?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Was Voice Input?": {
      "main": [
        [
          {
            "node": "Generate Audio Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Text Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Voice Message?": {
      "main": [
        [
          {
            "node": "Prepare Voice Message Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare Text Message Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Download Voice File": {
      "main": [
        [
          {
            "node": "Transcribe with Deepgram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Audio Response": {
      "main": [
        [
          {
            "node": "Send Audio Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transcribe with Deepgram": {
      "main": [
        [
          {
            "node": "Has Transcript?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Text Message Data": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Voice Message Data": {
      "main": [
        [
          {
            "node": "Get File Path",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}