File size: 4,695 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
  "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": {}
}