Skip to content

Instantly share code, notes, and snippets.

@en9inerd
Last active April 12, 2024 17:51
Show Gist options
  • Save en9inerd/31d94befc2e235f704010b77d3877766 to your computer and use it in GitHub Desktop.
Save en9inerd/31d94befc2e235f704010b77d3877766 to your computer and use it in GitHub Desktop.
Node-RED flows
[{"id":"5a7d710eff9ddb36","type":"tab","label":"Get new posts from blog","disabled":false,"info":"","env":[]},{"id":"5470137f70f4ca7a","type":"feedparse","z":"5a7d710eff9ddb36","name":"","url":"https://enginerd.io/feed.xml","interval":"15","ignorefirst":true,"x":200,"y":140,"wires":[["814eb66a93a3e2e8"]]},{"id":"814eb66a93a3e2e8","type":"html","z":"5a7d710eff9ddb36","name":"","property":"payload","outproperty":"paragraphs","tag":"p","ret":"html","as":"single","x":170,"y":260,"wires":[["9c52eb61bd7cdaa6"]]},{"id":"29eb8d41a3eb35c6","type":"function","z":"5a7d710eff9ddb36","name":"get summary","func":"const summary = [];\nconst cleanedUpPayload = msg.payload.replace(/\\n/g, '').trim();\nconst publishedPosts = (cleanedUpPayload) ? cleanedUpPayload.split(',') : [];\nlet readMore = false;\n\nif (publishedPosts.includes(msg.topic)) return [null, null];\n\nfor (let i = 0; i < msg.paragraphs.length; i++) {\n summary.push(msg.paragraphs[i]);\n if (msg.paragraphs[i].includes('<!--more-->')) {\n summary[i] = summary[i].replace('<!--more-->', '');\n readMore = true;\n break;\n }\n}\n\nconst content = `<b>${msg.article.title}</b>\\n\\n` + summary.join('\\n\\n') + ((readMore) ? `\\n\\n<b><a href=\"${msg.topic}\">READ MORE</a></b>` : '');\nconst tgMsg = {\n payload: {\n type: 'message',\n content,\n chatId: context.get('channelId'),\n options: {\n parse_mode: 'html',\n disable_web_page_preview: true\n }\n }\n};\npublishedPosts.push(msg.topic);\nconst fileContent = {\n payload: publishedPosts.join(',')\n}\n\nreturn [tgMsg, fileContent];","outputs":2,"timeout":0,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\n\ncontext.set('channelId', '@en9inerd')","finalize":"","libs":[],"x":530,"y":140,"wires":[["1dbfe3be20f4d1ef"],["9652ba61b0800595"]]},{"id":"1dbfe3be20f4d1ef","type":"telegram sender","z":"5a7d710eff9ddb36","name":"","bot":"54994ca6895e98aa","haserroroutput":false,"outputs":1,"x":740,"y":100,"wires":[[]]},{"id":"9652ba61b0800595","type":"file","z":"5a7d710eff9ddb36","name":"","filename":"/data/postIds.txt","filenameType":"str","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"utf8","x":750,"y":180,"wires":[[]]},{"id":"2de74bc5a15a0092","type":"file in","z":"5a7d710eff9ddb36","name":"","filename":"/data/postIds.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"utf8","allProps":false,"x":550,"y":260,"wires":[["29eb8d41a3eb35c6"]]},{"id":"9c52eb61bd7cdaa6","type":"delay","z":"5a7d710eff9ddb36","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"0.1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":330,"y":260,"wires":[["2de74bc5a15a0092"]]},{"id":"54994ca6895e98aa","type":"telegram bot","botname":"Helper","usernames":"","chatids":"","baseapiurl":"","updatemode":"none","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]
[{"id":"d74a153c9e7881b3","type":"tab","label":"Visitor Notifier","disabled":false,"info":"","env":[]},{"id":"66321878763425a5","type":"function","z":"d74a153c9e7881b3","name":"validate and transfer","func":"msg.payload = {\n type: 'message',\n content: [\n \"Tag: MyWebSite\",\n `When: ${new Date().toLocaleString(\"en-US\", {\n timeZone: \"America/New_York\",\n month: \"long\",\n day: \"numeric\",\n year: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n hour12: true\n })}`,\n `IP: ${msg.payload?.ip ?? ''}`,\n 'Location: ' + [msg.payload?.location?.city, msg.payload?.location?.region, msg.payload?.location?.country].join(', '),\n `URL: ${msg.payload?.url}`,\n `Screen Dimensions: ${msg.payload?.screenDimensions ?? ''}`,\n `Has Touchscreen: ${msg.payload?.hasTouchScreen}`,\n `Referrer: ${msg.payload?.referrer ?? ''}`,\n `User Agent: ${msg.payload?.userAgent ?? ''}`\n ].join('\\n'),\n chatId: context.get('channelId'),\n options: {\n disable_web_page_preview: true\n }\n};\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\n\ncontext.set('channelId', '-1001635732645')","finalize":"","libs":[],"x":800,"y":380,"wires":[["7d50019c50266681"]]},{"id":"219e8d16ecb74da3","type":"http in","z":"d74a153c9e7881b3","name":"","url":"/:token/visitor-notifier","method":"post","upload":false,"swaggerDoc":"","x":220,"y":100,"wires":[["a64ffb4012c9f7ce"]]},{"id":"7d50019c50266681","type":"telegram sender","z":"d74a153c9e7881b3","name":"","bot":"54994ca6895e98aa","haserroroutput":true,"outputs":2,"x":1020,"y":380,"wires":[["38cb65ff73dc3f59"],["26d8dafdc69b13fb"]]},{"id":"a64ffb4012c9f7ce","type":"function","z":"d74a153c9e7881b3","name":"token validation and ip extraction","func":"if (msg.req.params.token === context.get('token')) {\n const forwardedFor = msg.req.headers['x-forwarded-for'];\n const remoteAddress = msg.req.connection.remoteAddress;\n const clientIp = forwardedFor ? forwardedFor.split(',')[0] : remoteAddress;\n const isLocal = clientIp.startsWith('192.168.') || clientIp.startsWith('10.') || clientIp.startsWith('172.16.');\n\n const cleanedUpMsg = {\n payload: {\n url: msg.payload?.url,\n screenDimensions: msg.payload?.screenDimensions,\n referrer: msg.payload?.referrer,\n userAgent: msg.payload?.userAgent,\n hasTouchScreen: msg.payload?.hasTouchScreen\n }\n }\n\n if (isLocal) {\n return [cleanedUpMsg, null, null, msg];\n } else {\n cleanedUpMsg.payload.ip = clientIp;\n return [null, cleanedUpMsg, null, msg];\n }\n} else {\n msg.payload = {\n error: 'Invalid token.'\n };\n return [null, null, msg, null];\n}\n\n","outputs":4,"timeout":0,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\n\ncontext.set('token', 'cP7cTI1ZLRjZacB2QoWtvJ');","finalize":"","libs":[],"x":220,"y":220,"wires":[["5d5c8d181983ed86"],["9606031402adaaf4"],["684bd533eac3d516"],["acee51bc4c42230f"]]},{"id":"684bd533eac3d516","type":"http response","z":"d74a153c9e7881b3","name":"","statusCode":"401","headers":{},"x":520,"y":280,"wires":[]},{"id":"0aa26a2cd6e3ae9f","type":"http request","z":"d74a153c9e7881b3","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://api64.ipify.org?format=json","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":730,"y":200,"wires":[["baface4049413178"]]},{"id":"9606031402adaaf4","type":"ip-location-lite","z":"d74a153c9e7881b3","name":"","inputField":"payload.ip","outputField":"payload.location","x":790,"y":280,"wires":[["66321878763425a5"]]},{"id":"38cb65ff73dc3f59","type":"debug","z":"d74a153c9e7881b3","name":"telegram node ouput","active":true,"tosidebar":false,"console":true,"tostatus":false,"complete":"payload.options","targetType":"msg","statusVal":"","statusType":"auto","x":1270,"y":360,"wires":[]},{"id":"8251c1a6daadbf1b","type":"http response","z":"d74a153c9e7881b3","name":"","statusCode":"200","headers":{},"x":520,"y":340,"wires":[]},{"id":"26d8dafdc69b13fb","type":"debug","z":"d74a153c9e7881b3","name":"telegram node error","active":true,"tosidebar":false,"console":true,"tostatus":false,"complete":"error","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"y":400,"wires":[]},{"id":"acee51bc4c42230f","type":"change","z":"d74a153c9e7881b3","name":"","rules":[{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":340,"wires":[["8251c1a6daadbf1b"]]},{"id":"5d5c8d181983ed86","type":"change","z":"d74a153c9e7881b3","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"originalPayload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":200,"wires":[["0aa26a2cd6e3ae9f"]]},{"id":"baface4049413178","type":"change","z":"d74a153c9e7881b3","name":"","rules":[{"t":"set","p":"originalPayload.ip","pt":"msg","to":"payload.ip","tot":"msg"},{"t":"move","p":"originalPayload","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":200,"wires":[["9606031402adaaf4"]]},{"id":"54994ca6895e98aa","type":"telegram bot","botname":"Helper","usernames":"","chatids":"","baseapiurl":"","updatemode":"none","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment