How-To: HomeBot - Chat to your home #2671
smashah
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
[Placeholder]
Demo
https://twitter.com/openwadev/status/1488081746012909569
https://twitter.com/i/status/1487451302577807361
First Install the open-wa node-red contrib:
Prerequisites
How to get your almond converse URL from home assistant set up:
It should look something like this:
connect.sid=s%3A2HexASDASDSADkl8Pt1Sl0-tQ72b0Cs3--zZhUyz1.prR%2BFwSxwX5j%ASDASDASDASD
Now you can make requests to the almond api:
##Main Flow
main_flow.json
```javascript [{"id":"66b6a3b8317d53bd","type":"tab","label":"Main","disabled":false,"info":"","env":[]},{"id":"fb908e01fae6fd12","type":"debug","z":"66b6a3b8317d53bd","name":"Log result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1000,"y":100,"wires":[]},{"id":"ce94b552ef33dfab","type":"link call","z":"66b6a3b8317d53bd","name":"","links":["4ecb20950c0b5628"],"timeout":"30","x":660,"y":80,"wires":[["f342a28818787578"]]},{"id":"faa33fb9abacc7b3","type":"ha-get-entities","z":"66b6a3b8317d53bd","name":"Get Users","server":"d4d20f6d.9d368","version":0,"rules":[{"property":"entity_id","logic":"is","value":"number.almond\\_valid.*","valueType":"re"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":320,"y":260,"wires":[["fd7683ccf9ec718e"]]},{"id":"fd7683ccf9ec718e","type":"function","z":"66b6a3b8317d53bd","name":"","func":"msg.validNumbers = msg.payload\nif(msg.incomingMessage && msg.incomingMessage.from) {\nconst valid = msg.validNumbers.find(x=>msg.incomingMessage.from.includes(x.state))\nif(valid){\n msg.payload = msg.incomingMessage\nreturn msg; \n}\n}\nmsg.payload = false;\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":260,"wires":[["c15c3a35b8bb5a33"]]},{"id":"c36da981d724ddf9","type":"function","z":"66b6a3b8317d53bd","name":"","func":"msg.incomingMessage = msg.payload;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":180,"y":260,"wires":[["faa33fb9abacc7b3"]]},{"id":"c7804a23d221e766","type":"link in","z":"66b6a3b8317d53bd","name":"auth-incoming-msg","links":[],"x":85,"y":260,"wires":[["c36da981d724ddf9"]]},{"id":"c15c3a35b8bb5a33","type":"link out","z":"66b6a3b8317d53bd","name":"","mode":"return","links":[],"x":555,"y":260,"wires":[]},{"id":"5414072db8f6868f","type":"comment","z":"66b6a3b8317d53bd","name":"Auth","info":"","x":110,"y":200,"wires":[]},{"id":"947db309dcf426d5","type":"link call","z":"66b6a3b8317d53bd","name":"","links":["c7804a23d221e766"],"timeout":"30","x":330,"y":100,"wires":[["69edf9e8ac6e1fdc"]]},{"id":"69edf9e8ac6e1fdc","type":"switch","z":"66b6a3b8317d53bd","name":"If allowed","property":"payload","propertyType":"msg","rules":[{"t":"else"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":500,"y":100,"wires":[["ce94b552ef33dfab"],["b5e5e20aac03a0c4"]]},{"id":"98d633a98ab558f9","type":"listen","z":"66b6a3b8317d53bd","name":"","server":"98afb117d5e2d99f","listener":"onMessage","x":130,"y":100,"wires":[["5793fd57ae83dfe3","947db309dcf426d5"]]},{"id":"5793fd57ae83dfe3","type":"debug","z":"66b6a3b8317d53bd","name":"Log incoming msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":330,"y":140,"wires":[]},{"id":"0051671cae23a30c","type":"function","z":"66b6a3b8317d53bd","name":"ALMOND 2 WA CONVERT","func":"msg.genieResult = msg.payload;\nlet m = msg.payload.messages || []\nconst validTypes = [\n \"text\",\n \"button\",\n \"choice\",\n \"picture\",\n \"rdl\",\n \"text\",\n ]\nconst btnTypes = [\n \"button\",\n \"choice\"\n]\nlet bid = 0;\n\nconst outgoingMessages = [];\n\nconst addOut = (method, args) => outgoingMessages.push({\n method,\n args: {\n to: msg.incomingMessage.from,\n ...args\n }\n })\n\nif(msg.payload) {\n const msgs = m.filter(({type})=>validTypes.includes(type));\n //check if there are choices/buttons\n const buttons = msgs.filter(({type})=>btnTypes.includes(type))\n //check if there are pictures\n const pics = msgs.filter(({type})=>type===\"picture\")\n //check if there are links\n const links = msgs.filter(({type})=>type===\"rdl\")\n const texts = msgs.filter(({type})=>type===\"text\")\n \n //order should be text, buttons, picture, rdl\n if(texts.length && buttons.length > 1){\n if(buttons.length > 3) {\n //merge the last text with the buttons message\n addOut(\"sendListMessage\", {\n \"title\": \"Select\",\n \"description\":texts.slice(-1)[0].text || \"hello\",\n \"sections\": [\n {\n title: \"\",\n rows: buttons.map(b=>({\n id: bid++,\n title: b.title\n }))\n }\n ],\n \"actionText\": \"Choose\"\n })\n } else {\n addOut(\"sendButtons\", {\n body: texts.slice(-1)[0].text,\n title: \"Respond to continue\",\n buttons: buttons.map(b=>({\n id: bid++,\n text: b.title\n }))\n })\n }\n texts.pop()\n }\n if(texts.length && pics.length == 1) {\n addOut(\"sendImage\", {\n caption: texts.slice(-1)[0].text,\n file: pics[0].url,\n filename: \"image.jpeg\"\n })\n texts.pop()\n }\n \n if(texts.length && links.length == 1) {\n //if the link has a picture URL then just send it as an image with a caption\n if(links[0].rdl.pictureUrl) {\n addOut(\"sendImage\", {\n caption: `${texts.slice(-1)[0].text}\n${links[0].rdl.displayTitle || ''}`,\n file: links[0].rdl.pictureUrl,\n filename: \"image.jpeg\"\n })\n } else {\n addOut(\"sendLinkWithAutoPreview\", {\n text: texts.slice(-1)[0].text + ' ' + links[0].rdl.displayTitle || '',\n url: links[0].rdl.webCallback,\n })\n }\n \n texts.pop()\n }\n \n \n texts.map(({text})=>addOut(\"sendText\", {\n content: text\n }))\n \n \n \n\n} else {\n addOut(\"sendText\", {\n content: \"401\"\n })\n}\nmsg.payload = [\n ...outgoingMessages.filter(({method})=> method === \"sendText\"),\n ...outgoingMessages.filter(({method})=> method !== \"sendText\")\n ]\nnode.warn(msg)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":420,"wires":[["560ced54eea75a61"]]},{"id":"560ced54eea75a61","type":"split","z":"66b6a3b8317d53bd","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":430,"y":420,"wires":[["6d728e2ad6e3e7e0"]]},{"id":"6d728e2ad6e3e7e0","type":"function","z":"66b6a3b8317d53bd","name":"","func":"return msg.payload;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":420,"wires":[["4a5c2ed5bf8aa3c7"]]},{"id":"4a5c2ed5bf8aa3c7","type":"cmd","z":"66b6a3b8317d53bd","name":"Send WA Command","server":"98afb117d5e2d99f","method":"addLabel","args":"{\"label\":\"string\",\"chatId\":\"[email protected] or [email protected]\"}","x":740,"y":420,"wires":[["0ca4a2068c166198"]]},{"id":"51c7f805f6850f4c","type":"comment","z":"66b6a3b8317d53bd","name":"Send Almond message to WA","info":"","x":180,"y":360,"wires":[]},{"id":"5f8e9059cc45f2c9","type":"link in","z":"66b6a3b8317d53bd","name":"almond-2-wa","links":[],"x":86,"y":421,"wires":[["0051671cae23a30c"]]},{"id":"0ca4a2068c166198","type":"link out","z":"66b6a3b8317d53bd","name":"","mode":"return","links":[],"x":875,"y":420,"wires":[]},{"id":"f342a28818787578","type":"link call","z":"66b6a3b8317d53bd","name":"","links":["5f8e9059cc45f2c9"],"timeout":"30","x":840,"y":100,"wires":[["fb908e01fae6fd12"]]},{"id":"b5e5e20aac03a0c4","type":"function","z":"66b6a3b8317d53bd","name":"___spacer___","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":120,"wires":[["f342a28818787578"]]},{"id":"8695bd49a81fc13b","type":"comment","z":"66b6a3b8317d53bd","name":"Main flow","info":"","x":120,"y":60,"wires":[]},{"id":"d4d20f6d.9d368","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":true,"heartbeatInterval":"30","credentials":{}},{"id":"98afb117d5e2d99f","type":"owa-server","name":"HOMEBOT","url":"https://1d62-82-41-76-185.ngrok.io","key":""}]Beta Was this translation helpful? Give feedback.
All reactions