{ "name": "Send a Telegram Message from a Webhook", "nodes": [ { "name": "Receive a message by GET", "type": "n8n-nodes-base.webhook", "position": [ 100, 240 ], "parameters": { "authentication": "none", "httpMethod": "GET", "path": "send-telegram-message-from-webhook", "responseMode": "lastNode", "responseData": "firstEntryJson", "options": { "responsePropertyName": "response", "responseContentType": "text/plain; charset=utf-8" } }, "typeVersion": 1, "id": "send-telegram-message-from-webhook-webhook" }, { "id": "send-telegram-message-from-webhook-query", "name": "Read the message and private chat", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 340, 240 ], "parameters": { "mode": "runOnceForEachItem", "jsCode": "function telegramWebhookQuery({ query, chatId }) {\n const text = query?.parameter;\n if (typeof text !== 'string' || !text.trim() || text.length > 4096) throw new Error('Supply one nonempty parameter query value of at most 4096 characters');\n const recipient = typeof chatId === 'number' ? String(chatId) : chatId;\n if (typeof recipient !== 'string' || !/^[1-9][0-9]*$/.test(recipient) || !Number.isSafeInteger(Number(recipient))) throw new Error('Configure the original recipient’s positive numeric private chat ID');\n return { text, chatId: recipient };\n}\n\nreturn {json:telegramWebhookQuery({query:$json.query,chatId:$(\"Send the Telegram message\").params.chatId})};" } }, { "id": "send-telegram-message-from-webhook-send", "name": "Send the Telegram message", "type": "n8n-nodes-base.telegram", "typeVersion": 1, "position": [ 580, 240 ], "parameters": { "resource": "message", "operation": "sendMessage", "chatId": "YOUR_TELEGRAM_CHAT_ID", "text": "={{ $json.text }}", "replyMarkup": "none", "additionalFields": { "parse_mode": "Markdown" } } }, { "id": "send-telegram-message-from-webhook-confirm", "name": "Confirm the private message", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 820, 240 ], "parameters": { "mode": "runOnceForEachItem", "jsCode": "function telegramWebhookConfirmation({ message, text, chatId }) {\n if (!message || !Number.isSafeInteger(message.messageId) || message.messageId <= 0 || !Number.isSafeInteger(message.date) || message.date < 0 || message.chat?.type !== 'private' || String(message.chat.id) !== String(chatId) || typeof message.chat.firstName !== 'string' || !message.chat.firstName.trim()) throw new Error('Telegram did not confirm the message in the configured private chat with its recipient’s first name; sending may already have occurred');\n if (typeof text !== 'string' || !text) throw new Error('The original message text is missing');\n return { message, response: 'Sent message to ' + message.chat.firstName + ': \"' + text + '\"' };\n}\n\nif($json.ok!==true)throw new Error(\"Telegram did not confirm the send; a message may already exist\");const input=$(\"Read the message and private chat\").first().json;const result=telegramWebhookConfirmation({message:{messageId:$json.result?.message_id,date:$json.result?.date,chat:{id:$json.result?.chat?.id,type:$json.result?.chat?.type,firstName:$json.result?.chat?.first_name}},text:input.text,chatId:input.chatId});return {json:{...$json,response:result.response}};" } } ], "active": false, "settings": { "executionOrder": "v1" }, "connections": { "Receive a message by GET": { "main": [ [ { "node": "Read the message and private chat", "type": "main", "index": 0 } ] ] }, "Read the message and private chat": { "main": [ [ { "node": "Send the Telegram message", "type": "main", "index": 0 } ] ] }, "Send the Telegram message": { "main": [ [ { "node": "Confirm the private message", "type": "main", "index": 0 } ] ] } }, "pinData": {} }