{ "name": "Outlevo: Usage alert to Microsoft Teams", "nodes": [ { "id": "77ac655a-3214-4cd1-82d6-dd5dc02091eb", "name": "How to set it up", "type": "n8n-nodes-base.stickyNote", "typeVersion": 1, "position": [ -40, -540 ], "parameters": { "content": "## How to set it up\n\n**Credentials** (n8n asks for each on first open):\n- **Outlevo webhook signing secret**: a Crypto credential; paste the `secret` Outlevo returns when you register the webhook (below) as its secret.\n- **Microsoft Teams**: connect your Microsoft Teams account.\n\n**Pick in the node**:\n- **Post to Teams**: the team and the channel\n\n**Register the webhook** (needs a key with `webhooks:manage`): copy the Production URL of the **Outlevo webhook** node, then:\n```\ncurl -X POST https://api.outlevo.com/v1/webhooks \\\n -H \"Authorization: Bearer $OUTLEVO_KEY\" \\\n -H \"Idempotency-Key: $(uuidgen)\" \\\n -H \"Content-Type: application/json\" \\\n -d '{\"url\": \"\", \"events\": [\"usage.threshold\"]}'\n```\nPut the `secret` from the answer into the Crypto credential.\n\nThen switch the workflow to **Active**.", "height": 480, "width": 620 } }, { "id": "bf1c3c42-9f6f-4e72-950d-1df40dd5625a", "name": "Outlevo webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 2, "position": [ 0, 0 ], "parameters": { "httpMethod": "POST", "path": "outlevo-usage-alert-to-teams", "responseMode": "onReceived", "options": { "rawBody": true } }, "webhookId": "5b2df520-b72e-4981-9a3c-fa88e01349e0" }, { "id": "1db857c2-dbcb-4b78-8cf9-4a7b71aa6a84", "name": "Read the body", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 240, 0 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "// Outlevo signs every delivery: Outlevo-Signature: t=,v1=.\n// This node reads the raw body and the header; the Crypto node signs the same text with your secret; the next node compares.\nconst item = $input.first();\nconst raw = (await this.helpers.getBinaryDataBuffer(0, 'data')).toString('utf8');\nconst header = String((item.json.headers || {})['outlevo-signature'] || '');\nconst parts = header.split(',').map((kv) => kv.trim().split('='));\nconst t = Number((parts.find(([k]) => k === 't') || [])[1]) || 0;\nconst sigs = parts.filter(([k]) => k === 'v1').map(([, v]) => v || '');\nreturn [{ json: { raw, t, sigs, signed: `${t}.${raw}` } }];\n" } }, { "id": "acbd1357-a6fa-402b-8061-7c8478917931", "name": "Sign with your secret", "type": "n8n-nodes-base.crypto", "typeVersion": 2, "position": [ 480, 0 ], "parameters": { "action": "hmac", "type": "SHA256", "value": "={{ $json.signed }}", "dataPropertyName": "hmac", "encoding": "hex" }, "credentials": { "crypto": { "id": "", "name": "Outlevo webhook signing secret" } } }, { "id": "f57556a3-7790-4f8c-afc8-9bf1d5af5503", "name": "Check signature", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 720, 0 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "// Accept the event only if a v1 signature equals the HMAC and it was signed in the last 5 minutes; anything else stops here.\nconst { raw, t, sigs, hmac } = $input.first().json;\nconst same = (a, b) => { if (typeof a !== 'string' || a.length !== b.length) return false; let d = 0; for (let i = 0; i < a.length; i++) d |= a.charCodeAt(i) ^ b.charCodeAt(i); return d === 0; };\nif (!t || Math.abs(Date.now() / 1000 - t) > 300 || !sigs.some((s) => same(s, hmac))) throw new Error('Outlevo signature did not verify');\nconst event = JSON.parse(raw);\nreturn [{ json: { ...event.data, event_id: event.id, event: event.type, created_at: event.created_at } }];\n" } }, { "id": "dc4fc674-9dc3-4db7-a07a-17c0d5252871", "name": "Usage alert?", "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [ 960, 0 ], "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "7f127f53-bb1e-4f88-8cfa-f6b62290f1fb", "leftValue": "={{ $json.event }}", "rightValue": "usage.threshold", "operator": { "type": "string", "operation": "equals" } } ], "combinator": "and" }, "options": {} } }, { "id": "ec5553ef-c423-416f-ba60-fcd3b100c6fb", "name": "Write the message", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1200, 0 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "const ev = $('Usage alert?').first().json;\nconst scope = String(ev.scope || 'org');\nconst what = scope === 'org' ? 'your monthly credits' : scope === 'org:forecast' ? 'your monthly credits (forecast)' : scope === 'org:daily_budget' ? \"today's credit budget\" : scope === 'org:monthly_budget' ? \"this month's credit budget\" : scope.startsWith('key:') ? `the limit of API key ${scope.slice(4)}` : scope;\nconst period = String(ev.period || '').replace(/^[a-z_]+:/, '');\nconst text = `⚠️ Outlevo: ${ev.percent ?? '?'}% of ${what} used${period ? ` (${period})` : ''}\\nhttps://app.outlevo.com/billing`;\nreturn [{ json: { text, subject: `Outlevo usage at ${ev.percent ?? '?'}%` } }];\n" } }, { "id": "6ea73966-2b71-4bad-9ac4-b1a853d50986", "name": "Post to Teams", "type": "n8n-nodes-base.microsoftTeams", "typeVersion": 2, "position": [ 1440, 0 ], "parameters": { "resource": "channelMessage", "operation": "create", "teamId": { "__rl": true, "mode": "list", "value": "" }, "channelId": { "__rl": true, "mode": "list", "value": "" }, "contentType": "html", "message": "={{ $json.text.split('&').join('&').split('<').join('<').split('>').join('>').split('\\n').join('
') }}", "options": { "includeLinkToWorkflow": false } }, "credentials": { "microsoftTeamsOAuth2Api": { "id": "", "name": "Microsoft Teams" } } } ], "connections": { "Outlevo webhook": { "main": [ [ { "node": "Read the body", "type": "main", "index": 0 } ] ] }, "Read the body": { "main": [ [ { "node": "Sign with your secret", "type": "main", "index": 0 } ] ] }, "Sign with your secret": { "main": [ [ { "node": "Check signature", "type": "main", "index": 0 } ] ] }, "Check signature": { "main": [ [ { "node": "Usage alert?", "type": "main", "index": 0 } ] ] }, "Usage alert?": { "main": [ [ { "node": "Write the message", "type": "main", "index": 0 } ], [] ] }, "Write the message": { "main": [ [ { "node": "Post to Teams", "type": "main", "index": 0 } ] ] } }, "settings": { "executionOrder": "v1" }, "pinData": {}, "meta": { "templateCredsSetupCompleted": false } }