{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "2001178d-e474-460a-9f07-37387adbb244",
      "name": "Access Denied",
      "type": "n8n-nodes-max.max",
      "position": [
        -592,
        2368
      ],
      "parameters": {
        "text": "This is a test bot. You cannot use it. To learn how to set it up, check here: https://www.youtube.com/@Aimaginelife",
        "userId": "={{ $('Max Trigger1').item.json.metadata.user_context.user_id }}",
        "additionalFields": {}
      },
      "credentials": {
        "maxApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d24b5e57-e179-465b-a6ba-55b17844834f",
      "name": "Check User",
      "type": "n8n-nodes-base.if",
      "position": [
        -576,
        2112
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "30b4a241-83e0-46ac-80c5-3908604b8355",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.metadata.user_context.user_id }}",
              "rightValue": "=50488534"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "d0956b08-0974-419a-b068-c692e265e0a4",
      "name": "Get Access Token",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        496,
        1904
      ],
      "parameters": {
        "url": "https://ngw.devices.sberbank.ru:9443/api/v2/oauth",
        "method": "POST",
        "options": {
          "redirect": {
            "redirect": {}
          },
          "allowUnauthorizedCerts": true
        },
        "sendBody": true,
        "contentType": "form-urlencoded",
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "scope",
              "value": "SALUTE_SPEECH_PERS"
            }
          ]
        },
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            },
            {
              "name": "RqUID",
              "value": "={{ 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }) }}"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "7ebae214-5c2e-495e-b642-39da65d5e324",
      "name": "Get Response",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        944,
        2000
      ],
      "parameters": {
        "url": "https://smartspeech.sber.ru/rest/v1/speech:recognize",
        "method": "POST",
        "options": {
          "redirect": {
            "redirect": {}
          },
          "allowUnauthorizedCerts": true
        },
        "sendBody": true,
        "sendQuery": true,
        "contentType": "binaryData",
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "queryParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "general"
            },
            {
              "name": "enable_profanity_filter",
              "value": "false"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "inputDataFieldName": "data"
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "3f34c236-47b6-4d1a-82b7-560e4f1dc305",
      "name": "Send Message",
      "type": "n8n-nodes-max.max",
      "position": [
        2000,
        2096
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "userId": "={{ $('Max Trigger1').item.json.metadata.user_context.user_id }}",
        "additionalFields": {}
      },
      "credentials": {
        "maxApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9c177ebd-1216-4229-8d97-e314f9dd18b4",
      "name": "Text/Attachment",
      "type": "n8n-nodes-base.switch",
      "position": [
        -272,
        2096
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Attachment",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "10d834ee-e759-4d11-a864-4927a8b032de",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.message.body.attachments[0].payload.url }}",
                    "rightValue": "file"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra",
          "renameFallbackOutput": "Other"
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "0af235ef-fab8-4564-8188-b6b4bda172db",
      "name": "Attachment Router",
      "type": "n8n-nodes-base.switch",
      "position": [
        224,
        2016
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Voice",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "0b09d62e-aaee-400e-a33e-5d43fa157109",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.body.attachments[0].type }}",
                    "rightValue": "audio"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "464585bd-7fd3-41d8-9d8a-60e32bbd00ba",
      "name": "Download Attachment",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -16,
        2016
      ],
      "parameters": {
        "url": "={{ $json.message.body.attachments[0].payload.url }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "d278ffe4-1bc5-4601-aea4-fd2f9ebe7539",
      "name": "Voice to Prompt",
      "type": "n8n-nodes-base.set",
      "position": [
        1120,
        2000
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "b6f3835d-b9d6-42d1-b738-74b731b29846",
              "name": "prompt",
              "type": "string",
              "value": "={{ $json.result[0] }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "d9dfde40-9a79-464d-b9be-0b7c992b4e79",
      "name": "Text to Prompt",
      "type": "n8n-nodes-base.set",
      "position": [
        464,
        2448
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9a930f27-a489-4d87-a21d-de563a97902b",
              "name": "prompt",
              "type": "string",
              "value": "={{ $json.message.body.text }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "35a83e22-8def-41ba-93c4-4effdf09b92c",
      "name": "Combine",
      "type": "n8n-nodes-base.merge",
      "position": [
        1392,
        2096
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "a01a27c5-2cbc-44a4-9e8e-ce9988732252",
      "name": "Max Trigger1",
      "type": "n8n-nodes-max.maxTrigger",
      "position": [
        -800,
        2112
      ],
      "parameters": {
        "additionalFields": {}
      },
      "credentials": {
        "maxApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "399cbd04-daa6-4fb0-84dc-f40b1174273e",
      "name": "Simple Memory1",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1712,
        2320
      ],
      "parameters": {
        "sessionKey": "={{ $('Max Trigger1').item.json.metadata.user_context.user_id }}",
        "sessionIdType": "customKey",
        "contextWindowLength": 10
      },
      "typeVersion": 1.3
    },
    {
      "id": "ae825377-0dbe-4427-bf5f-b923a02bcd49",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        720,
        2000
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "051abd7d-a7cd-4fa4-a99d-546125bc7c72",
      "name": "AI Agent1",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1616,
        2096
      ],
      "parameters": {
        "text": "={{ $json.prompt }}",
        "options": {
          "systemMessage": "=You are an ideal personal assistant. Your name is Betsy. Your task is to be a friendly assistant and answer questions.\n\nUser name: {{ $('Max Trigger1').item.json.metadata.user_context.display_name }}\n\nYou can sometimes mention your developer Konstantin if it fits the conversation context.\nHis telegram channel: https://t.me/adept_ecommerce\nHis youtube channel: https://www.youtube.com/@Aimaginelife\n\n\n\n## Final Reminders\n- Date and time: {{ $now }}"
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "3abca257-d6ca-48b2-b0d3-d07f2a0b013b",
      "name": "GigaChat Model1",
      "type": "n8n-nodes-gigachat.lmGigaChat",
      "position": [
        1584,
        2320
      ],
      "parameters": {
        "model": "GigaChat",
        "options": {}
      },
      "credentials": {
        "gigaChatApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "16ca0f73-987c-4dbb-915d-8b65882ef59e",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1408,
        1824
      ],
      "parameters": {
        "width": 592,
        "height": 736,
        "content": "## Max AI Chatbot with Voice Recognition\n\nBuild an intelligent chatbot for Max messenger that handles text and voice messages using Russian AI - GigaChat and Sber SmartSpeech.\n\n### How it works\n\nWhen someone messages your Max bot, the workflow checks their user ID (for access control). Text messages go straight to the AI, while voice messages are first sent to Sber SmartSpeech for transcription. The GigaChat AI then generates responses while remembering your last 10 messages. All replies are sent back through Max automatically.\n\n### Setup steps\n\n1. **Connect accounts:** Add your Max bot credentials (get them at https://business.max.ru/) to \"Max Trigger\", \"Send Message\", and \"Access Denied\" nodes. Add GigaChat credentials to \"GigaChat Model\" node. Add Sber authorization to \"Get Access Token\" and \"Get Response\" nodes.\n2. **Set access control:** Open \"Check User\" node and change the user_id (50488534) to your Max user ID. This prevents unauthorized access.\n3. **Customize personality:** Open \"AI Agent\" node and edit the system message to change bot name, behavior, and add your own links or information.\n4. **Test it:** Send a text or voice message to your Max bot!\n\n### Notes\n\nThis workflow uses Russian AI services (GigaChat for chat, Sber for voice recognition) which are great alternatives to OpenAI for Russian-speaking users."
      },
      "typeVersion": 1
    },
    {
      "id": "59aad5a6-2312-4b77-b25b-990477c1821f",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        1824
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 720,
        "content": "## Access Control\nChecks if user is authorized and blocks strangers. Use your ID instead of 50488534"
      },
      "typeVersion": 1
    },
    {
      "id": "85ae6c41-afdd-426d-b0a3-17c8a0ca534e",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        1824
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 368,
        "content": "## Attachment Router\nSeparates text messages from attachments (voice/files)."
      },
      "typeVersion": 1
    },
    {
      "id": "75d70d25-95ca-44dc-bc87-ca29c2c23fa1",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -352,
        1824
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 720,
        "content": "## Message Router\nSeparates text messages from attachments (text/files)."
      },
      "typeVersion": 1
    },
    {
      "id": "7e738d97-56ad-449d-bdf6-2f1f1bbaa94a",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        448,
        1824
      ],
      "parameters": {
        "color": 7,
        "width": 816,
        "height": 368,
        "content": "## Voice Processing\nDownloads voice, gets Sber token, transcribes audio to text."
      },
      "typeVersion": 1
    },
    {
      "id": "8d707d27-2238-4046-afb0-1be72e318605",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1520,
        1824
      ],
      "parameters": {
        "color": 7,
        "width": 400,
        "height": 640,
        "content": "## AI Response\nGigaChat generates replies with 10-message conversation memory."
      },
      "typeVersion": 1
    },
    {
      "id": "f3215a1f-dbf9-48db-a5bc-eb53593954a3",
      "name": "Sticky Note14",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1520,
        1568
      ],
      "parameters": {
        "color": 5,
        "width": 400,
        "height": 224,
        "content": "@[youtube](Mrf5TmlXkyI)"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Merge1": {
      "main": [
        [
          {
            "node": "Get Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine": {
      "main": [
        [
          {
            "node": "AI Agent1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent1": {
      "main": [
        [
          {
            "node": "Send Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check User": {
      "main": [
        [
          {
            "node": "Text/Attachment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Access Denied",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Response": {
      "main": [
        [
          {
            "node": "Voice to Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Max Trigger1": {
      "main": [
        [
          {
            "node": "Check User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory1": {
      "ai_memory": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Text to Prompt": {
      "main": [
        [
          {
            "node": "Combine",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "GigaChat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Text/Attachment": {
      "main": [
        [
          {
            "node": "Download Attachment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Text to Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Voice to Prompt": {
      "main": [
        [
          {
            "node": "Combine",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Access Token": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Attachment Router": {
      "main": [
        [
          {
            "node": "Get Access Token",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Download Attachment": {
      "main": [
        [
          {
            "node": "Attachment Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}