-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path105-alert-repeat-ping-cron.json
More file actions
220 lines (220 loc) · 7.83 KB
/
Copy path105-alert-repeat-ping-cron.json
File metadata and controls
220 lines (220 loc) · 7.83 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{
"name": "105 — Alert Repeat-Ping Cron (T2 @ 60min unacked → re-broadcast у топік з [⚠ REPEAT] prefix + snooze keyboard)",
"nodes": [
{
"parameters": {
"rule": {
"interval": [
{
"field": "cronExpression",
"expression": "*/15 * * * *"
}
]
}
},
"id": "cron-15min",
"name": "Cron */15min",
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1.2,
"position": [240, 300]
},
{
"parameters": {
"method": "POST",
"url": "={{ $env.PUBLIC_API_BASE_URL || 'https://api.invalid.local' }}/api/internal/alerts/pending",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "=Bearer {{ $env.INTERNAL_API_KEY }}"
},
{
"name": "Content-Type",
"value": "application/json"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={\n \"olderThanMinutes\": 60,\n \"notYetRepeated\": true,\n \"notSnoozed\": true,\n \"limit\": 50\n}",
"options": {
"response": {
"response": {
"neverError": true,
"fullResponse": false
}
}
}
},
"id": "fetch-pending",
"name": "POST /api/internal/alerts/pending",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [460, 300],
"onError": "continueRegularOutput"
},
{
"parameters": {
"jsCode": "// Split the /alerts/pending response so downstream nodes run once per\n// alert. Server already filtered by `olderThanMinutes=60`,\n// `notYetRepeated=true`, `notSnoozed=true`. We keep all severities\n// (P0..P3) here — repeat-ping is a generic accountability mechanic,\n// not severity-gated. P0/P1 already saw T1 escalation 45 min ago via\n// WF-103, but they still need T2 noise in the topic to surface the\n// alert to operators who weren't paged.\nconst body = $input.first()?.json ?? {};\nconst alerts = Array.isArray(body.alerts) ? body.alerts : [];\nreturn alerts.map((a) => ({ json: a }));"
},
"id": "split-alerts",
"name": "Split alerts",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [680, 300]
},
{
"parameters": {
"method": "POST",
"url": "={{ $env.PUBLIC_API_BASE_URL || 'https://api.invalid.local' }}/api/internal/alerts/repeat",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "=Bearer {{ $env.INTERNAL_API_KEY }}"
},
{
"name": "Content-Type",
"value": "application/json"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={\n \"alertId\": \"{{ $json.alert_id }}\"\n}",
"options": {
"response": {
"response": {
"neverError": true,
"fullResponse": false
}
}
}
},
"id": "post-repeat",
"name": "POST /api/internal/alerts/repeat",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [900, 300],
"onError": "continueRegularOutput"
},
{
"parameters": {
"jsCode": "// Build the [⚠ REPEAT] broadcast body. We re-post to the SAME topic as\n// the original alert (not founder DM — that's T1's job). Inline-keyboard\n// drops `investigating` (operator already knows it's pending) and adds\n// snooze 1h/4h so the operator can mute the next repeat-tick if they\n// are actively working on it.\n// `chatId` + `messageThreadId` come from the alert row's `metadata`,\n// which broadcast workflows populate via the standard `Build alert\n// payload` Code node (PR-2/PR-3/PR-4 wiring).\nconst alert = $('Split alerts').item.json;\nconst chatId = alert?.telegram_chat_id\n || alert?.metadata?.tgChatId\n || alert?.metadata?.chatId\n || $env?.SERGEANT_ALERT_OPS_CHAT_ID;\nconst messageThreadId = alert?.metadata?.tgMessageThreadId\n || alert?.metadata?.messageThreadId\n || null;\nconst posted = alert?.posted_at ? new Date(alert.posted_at) : null;\nconst minutesSincePost = posted ? Math.round((Date.now() - posted.getTime()) / 60000) : null;\nconst minutesLabel = minutesSincePost === null ? '?' : `${minutesSincePost}хв`;\nconst summary = (alert?.summary || '').slice(0, 3500);\nconst text = [\n `⚠ <b>REPEAT (${minutesLabel} без ack) — ${alert?.severity || 'P?'}</b>`,\n '',\n `<b>Topic:</b> ${alert?.topic || '—'}`,\n `<b>Alert ID:</b> <code>${alert?.alert_id || '—'}</code>`,\n '',\n summary || '<i>(no summary)</i>',\n].join('\\n');\nconst inlineKeyboard = [\n [\n { text: '✅ Прочитав', callback_data: `ack:r:${alert?.alert_id}` },\n { text: '🕐 Snooze 1h', callback_data: `snooze:1h:${alert?.alert_id}` },\n { text: '🕓 Snooze 4h', callback_data: `snooze:4h:${alert?.alert_id}` },\n ],\n];\nreturn [{ json: {\n chatId,\n messageThreadId,\n text,\n inlineKeyboard,\n alertId: alert?.alert_id,\n} }];"
},
"id": "build-repeat",
"name": "Build [REPEAT] message",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1120, 300]
},
{
"parameters": {
"method": "POST",
"url": "=https://api.telegram.org/bot{{ $env.SERGEANT_ALERT_BOT_TOKEN }}/sendMessage",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Content-Type",
"value": "application/json"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={\n \"chat_id\": {{ Number($json.chatId) }},\n \"text\": {{ JSON.stringify($json.text) }},\n \"parse_mode\": \"HTML\",\n \"reply_markup\": {\n \"inline_keyboard\": {{ JSON.stringify($json.inlineKeyboard) }}\n }{{ $json.messageThreadId ? ',\\n \"message_thread_id\": ' + Number($json.messageThreadId) : '' }}\n}",
"options": {
"response": {
"response": {
"neverError": true,
"fullResponse": false
}
}
}
},
"id": "send-repeat",
"name": "Telegram sendMessage [REPEAT]",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [1340, 300],
"onError": "continueRegularOutput"
}
],
"connections": {
"Cron */15min": {
"main": [
[
{
"node": "POST /api/internal/alerts/pending",
"type": "main",
"index": 0
}
]
]
},
"POST /api/internal/alerts/pending": {
"main": [
[
{
"node": "Split alerts",
"type": "main",
"index": 0
}
]
]
},
"Split alerts": {
"main": [
[
{
"node": "POST /api/internal/alerts/repeat",
"type": "main",
"index": 0
}
]
]
},
"POST /api/internal/alerts/repeat": {
"main": [
[
{
"node": "Build [REPEAT] message",
"type": "main",
"index": 0
}
]
]
},
"Build [REPEAT] message": {
"main": [
[
{
"node": "Telegram sendMessage [REPEAT]",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {
"executionOrder": "v1",
"timezone": "Europe/Kyiv",
"errorWorkflow": "iC82EFJzqBny9kxI"
},
"active": false,
"tags": [
{
"name": "sergeant"
},
{
"name": "alerts"
},
{
"name": "experimental"
}
]
}