Skip to content

Instantly share code, notes, and snippets.

@supermamon
Created December 1, 2021 10:48
Show Gist options
  • Save supermamon/10e6e79443b6a4a4be3f8fc03eeb2d0a to your computer and use it in GitHub Desktop.
Save supermamon/10e6e79443b6a4a4be3f8fc03eeb2d0a to your computer and use it in GitHub Desktop.
Node-RED workflow to mutate JSON payload to Pushcut widget data format.
[
{
"id": "2fb1e3e3.67ea9c",
"type": "tab",
"label": "/pushcut widget mutate",
"disabled": false,
"info": ""
},
{
"id": "14f45599.1f7022",
"type": "http in",
"z": "2fb1e3e3.67ea9c",
"name": "POST /pushcut",
"url": "/pushcut/:api_key/widgets/:widget_name",
"method": "post",
"upload": false,
"swaggerDoc": "",
"x": 120,
"y": 80,
"wires": [
[
"c074ddc6.9e5f1",
"1d22392e.a1be57",
"a421b460.7b6ce"
]
],
"info": "POST /pushcut\n\nThis is the endpoint to receive data. Data can either be plain text or JSON.\n\n* URL format\n\nThe URL should be the same as the pushcut widget URL. Replace `api.pushcut.io/` with `domain-name.tld/pushcut`\n\nExample\nOriginal - \nhttps://api.pushcut.io/Xrsjgal82nl384/widgets/WidgetOne?content=ContentOne\n\nUpdated - \nhttps://homelab.com/pushcut/Xrsjgal82nl384/widgets/WidgetOne?content=ContentOne\n\n- Xrsjgal82nl384 = api_key\n- WidgetOne = widget_name\n- ContentOne = content\n"
},
{
"id": "4ad1cfa1.98bb88",
"type": "http response",
"z": "2fb1e3e3.67ea9c",
"name": "send receipt",
"statusCode": "200",
"headers": {},
"x": 570,
"y": 80,
"wires": []
},
{
"id": "a421b460.7b6ce",
"type": "debug",
"z": "2fb1e3e3.67ea9c",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 370,
"y": 260,
"wires": []
},
{
"id": "c074ddc6.9e5f1",
"type": "function",
"z": "2fb1e3e3.67ea9c",
"name": "mutate",
"func": "// function to move child keys to top level\nJSON.flatten = function(data) {\n var result = {};\n function recurse (cur, prop) {\n var sep = \".\"\n var spc = \" \"\n \n prop = prop.replace(' ',spc)\n \n if (Object(cur) !== cur) {\n result[prop] = `${cur}`;\n } else if (Array.isArray(cur)) {\n for(var i=0, l=cur.length; i<l; i++)\n recurse(cur[i], prop ? prop+sep+i : \"\"+i);\n if (l == 0)\n result[prop] = [];\n } else {\n var isEmpty = true;\n for (var p in cur) {\n isEmpty = false;\n recurse(cur[p], prop ? prop+sep+p : p);\n }\n if (isEmpty)\n result[prop] = {};\n }\n }\n recurse(data, \"\");\n return result;\n}\n// ---------------------------------\n\nvar flatten = msg.req.query.flatten\nvar payload = msg.payload\n\n// attempt to parse payload if not JSON\n// if not parsable, return { raw: ... }\nif (typeof payload != 'object') {\n try {\n payload = JSON.parse(payload)\n } catch (e) {\n payload = {raw: payload}\n }\n}\n\n// move all childen to top level\nif (flatten==\"1\") {\n payload = JSON.flatten(payload)\n}\n\n// params for the url\nmsg.params = {\n api_key: msg.req.params.api_key,\n widget_name: msg.req.params.widget_name,\n content: msg.req.query.content\n}\nmsg.headers = {\n \"Content-Type\": \"application/json\"\n}\n\n// pushcut style payload\nmsg.payload = {\n content: msg.req.query.content,\n inputs: payload\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 360,
"y": 180,
"wires": [
[
"1e09dd37.03d883"
]
]
},
{
"id": "1d22392e.a1be57",
"type": "function",
"z": "2fb1e3e3.67ea9c",
"name": "response",
"func": "msg.payload = {\n message: \"Received\"\n \n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 360,
"y": 80,
"wires": [
[
"4ad1cfa1.98bb88"
]
]
},
{
"id": "e57ed419.fb0db",
"type": "debug",
"z": "2fb1e3e3.67ea9c",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1070,
"y": 180,
"wires": []
},
{
"id": "1e09dd37.03d883",
"type": "change",
"z": "2fb1e3e3.67ea9c",
"name": "update url slugs",
"rules": [
{
"t": "set",
"p": "api_key",
"pt": "msg",
"to": "msg.params.api_key",
"tot": "jsonata"
},
{
"t": "set",
"p": "widget_name",
"pt": "msg",
"to": "msg.params.widget_name",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 580,
"y": 180,
"wires": [
[
"dd1cbe86.2b16b8",
"165b1398.745f0c"
]
]
},
{
"id": "dd1cbe86.2b16b8",
"type": "debug",
"z": "2fb1e3e3.67ea9c",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 810,
"y": 260,
"wires": []
},
{
"id": "165b1398.745f0c",
"type": "http request",
"z": "2fb1e3e3.67ea9c",
"name": "POST api.pushcut.io",
"method": "POST",
"ret": "txt",
"paytoqs": "ignore",
"url": "https://api.pushcut.io/{{api_key}}/widgets/{{widget_name}}",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 840,
"y": 180,
"wires": [
[
"e57ed419.fb0db"
]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment