Готовый шаблон для n8n на замену внезапно исчезнувшему Twisty боту

Итак, Twisty бот внезапно с нами попрощался. Для многих админов это означало потерю удобного способа общения с подписчиками через систему топиков. И я решила набросать в n8n замену его функциональности — бота формы обратной связи.

Готовый шаблон для n8n на замену внезапно исчезнувшему Twisty боту

Задача:

1. Пользователь пишет в бота.
2. Создается топик в закрытой группе, и сообщения пересылаются туда.
3. Вы, как админ, общаетесь в этом топике, и бот отправляет сообщения пользователю обратно в бот.

Очень красиво и удобно.

Проблемы:

1. База данных. Хотела сделать его совсем без внешних сервисов, но в API, как я понимаю, нельзя нормально поискать топики. Поэтому я решила, что для меня меньшим из зол будет сохранять в базу маппинг chat_id и topic_id. Можно переделать под Excel-лист или что-то вроде того для еще большей простоты. Но нужен запрос на это :)

2. Редактирование сообщений. Когда пользователь редактирует сообщение, я решила упростить процесс и просто дополнительно отправлять отредактированное сообщение, а не менять исходное. Не очень красиво, но быстро и работает.

3. Редактирование от админа. То же самое происходит и с отредактированными сообщениями от админа.

Поиск ID приватной группы — не тривиальная задача. Мне помог в этом @LeadConverterToolkitBot. Вы можете использовать то, что удобно вам.

Инструкции для использования:

1. Замените {{YOUR_BOT_TOKEN}} на токен вашего Telegram-бота

2. Замените YOUR_ADMIN_GROUP_ID на ID вашей админской группы

3. Настройте credentials для Telegram API и Supabase

4. Создайте таблицу в Supabase с полями user_chat_id и topic_id

5. Активируйте workflow после настройки

Угощайтесь и подписывайтесь на мой канал Мама стартапит :)

