patchwise-agent-studio / integrations /n8n /patchwise-telegram.json
EnergyVenom's picture
Patchwise Agent Studio: Oracle AI Agent Studio cycles
af1bee9 verified
Raw History Blame Contribute Delete
4.7 kB
{
"name": "Patchwise to Telegram",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "patchwise-events",
"responseMode": "onReceived",
"options": {
"rawBody": true
}
},
"id": "a1f0c6d2-0001-4a52-9a10-000000000001",
"name": "Patchwise event",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [
240,
300
],
"webhookId": "patchwise-events"
},
{
"parameters": {
"jsCode": "// Patchwise -> Telegram: check the signature, then build the message.\n// The secret and chat id come from n8n's environment (PATCHWISE_SECRET, TELEGRAM_CHAT_ID). If this n8n blocks\n// environment access in nodes (N8N_BLOCK_ENV_ACCESS_IN_NODE=true), paste them into the two constants below instead.\nconst SECRET_FALLBACK = '';\nconst CHAT_ID_FALLBACK = '';\n// Patchwise's address, for the \"Open in Patchwise\" button when an event carries a relative link (FPT_PUBLIC_URL unset).\nconst PATCHWISE_URL_FALLBACK = 'https://energyvenom-patch-testing-coworker.hf.space';\n// Needs the built-in crypto module: set NODE_FUNCTION_ALLOW_BUILTIN=crypto on the n8n server.\nconst crypto = require('crypto');\n\nlet secret = SECRET_FALLBACK;\nlet chatId = CHAT_ID_FALLBACK;\ntry { secret = $env.PATCHWISE_SECRET || secret; } catch (e) { /* env access blocked */ }\ntry { chatId = $env.TELEGRAM_CHAT_ID || chatId; } catch (e) { /* env access blocked */ }\nlet base = PATCHWISE_URL_FALLBACK;\ntry { base = $env.PATCHWISE_URL || base; } catch (e) { /* env access blocked */ }\nconst absolute = (l) => (/^https?:\\/\\//.test(String(l || '')) ? l : base.replace(/\\/$/, '') + (String(l || '/').startsWith('/') ? l : '/' + l));\nif (!secret || !chatId) throw new Error('Set PATCHWISE_SECRET and TELEGRAM_CHAT_ID (or the fallbacks in this node).');\n\nconst esc = (s) => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\nconst out = [];\nfor (const item of $input.all()) {\n const headers = item.json.headers || {};\n const given = String(headers['x-patchwise-signature'] || '');\n const raw = item.binary && item.binary.data ? Buffer.from(item.binary.data.data, 'base64') : Buffer.from(JSON.stringify(item.json.body || {}));\n const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(raw).digest('hex');\n const ok = given.length === expected.length && crypto.timingSafeEqual(Buffer.from(given), Buffer.from(expected));\n if (!ok) continue; // not from Patchwise: dropped silently\n const e = JSON.parse(raw.toString('utf8'));\n out.push({ json: {\n chatId,\n link: absolute(e.link),\n text: '<b>' + esc(e.coworker || 'Patchwise') + '</b>: ' + esc(e.title) + (e.body ? String.fromCharCode(10) + esc(e.body) : ''),\n } });\n}\nreturn out;"
},
"id": "a1f0c6d2-0002-4a52-9a10-000000000002",
"name": "Check signature and write message",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
480,
300
]
},
{
"parameters": {
"chatId": "={{ $json.chatId }}",
"text": "={{ $json.text }}",
"replyMarkup": "inlineKeyboard",
"inlineKeyboard": {
"rows": [
{
"row": {
"buttons": [
{
"text": "Open in Patchwise",
"additionalFields": {
"url": "={{ $json.link }}"
}
}
]
}
}
]
},
"additionalFields": {
"parse_mode": "HTML",
"appendAttribution": false
}
},
"id": "a1f0c6d2-0003-4a52-9a10-000000000003",
"name": "Send to Telegram",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
720,
300
],
"credentials": {
"telegramApi": {
"id": "",
"name": "Telegram account"
}
}
}
],
"connections": {
"Patchwise event": {
"main": [
[
{
"node": "Check signature and write message",
"type": "main",
"index": 0
}
]
]
},
"Check signature and write message": {
"main": [
[
{
"node": "Send to Telegram",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1"
},
"pinData": {}
}