raweee / node-red-contrib-whatsapp-link

Simple Node-Red node to link Whatsapp
https://discord.gg/VApVeGuF
26 stars 7 forks source link

Gets disconnected #18

Closed alex-shape closed 9 months ago

alex-shape commented 1 year ago

Hi guys,

I have been testing your nodes and it works fine, it sometimes drops a bunch of errors like this:

msg : error
"Error: Timed Out"

But it works, i can provide fields from other node and it sends the message without issues.

The fact is that it eventually it gets disconnected, I can track by the online status of that number and it shows 'online' when it works, and 'last seen xxxxx', I see the nodes and have the red dot and 'disconnected', I need to restart them (many times) so it can get connected again, after that it keeps connection by about two or three days.

It asked me to install some puppeter but IDK if I installed the right node, Im more an enthusiast than a developer.

Idk if Im doing something wrong but the !noderedcommand doesn't get a response

Thanks again and I hope this helps to improve the node.

BR

raweee commented 1 year ago

Hi @alex-shape, thanks for your detailed observations,

Node is packed with all dependencies including Puppeteer, so no other dependencies required.

I'm working on a way to avoid sleeping of whatsapp Hope that auto-ping type method to keep whatsapp alive would be completed till next update.

ryangonzalez133 commented 1 year ago

hello again friend, one thing that is missing in the lite version is the automatic reconnection, because after a while, the node drops and you have to restart it to connect again. I think the web version is more secure, but unfortunately it doesn't have some options like the lite, like the messages per button. Thanks for the dedication

raweee commented 1 year ago

All done, Pings at inverter added in Whatsapp-Link Node. See if you find any other bugs.

ryangonzalez133 commented 1 year ago

After the update, this error keeps spawning and then node-red crashes " 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [error] [whatsappLink:Web] Error in whatsapp Ping. 18 Feb 13:25:54 - [red] Uncaught Exception: 18 Feb 13:25:54 - [error] Error: Connection Closed at sendRawMessage (/data/node_modules/@adiwajshing/baileys/lib/Socket/socket.js:50:19) at sendNode (/data/node_modules/@adiwajshing/baileys/lib/Socket/socket.js:61:16) at Object.sendPresenceUpdate (/data/node_modules/@adiwajshing/baileys/lib/Socket/chats.js:392:19) at Timeout._onTimeout (/data/node_modules/node-red-contrib-whatsapp-link/whatsappLink.js:221:34) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7) "

Correction, I did not change the ping time, in case it occurs with someone. Just change the configuration in the "admin" node

ryangonzalez133 commented 1 year ago

Hello, the reconnection has improved, but from time to time the connection continues to drop without coming back that I have noticed, in my terminal it returned this error "[whatsappLink:Web] Error : Error: Stream Errored (unknown)"

other errors that also appear are " [info] [whatsappLink:Web] Error : Error: Stream Errored (conflict). [info] [whatsappLink:Web] Error : undefined"

ryangonzalez133 commented 1 year ago

Despite having improved disconnection, it keeps disconnecting and only reconnects if you restart the node. The status is "Disconnect" when it drops

alex-shape commented 1 year ago

Hi guys,

The ping improves the stability but eventually falls and drops the message:

"Error in whatsapp Ping."

And all nodes shows disconnected flag

The admin does not respond to test command, just drops a bunch of "Error: Connection Closed" at debug's screen.

I think it could be helpful besides the ping strategy, a way to detect and restart the nodes, currently Im restarting all the flows, I think a workaround could be get a 'disconnected' response (to the 'test' command, ie), and a way how to 'restart' the nodes individually, so it could be solved by a flow automatically, no need to warantee no disconnection, just a way how the connection could be recovered in case it drops.

Long story short, a way how it can be detecteed there are no connection and a way how to restart nodes after the connection is restablished, without the need of someones intervention.

Thanks again for your work.

ryangonzalez133 commented 1 year ago

This problem is really common. I made a node to automatically restart the flows and thus reconnect, so every 40 minutes the flows are restarted. Only solution I found. I even tried putting a code inside to restart every time, but eventually the connection drops again

Nicolas-Rocha commented 1 year ago

Good evening @ryangonzalez133

Could you share how you do it to restart every 40 minutes?

ryangonzalez133 commented 1 year ago

Ok, the solution I had, restart all the flows, I thought it was good because some nodes that for some reason, disconnected and did not reconnect, but it does not happen as often as the Whatsapp link. With this restart, all flows restart and those that were disconnected reconnect and those that were already working continue working.

I will provide two types of nodes, one if you have username and password protection on your node-red and another if you have no protection when entering the node-red. That way, you just need to modify some information for your node red, such as ip, or username and password. Just below are the nodes if you have node-red protection.