{ "name": "Telegram Support Bot - Feedback System", "nodes": [ { "parameters": { "updates": [ "message", "edited_message" ], "additionalFields": {} }, "id": "telegram-trigger", "name": "Telegram Trigger", "type": "n8n-nodes-base.telegramTrigger", "position": [ 112, 304 ], "typeVersion": 1.2, "webhookId": "WEBHOOK_ID_PLACEHOLDER", "credentials": { "telegramApi": { "id": "TELEGRAM_CREDENTIALS_ID", "name": "YOUR_BOT_NAME" } } }, { "parameters": { "assignments": { "assignments": [ { "id": "user_id", "name": "user_id", "type": "string", "value": "={{ $json.message.from.id }}" }, { "id": "user_name", "name": "user_name", "type": "string", "value": "={{ ($json.message.from.first_name || '') + ' ' + ($json.message.from.last_name || '') }}" }, { "id": "username", "name": "username", "type": "string", "value": "={{ $json.message.from.username || 'No username' }}" }, { "id": "message_text", "name": "message_text", "type": "string", "value": "={{ $json.message.text || '[No text content]' }}" }, { "id": "admin_group_id", "name": "admin_group_id", "type": "string", "value": "YOUR_ADMIN_GROUP_ID" }, { "id": "timestamp", "name": "timestamp", "type": "string", "value": "={{ new Date().toISOString() }}" }, { "id": "a9a11fc3-a5b9-49d9-a352-4e8b78f0660a", "name": "chat_id", "value": "={{ $json.message.chat.id }}", "type": "string" } ] }, "options": {} }, "id": "process-user-message", "name": "Process User Message", "type": "n8n-nodes-base.set", "position": [ 480, 176 ], "typeVersion": 3.4 }, { "parameters": { "assignments": { "assignments": [ { "id": "reply_text", "name": "reply_text", "type": "string", "value": "={{ $json.message.text }}" }, { "id": "reply_timestamp", "name": "reply_timestamp", "type": "string", "value": "={{ new Date().toISOString() }}" } ] }, "options": {} }, "id": "process-admin-reply", "name": "Process Admin Reply", "type": "n8n-nodes-base.set", "position": [ 720, 496 ], "typeVersion": 3.4 }, { "parameters": {}, "id": "ignore-message", "name": "Ignore Message", "type": "n8n-nodes-base.noOp", "position": [ 784, 960 ], "typeVersion": 1 }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 1 }, "conditions": [ { "id": "f388a84c-6bb6-4c9b-a17c-2df723d55411", "leftValue": "={{ $json.message.chat.type }}", "rightValue": "private", "operator": { "type": "string", "operation": "equals", "name": "filter.operator.equals" } } ], "combinator": "and" }, "options": {} }, "id": "check-message-type", "name": "Is private chat", "type": "n8n-nodes-base.if", "position": [ 304, 304 ], "typeVersion": 2 }, { "parameters": { "method": "POST", "url": "=https://api.telegram.org/bot{{YOUR_BOT_TOKEN}}/createForumTopic", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"chat_id\": \"{{ $('Process User Message').item.json.admin_group_id}}\", \n \"name\": \"{{ $('Process User Message').item.json.user_name }}\",\n \"icon_color\": 7322096\n}", "options": {} }, "id": "bfbd8f5a-8e21-4b4f-bf59-bdfbb1f44eb0", "name": "Create Forum Topic", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 976, 64 ] }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [ { "id": "451df91a-4161-499c-ab64-5fbd62e6f66e", "leftValue": "={{ $('Telegram Trigger').item.json.message.entities[0].type }}", "rightValue": "bot_command", "operator": { "type": "string", "operation": "notEquals" } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 1328, 64 ], "id": "53f2ebf9-fc5f-4197-90d0-260bddc2076c", "name": "Is NOT a command" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [ { "id": "0c44d1a4-e161-4f94-bca8-e74f43896c57", "leftValue": "={{ $json }}", "rightValue": "", "operator": { "type": "object", "operation": "empty", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 768, 176 ], "id": "caf7bb70-67cb-4dcc-9007-c6316196c658", "name": "First contact?" }, { "parameters": { "chatId": "={{ $('Process User Message').item.json.admin_group_id }}", "text": "=💬 <b>Новый чат создан</b>\n\n👤 <b>Имя:</b> {{ $('Process User Message').item.json.user_name.trim() }}\n🆔 <b>ИД:</b> {{ $('Process User Message').item.json.user_id }}\n👤 <b>Логин:</b> @{{ $('Process User Message').item.json.username }}\n\n💬 <b>Сообщение:</b>\n{{ $('Process User Message').item.json.message_text }}", "additionalFields": { "appendAttribution": false, "parse_mode": "HTML", "message_thread_id": "={{ $json.topic_id }}" } }, "id": "5c9b8331-f3eb-445b-adae-e8ed6f0df0fa", "name": "Send to Admin Group2", "type": "n8n-nodes-base.telegram", "position": [ 1584, 160 ], "typeVersion": 1.2, "webhookId": "WEBHOOK_ID_PLACEHOLDER_2", "credentials": { "telegramApi": { "id": "TELEGRAM_CREDENTIALS_ID", "name": "YOUR_BOT_NAME" } } }, { "parameters": { "method": "POST", "url": "=https://api.telegram.org/bot{{YOUR_BOT_TOKEN}}/copyMessage", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"chat_id\": \"{{ $('Process User Message').item.json.admin_group_id }}\", \n \"from_chat_id\": \"{{ $json.user_chat_id }}\",\n \"message_thread_id\": {{ $json.topic_id }},\n \"message_id\": {{ $('Telegram Trigger').item.json.message.message_id }}\n}", "options": {} }, "id": "c0c4e73e-b30f-42ee-9661-d36a442d0cdb", "name": "Copy a message", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 976, 272 ] }, { "parameters": { "method": "POST", "url": "=https://api.telegram.org/bot{{YOUR_BOT_TOKEN}}/copyMessage", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"chat_id\": \"{{ $('Process User Message').item.json.admin_group_id }}\", \n \"from_chat_id\": \"{{ $json.user_chat_id }}\",\n \"message_thread_id\": {{ $json.topic_id }},\n \"message_id\": {{ $('Telegram Trigger').item.json.message.message_id }}\n}", "options": {} }, "id": "a7d6f6fb-2acd-4898-bf27-baa395787dc9", "name": "Copy a message1", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1584, -32 ] }, { "parameters": { "method": "POST", "url": "=https://api.telegram.org/bot{{YOUR_BOT_TOKEN}}/copyMessage", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"chat_id\": \"{{ $json.user_chat_id }}\", \n \"from_chat_id\": \"{{ $('Telegram Trigger').item.json.message.chat.id}}\",\n \"message_id\": {{ $('Telegram Trigger').item.json.message.message_id }}\n}", "options": {} }, "id": "819a8a17-d08b-4f7c-b205-3e1202f890bf", "name": "Copy a message2", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1072, 496 ] }, { "parameters": { "method": "POST", "url": "=https://api.telegram.org/bot{{YOUR_BOT_TOKEN}}/copyMessage", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"chat_id\": \"{{ $json.result.chat.id }}\", \n \"from_chat_id\": \"{{ $('Get user details').item.json.user_chat_id }}\",\n \"message_thread_id\": {{ $('Get user details').item.json.topic_id }},\n \"message_id\": {{ $('Telegram Trigger').item.json.edited_message.message_id }}\n}", "options": {} }, "id": "6a0e5c48-463a-424e-99b5-2f668e82c4ab", "name": "Copy a message3", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1200, 720 ] }, { "parameters": { "operation": "get", "tableId": "YOUR_TABLE_NAME", "filters": { "conditions": [ { "keyName": "user_chat_id", "keyValue": "={{ $json.chat_id }}" } ] } }, "type": "n8n-nodes-base.supabase", "typeVersion": 1, "position": [ 624, 176 ], "id": "8bf88d40-2f8b-44dc-a105-3db05305ec3e", "name": "Get user", "alwaysOutputData": true, "credentials": { "supabaseApi": { "id": "SUPABASE_CREDENTIALS_ID", "name": "Your Supabase Connection" } } }, { "parameters": { "tableId": "YOUR_TABLE_NAME", "fieldsUi": { "fieldValues": [ { "fieldId": "user_chat_id", "fieldValue": "={{ $('Telegram Trigger').item.json.message.chat.id }}" }, { "fieldId": "topic_id", "fieldValue": "={{ $json.result.message_thread_id }}" } ] } }, "type": "n8n-nodes-base.supabase", "typeVersion": 1, "position": [ 1152, 64 ], "id": "8adee532-aef0-441e-8311-2110db754fcf", "name": "Save user data", "credentials": { "supabaseApi": { "id": "SUPABASE_CREDENTIALS_ID", "name": "Your Supabase Connection" } } }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 1 }, "conditions": [ { "id": "e86a2837-c4f3-4936-a6d1-7dd7c8eb3d3c", "leftValue": "={{ $json.message.reply_to_message }}", "rightValue": "true", "operator": { "type": "object", "operation": "notEmpty", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "id": "check-if-reply", "name": "Check if admin reply", "type": "n8n-nodes-base.if", "position": [ 496, 512 ], "typeVersion": 2 }, { "parameters": { "operation": "get", "tableId": "YOUR_TABLE_NAME", "filters": { "conditions": [ { "keyName": "topic_id", "keyValue": "={{ $('Telegram Trigger').item.json.message.message_thread_id }}" } ] } }, "type": "n8n-nodes-base.supabase", "typeVersion": 1, "position": [ 912, 496 ], "id": "1ef2ecda-7f71-4ee5-8564-750ef04bc2e1", "name": "Get user data", "alwaysOutputData": true, "credentials": { "supabaseApi": { "id": "SUPABASE_CREDENTIALS_ID", "name": "Your Supabase Connection" } } }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [ { "id": "6012bb1a-363c-45f7-b213-5a0ec872838f", "leftValue": "={{ $json.edited_message }}", "rightValue": "", "operator": { "type": "object", "operation": "exists", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 608, 736 ], "id": "5a8962a8-ac1c-4678-a864-c9da41457471", "name": "If message was edited" }, { "parameters": { "operation": "get", "tableId": "YOUR_TABLE_NAME", "filters": { "conditions": [ { "keyName": "user_chat_id", "keyValue": "={{ $json.edited_message.chat.id }}" } ] } }, "type": "n8n-nodes-base.supabase", "typeVersion": 1, "position": [ 880, 720 ], "id": "b38a01bd-6f33-4aa5-a452-eaab5219cb28", "name": "Get user details", "alwaysOutputData": true, "credentials": { "supabaseApi": { "id": "SUPABASE_CREDENTIALS_ID", "name": "Your Supabase Connection" } } }, { "parameters": { "chatId": "YOUR_ADMIN_GROUP_ID", "text": "=Сообщение отредактировано:", "additionalFields": { "appendAttribution": false, "parse_mode": "HTML", "message_thread_id": "={{ $json.topic_id }}" } }, "id": "a1f40f24-1887-495b-83db-e8f7e04e7941", "name": "Send edit message", "type": "n8n-nodes-base.telegram", "position": [ 1248, 848 ], "typeVersion": 1.2, "webhookId": "WEBHOOK_ID_PLACEHOLDER_3", "credentials": { "telegramApi": { "id": "TELEGRAM_CREDENTIALS_ID", "name": "YOUR_BOT_NAME" } } }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [ { "id": "ed8c3e33-60e8-4762-a984-61ee1765a421", "leftValue": "={{ $('Telegram Trigger').item.json.edited_message.is_topic_message }}", "rightValue": "", "operator": { "type": "boolean", "operation": "true", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 832, 736 ], "id": "711f0a14-a8a2-4411-889b-4ea2c82268f4", "name": "Message from admin?" }, { "parameters": { "method": "POST", "url": "=https://api.telegram.org/bot{{YOUR_BOT_TOKEN}}/copyMessage", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"chat_id\": \"{{ $('Get user details1').item.json.user_chat_id }}\", \n \"from_chat_id\": \"{{ $('Telegram Trigger').item.json.edited_message.chat.id }}\",\n \"message_id\": {{ $('Telegram Trigger').item.json.edited_message.message_id }}\n}", "options": {} }, "id": "4fed9ea6-af51-42cc-8dce-48143e9d5b2e", "name": "Copy a message4", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1424, 688 ] }, { "parameters": { "operation": "get", "tableId": "YOUR_TABLE_NAME", "filters": { "conditions": [ { "keyName": "topic_id", "keyValue": "={{ $('Telegram Trigger').item.json.edited_message.message_thread_id }}" } ] } }, "type": "n8n-nodes-base.supabase", "typeVersion": 1, "position": [ 1072, 688 ], "id": "41cf9a55-35f1-471b-98d7-63539d9a7526", "name": "Get user details1", "alwaysOutputData": true, "credentials": { "supabaseApi": { "id": "SUPABASE_CREDENTIALS_ID", "name": "Your Supabase Connection" } } }, { "parameters": { "chatId": "={{ $json.user_chat_id }}", "text": "=Сообщение отредактировано:", "additionalFields": { "appendAttribution": false, "parse_mode": "HTML" } }, "id": "c7447b28-4e1b-470d-b3eb-8a0c6a7cd7c0", "name": "Send edit message1", "type": "n8n-nodes-base.telegram", "position": [ 1248, 688 ], "typeVersion": 1.2, "webhookId": "WEBHOOK_ID_PLACEHOLDER_4", "credentials": { "telegramApi": { "id": "TELEGRAM_CREDENTIALS_ID", "name": "YOUR_BOT_NAME" } } } ], "pinData": {}, "connections": { "Telegram Trigger": { "main": [ [ { "node": "Is private chat", "type": "main", "index": 0 } ] ] }, "Process User Message": { "main": [ [ { "node": "Get user", "type": "main", "index": 0 } ] ] }, "Process Admin Reply": { "main": [ [ { "node": "Get user data", "type": "main", "index": 0 } ] ] }, "Is private chat": { "main": [ [ { "node": "Process User Message", "type": "main", "index": 0 } ], [ { "node": "Check if admin reply", "type": "main", "index": 0 } ] ] }, "Create Forum Topic": { "main": [ [ { "node": "Save user data", "type": "main", "index": 0 } ] ] }, "Is NOT a command": { "main": [ [ { "node": "Copy a message1", "type": "main", "index": 0 } ], [ { "node": "Send to Admin Group2", "type": "main", "index": 0 } ] ] }, "First contact?": { "main": [ [ { "node": "Create Forum Topic", "type": "main", "index": 0 } ], [ { "node": "Copy a message", "type": "main", "index": 0 } ] ] }, "Get user": { "main": [ [ { "node": "First contact?", "type": "main", "index": 0 } ] ] }, "Save user data": { "main": [ [ { "node": "Is NOT a command", "type": "main", "index": 0 } ] ] }, "Check if admin reply": { "main": [ [ { "node": "Process Admin Reply", "type": "main", "index": 0 } ], [ { "node": "If message was edited", "type": "main", "index": 0 } ] ] }, "Get user data": { "main": [ [ { "node": "Copy a message2", "type": "main", "index": 0 } ] ] }, "If message was edited": { "main": [ [ { "node": "Message from admin?", "type": "main", "index": 0 } ], [ { "node": "Ignore Message", "type": "main", "index": 0 } ] ] }, "Get user details": { "main": [ [ { "node": "Send edit message", "type": "main", "index": 0 } ] ] }, "Send edit message": { "main": [ [ { "node": "Copy a message3", "type": "main", "index": 0 } ] ] }, "Message from admin?": { "main": [ [ { "node": "Get user details1", "type": "main", "index": 0 } ], [ { "node": "Get user details", "type": "main", "index": 0 } ] ] }, "Get user details1": { "main": [ [ { "node": "Send edit message1", "type": "main", "index": 0 } ] ] }, "Send edit message1": { "main": [ [ { "node": "Copy a message4", "type": "main", "index": 0 } ] ] } }, "active": false, "settings": { "executionOrder": "v1", "saveDataErrorExecution": "all", "saveDataSuccessExecution": "all", "saveManualExecutions": true, "saveExecutionProgress": true }, "versionId": "TEMPLATE_VERSION_ID", "meta": { "templateCredsSetupCompleted": false, "instanceId": "INSTANCE_ID_PLACEHOLDER" }, "id": "WORKFLOW_ID_PLACEHOLDER", "tags": [] }
1
Начать дискуссию