The only node that must be modified is "request" and "request Token"

[ { "id": "ae956638990ea00e", "type": "function", "z": "6937d5b12ec486c4", "name": "Request Token", "func": "msg.payload = {\n \"client_id\": \"node-red-editor\",\n \"grant_type\": \"password\",\n \"scope\": \"*\",\n \"username\": \"your-username\",\n \"password\": \"your-password\"\n}\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 360, "y": 840, "wires": [ [ "c2d34519fea0a84f" ] ], "info": "(do not modify the grant_type if client_id)" }, { "id": "acff549fe89a6a3e", "type": "inject", "z": "6937d5b12ec486c4", "name": "Manual reload", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "2400", "crontab": "", "once": false, "onceDelay": "", "topic": "", "payload": "", "payloadType": "str", "x": 160, "y": 840, "wires": [ [ "ae956638990ea00e" ] ] }, { "id": "c2d34519fea0a84f", "type": "http request", "z": "6937d5b12ec486c4", "name": "Token", "method": "POST", "ret": "txt", "paytoqs": false, "url": "http://localhost:1880/auth/token", "tls": "", "persist": false, "proxy": "", "authType": "", "x": 550, "y": 840, "wires": [ [ "1cd73182732803f1" ] ] }, { "id": "1cd73182732803f1", "type": "function", "z": "6937d5b12ec486c4", "name": "Confirm token", "func": "// get the status of the request\nvar status = msg.statusCode;\n//var token = {};\nvar token = '';\nmsg.headers ={};\n\n//let node = feedback;\n\nswitch(status){\n case 200:\n node.log(\"Secure restart\");\n token = JSON.parse(msg.payload);\n token = 'Bearer '+token.access_token;\n msg.headers = {\n \"Authorization\": token,\n \"Node-RED-Deployment-Type\":\"reload\"\n }\nmsg.payload =\"\";\n break;\n case 204:\n node.log(\"Secure without restart\");\n global.set('result','\tSuccess - with no further content');\n break;\n case 400:\n node.warn(\"Bad request\");\n break;\n case 401:\n node.warn(\"Not authorized\");\n break;\n case 403:\n node.warn(\"Forbidden\");\n break;\n case 404:\n node.log(\"Unsecure restart\");\n msg.headers = {\n \"Node-RED-Deployment-Type\":\"reload\"\n }\n break;\n case 409:\n node.warn(\"Version mismatch\");\n break;\n case 500:\n node.error(\"Server Error\");\n break;\n default:\n node.warn(\"Unknown Error\");\n break;\n}\n\nmsg.payload = \"\";\nreturn msg;", "outputs": 1, "noerr": 0, "x": 720, "y": 840, "wires": [ [ "4fdff0f31eb56a92" ] ], "info": "Restart of Node-Red flows.\nWill check if the action needs security or not.\nNote: if the first 5 attemps return a statuscode 403 'forbidden'\nthen the server will break and only way to recover is then to\nrestart the service" }, { "id": "4fdff0f31eb56a92", "type": "http request", "z": "6937d5b12ec486c4", "name": "Restart", "method": "POST", "ret": "txt", "paytoqs": false, "url": "http://localhost:1880/flows", "tls": "", "persist": false, "proxy": "", "authType": "", "x": 940, "y": 840, "wires": [ [ "928b01812f042d1d" ] ] }, { "id": "928b01812f042d1d", "type": "debug", "z": "6937d5b12ec486c4", "name": "debug 51", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 1100, "y": 840, "wires": [] } ]

and if you don't have node-red login protection, use the one below.

[ { "id": "f660caaff70335f0", "type": "function", "z": "6937d5b12ec486c4", "name": "request", "func": "msg.url = 'http://your node-red ip and port:1880/#flows';\nmsg.method = 'POST';\nmsg.headers = {};\nmsg.headers['content-type'] = \"application/json; charset=utf-8\";\nmsg.headers['Node-RED-Deployment-Type'] = \"reload\";\nmsg.headers['Node-RED-API-Version'] = \"v2\";\nmsg.payload = {\n \"flows\": [\n {\n \"type\": \"tab\",\n \"id\": \"the id of the flow you want to restart\",\n \"label\": \"the name of the flow you got the id\"\n }\n ]\n};\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 260, "y": 900, "wires": [ [ "c99657e1f04c6bc4" ] ] }, { "id": "f1499407499a6e30", "type": "debug", "z": "6937d5b12ec486c4", "name": "results", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 610, "y": 900, "wires": [] }, { "id": "a135faa7aaa703b3", "type": "inject", "z": "6937d5b12ec486c4", "name": "", "props": [ { "p": "payload", "v": "true", "vt": "bool" }, { "p": "topic", "v": "", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "true", "payloadType": "bool", "x": 130, "y": 900, "wires": [ [ "f660caaff70335f0" ] ] }, { "id": "c99657e1f04c6bc4", "type": "http request", "z": "6937d5b12ec486c4", "name": "", "method": "use", "ret": "txt", "paytoqs": false, "url": "", "tls": "", "persist": false, "proxy": "", "authType": "", "credentials": {}, "x": 450, "y": 900, "wires": [ [ "f1499407499a6e30" ] ] } ]

the id and name of the flow, you can find it in the node-red files, file called "flows.json". if you don't understand something just say.

ryangonzalez133 commented 1 year ago

in case someone doesn't understand, just copy this code and put it in the "import" session in node-red

JosepFarreLlados commented 1 year ago

Hello @ryangonzalez133,

I'm using Node-red v3.0.2 in a docker without login protection, using the lite connection with the auto-ping type set to 0.5

My whatsap connection is established and after some time disconnects (sometimes the connection duration is 20hours). So for this reason I have implemented the reconnect code, See attached my code:

[ { "id": "497316875f3bd222", "type": "tab", "label": "Flow 2", "disabled": false, "info": "", "env": [] }, { "id": "f1499407499a6e30", "type": "debug", "z": "497316875f3bd222", "name": "results", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 670, "y": 180, "wires": [] }, { "id": "a135faa7aaa703b3", "type": "inject", "z": "497316875f3bd222", "name": "", "props": [ { "p": "payload", "v": "true", "vt": "bool" }, { "p": "topic", "v": "", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "true", "payloadType": "bool", "x": 190, "y": 180, "wires": [ [ "f87da43d52a18ed7" ] ] }, { "id": "c99657e1f04c6bc4", "type": "http request", "z": "497316875f3bd222", "name": "", "method": "use", "ret": "txt", "paytoqs": false, "url": "", "tls": "", "persist": false, "proxy": "", "authType": "", "x": 510, "y": 180, "wires": [ [ "f1499407499a6e30" ] ] }, { "id": "8d21e460d6366c74", "type": "admin", "z": "497316875f3bd222", "name": "WA Admin", "whatsappLink": "00297f03b95dc6ea", "x": 210, "y": 100, "wires": [ [ "aef3ffbc1f8e6bf7" ] ] }, { "id": "aef3ffbc1f8e6bf7", "type": "debug", "z": "497316875f3bd222", "name": "debug 3", "active": true, "tosidebar": true, "console": true, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 410, "y": 100, "wires": [] }, { "id": "f87da43d52a18ed7", "type": "function", "z": "497316875f3bd222", "name": "request", "func": "msg.url = 'http://192.168.1.3:1880/#flows'; //The node-red Host\nmsg.method = 'POST';\nmsg.headers = {};\nmsg.headers['content-type'] = \"application/json; charset=utf-8\";\nmsg.headers['Node-RED-Deployment-Type'] = \"reload\";\nmsg.headers['Node-RED-API-Version'] = \"v2\";\nmsg.payload = {\n \"flows\": [\n {\n \"type\": \"tab\",\n \"id\": \"497316875f3bd222\", //the id of the flow you want to restart\n \"label\": \"Flow 2\" //the name of the flow you got the id\n }\n ]\n};\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 340, "y": 180, "wires": [ [ "c99657e1f04c6bc4" ] ] }, { "id": "00297f03b95dc6ea", "type": "whatsappLink", "cName": "whatsapp-web", "name": "Web", "clientType": "waSocketClient", "onlineStatus": true, "loopTime": "0.5" } ]

If I restart or deploy Node-red manually then whatsap connection works. On other hand after send msg.payload true using your code I get the following debug message and whatsap is not re-connected. I set the "id", "label" and the "url of node-red" at your code, where is my mistake?

image

Thanks in advance, Bets regards

ryangonzalez133 commented 1 year ago

Hey friend, try to remove the '#' inside your node-red link, leaving it like this "http://192.168.1.3:1880/flows" possibly for some reason it stopped working with the # in the domain. If it happens again or it doesn't work, use the other code that I say is for those who have login protection on node-red, and just change where it is to have the login and password protection, leave it empty and test it, I tested it here and it worked correctly. Anything let me know

JosepFarreLlados commented 1 year ago

Thanks for your reply @ryangonzalez133,

I'm using your code without login protection and without the '#' from the "url of node-red" and works perfect and restarts all flows!!

Focusing on my setup, I have on Node-Red two flows ("Flow 1" and "Flow 2"). "Flow 1" contain certain global variable that when restarts loses the value. Could be possible only restart the "Flow 2"?

According with the code comments, must restart only "the id of the flow you want to restart", in my case "Flow 2", and what the code do is restart "Flow 1" and "Flow 2" : image

Thanks in advance, Best regards

ryangonzalez133 commented 1 year ago

Hi friend, unfortunately I can't help you. I even researched it at the time, but from what I saw and said, node-red only has this api to restart all flows, although the code only asks for an id and name of the flow, it really doesn't just restart it

Charlloteb2 commented 1 year ago

Hi, just in case someone need a start to keep connected, try import this code:

[{"id":"56de8e70fe3c60fa","type":"chats-in","z":"b2797fd5c8a3e25a","name":"Chats In","whatsappLink":"","whatsappLiteevent":"messages.upsert,connection.update,creds.update,messaging-history.set,chats.upsert,chats.update,chats.delete,presence.update,contacts.upsert,contacts.update,messages.delete,messages.update,messages.media-update,messages.reaction,message-receipt.update,groups.upsert,groups.update,group-participants.update,blocklist.set,blocklist.update,call","whatsappWebevent":"message,message_create,auth_failure,authenticated,change_battery,change_state,disconnected,group_join,group_leave,group_update,incoming_call,media_uploaded,message_ack,message_reaction,message_revoke_everyone,message_revoke_me,qr,ready","x":300,"y":280,"wires":[["aebe11816b229cc8"]]},{"id":"0a76816a678e4192","type":"group-out","z":"b2797fd5c8a3e25a","name":"set group id to receive \"online\"","whatsappLink":"71618517ab9a7ab6","gID":"","x":730,"y":220,"wires":[]},{"id":"aaaed519a6402120","type":"function","z":"b2797fd5c8a3e25a","name":"ping online","func":"msg.payload = {\n text: \"Online\"\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":220,"wires":[["0a76816a678e4192"]]},{"id":"aebe11816b229cc8","type":"delay","z":"b2797fd5c8a3e25a","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"2","nbRateUnits":"600","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":460,"y":280,"wires":[["07697a67e66096dd"]]},{"id":"07697a67e66096dd","type":"change","z":"b2797fd5c8a3e25a","name":"set timer1 to 0","rules":[{"t":"set","p":"timer1","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":280,"wires":[[]]},{"id":"bc9cf4ef23ad9f7a","type":"inject","z":"b2797fd5c8a3e25a","name":"1 second per second","props":[],"repeat":"1","crontab":"","once":true,"onceDelay":"3","topic":"","x":280,"y":140,"wires":[["5323f13c64e531d5"]]},{"id":"1a5e38569a154a6f","type":"change","z":"b2797fd5c8a3e25a","name":"timer1 + 1 per second","rules":[{"t":"set","p":"timer1","pt":"flow","to":"payload+1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":140,"wires":[[]]},{"id":"9ce002268992cba6","type":"inject","z":"b2797fd5c8a3e25a","name":"start timer","props":[],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","x":310,"y":100,"wires":[["6f4cb5431b7792cc"]]},{"id":"6f4cb5431b7792cc","type":"change","z":"b2797fd5c8a3e25a","name":"creates timer1 as 0","rules":[{"t":"set","p":"timer1","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":100,"wires":[[]]},{"id":"5323f13c64e531d5","type":"function","z":"b2797fd5c8a3e25a","name":"keep","func":"msg.payload = flow.get('timer1');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":140,"wires":[["1a5e38569a154a6f"]]},{"id":"d3a1171054f1fe7d","type":"inject","z":"b2797fd5c8a3e25a","name":"check if timer > limit offline","props":[],"repeat":"60","crontab":"","once":true,"onceDelay":"60","topic":"","x":260,"y":180,"wires":[["eb7e5523c8f70cb3"]]},{"id":"eb7e5523c8f70cb3","type":"switch","z":"b2797fd5c8a3e25a","name":"[ offline seconds limit ]","property":"timer1","propertyType":"flow","rules":[{"t":"gte","v":"1300","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":520,"y":180,"wires":[["59d75f083f6405af"]]},{"id":"6aae6a1fa7751b4b","type":"inject","z":"b2797fd5c8a3e25a","name":"[ set ping rate ]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"600","crontab":"","once":true,"onceDelay":"10","topic":"","payload":"","payloadType":"date","x":300,"y":220,"wires":[["aaaed519a6402120"]]},{"id":"59d75f083f6405af","type":"debug","z":"b2797fd5c8a3e25a","name":"put here the node that restarts node-red flows that work for you","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"71618517ab9a7ab6","type":"whatsappLink","cName":"whatsapp-web","name":"Web","clientType":"waSocketClient","onlineStatus":false,"loopTime":"0.1"}]

This is to keep sending at a interval, and trigger your restart node flows, if no one interaction to chat in for a time limit. Its just a start, to use you need:

that way will reset a timer if something detected, and if the timer get over a limit it triggerr your restart node flows... you can tweak it to your needs.