Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get SMS received node #11

Closed
ioull opened this issue Mar 17, 2022 · 11 comments
Closed

Get SMS received node #11

ioull opened this issue Mar 17, 2022 · 11 comments
Labels
enhancement New feature or request stale

Comments

@ioull
Copy link

ioull commented Mar 17, 2022

It could be very interressant to have a node which receive the SMS in order to do something using sender phone number and text message !

@grawsom
Copy link

grawsom commented Mar 17, 2022

Maybe I need to get back to this package - I was using this but with old AT stuff, and the "receiving" part is all thats holding mw back.
So maybe I should have a look into how long I came

@zinen
Copy link
Owner

zinen commented Mar 22, 2022

This have been requested before by @grawsom himself in #6. The biggest problem for me is that I can't test this as my router don't do sms. So to make this I would need some info from you guys on how to be successful and manage read/unread sms and maybe delete sms. Looking at the API sepcs I can see a function called getSmsList , deleteSms, setRead, moveSms and more.

Do you see a logical way of receiving a sms? My thoughts of the flow:

  1. Start from external node (e.g. like a inject node in repeat mode running every 10 minutes)
  2. getSmsList of all unread sms
  3. something is missing here, where do I read the content of an smsId?
  4. deleteSms on the items extracted via step 2
  5. Return an array of the unread sms to node red flow in form a an array of objects

This would clear up the inbox of the router and in case for errors the sms will be unread in router and be deleted on next successful run.

@grawsom
Copy link

grawsom commented Mar 23, 2022

@ioull @zinen I will try to get back to my project - hopefully at the lastest this week, and see if we can get this working, I'm using Huawei 3372h-320, so I can send and receive sms's.
Just remember - I'm only a hobby programmer, so it's trial and error, take bits here and there and put together and learning by doing.

@ioull
Copy link
Author

ioull commented Mar 23, 2022

Reading this code https://github.com/chenwei791129/Huawei-LTE-Router-SMS-to-E-mail-Sender/blob/master/check-sms.py (from python library) It seems that it use getSmsList to get the smscontent and the call phone number.
Perhaps you can jsut have a node for managing readed sms without any particular threatment, and the user decide to mark as read or delete the received sms from its own ?

@zinen
Copy link
Owner

zinen commented Mar 23, 2022

Try this out @grawsom. What do you see as result?

const huaweiLteApi = require('huawei-lte-api')
let connection
async function getSms() {
    try {
        connection = new huaweiLteApi.Connection('http://admin:password@192.168.8.1/')
        await connection.ready
        const result = await new huaweiLteApi.Sms(connection).getSmsList()
        console.log('getSmsList result =', result)
    } catch (error) {
        console.error('Ended in error:')
        console.error(error)
    } finally {
        // finallyCode - Code block to be executed regardless of the try result
        const resultLogout = await huaweiLteApi.User(connection).logout()
        console.log('result from logout=',resultLogout)
        connection = null
    }
}
getSms()

@grawsom
Copy link

grawsom commented Mar 27, 2022

@zinen I get this error with this scrìpt

C:\Users\ivans\.node-red\node_modules>node test.js
getSmsList result = { Count: '0', Messages: '\n\t' }
C:\Users\ivans\.node-red\node_modules\test.js:14
        const resultLogout = await huaweiLteApi.User(connection).logout()
                                                                 ^

TypeError: huaweiLteApi.User(...).logout is not a function
    at getSms (C:\Users\ivans\.node-red\node_modules\test.js:14:66)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Like I said back to the drawing board, I now have a running sample that reads sms then delete the sms, only problem is I get error 125003: Unknown.

If I just use my readsms function and set the inject node to do this every 5 sec. I get this error on every third on.

27.3.2022 15.41.04[node: 6339d13d376d009b](http://127.0.0.1:1880/#)msg : Object
{ _msgid: "181b272c4b7a0dea", payload: object }
27.3.2022 15.41.09[node: 6339d13d376d009b](http://127.0.0.1:1880/#)msg : Object
{ _msgid: "4119118b37d94e14", payload: object }
27.3.2022 15.41.14[node: ba73047714250071](http://127.0.0.1:1880/#)
msg : string[15]
"125003: Unknown"

I then went along and did the same with your "send sms" and I get the same error on every 3'one.

I have LTE-sms and I have "tried" to add you to my private repository - and added my flow to readme for you to see.

@grawsom
Copy link

grawsom commented Mar 27, 2022

@zinen if I do this, I can do it again and again, but how to gat that in my node without getting errors.

const huaweiLteApi = require('huawei-lte-api')
let connection
async function getSms() {
    try {
        connection = new huaweiLteApi.Connection('http://admin:password@192.168.8.1/')
        await connection.ready
        const result = await new huaweiLteApi.Sms(connection).getSmsList()
        console.log('getSmsList result =', result)
    } catch (error) {
        console.error('Ended in error:')
        console.error(error)
    } finally {
        const user = new huaweiLteApi.User(connection)
        // finallyCode - Code block to be executed regardless of the try result
        const resultLogout = await user.logout()
        console.log('result from logout=',resultLogout)
        connection = null
    }
}
getSms()

I get this result.

C:\Users\ivans\.node-red\node_modules>node test.js
getSmsList result = { Count: '0', Messages: '\n\t' }
result from logout= OK

@zinen
Copy link
Owner

zinen commented Mar 28, 2022

Okay, looks like you acual got something back. { Count: '0', Messages: '\n\t' } should indicate either that you got zero messages in inbox or zero new/unread messages. Does this match your expectations?

@grawsom
Copy link

grawsom commented May 14, 2022

Yes - ..

But I can't get it to work in the node,. don't know how to go on from here.

@grawsom
Copy link

grawsom commented May 15, 2022

@zinen
@ioull
I have droped the Node stuff and instead made a flow that handles it, with the help off the script that we saw working.

[{"id":"b1c0d59a0b7da425","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"1b04a0c8a9c867be","type":"debug","z":"b1c0d59a0b7da425","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":220,"wires":[]},{"id":"edb8d07028a8e466","type":"function","z":"b1c0d59a0b7da425","name":"Check if any messages","func":"let c = Number(msg.payload.Count);\nif (c > 0) {\nreturn [msg, null]\n}else {\n    return [null, msg]\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":200,"wires":[["f54ed3555a011101","e9a74fef3071f5f6"],["1b04a0c8a9c867be"]],"outputLabels":["New Message","No New Messages"]},{"id":"4b047faf93dd8a46","type":"file","z":"b1c0d59a0b7da425","name":"","filename":"","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":720,"y":260,"wires":[["e80555f57328e0b0"]]},{"id":"f68fda565295f77e","type":"debug","z":"b1c0d59a0b7da425","name":"Output Single Index","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":320,"wires":[]},{"id":"f0bb1ab630579117","type":"file in","z":"b1c0d59a0b7da425","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":360,"y":400,"wires":[["760ca1fcaea46b11"]]},{"id":"880ef1cd60d857df","type":"inject","z":"b1c0d59a0b7da425","name":"","props":[{"p":"index","v":"40026","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":60,"wires":[["5f68fccb2fb2f2ad"]]},{"id":"6339d13d376d009b","type":"debug","z":"b1c0d59a0b7da425","name":"Result from deleteSms","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":60,"wires":[]},{"id":"f5a38b6b8cd119f3","type":"debug","z":"b1c0d59a0b7da425","name":"Result from getSmsList","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":140,"wires":[]},{"id":"1320e7aaeec4d3c5","type":"inject","z":"b1c0d59a0b7da425","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":140,"wires":[["b35b50e3fbb728d8"]]},{"id":"e9a74fef3071f5f6","type":"function","z":"b1c0d59a0b7da425","name":"If messages - save info to file ","func":"var d = new Date().toLocaleString().replace(/:/g, \"\");\n\nvar folder = \"D:\\\\sms\\\\incoming\\\\\";\nvar folderandname = folder.concat(d);\nvar finalPath = folderandname.concat(\".txt\");\nmsg.filename = finalPath;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":260,"wires":[["4b047faf93dd8a46"]]},{"id":"2bb92f9b4065b673","type":"watch","z":"b1c0d59a0b7da425","name":"","files":"D:\\\\sms\\\\incoming","recursive":"","x":130,"y":400,"wires":[["c05ad4167bc80dce"]]},{"id":"3357b7670cb616b1","type":"function","z":"b1c0d59a0b7da425","name":"","func":"\nmsg.payload = JSON.parse(msg.payload);\nnode.warn(msg.payload);\n//node.warn(msg.payload.Messages.Message.length);\n\n\nvar count = Number(msg.payload.Count);\nvar messageCount = msg.payload.Messages.Message.length;\nnode.warn(\"Number of messages:\" + count);\nnode.warn(\"Number of arrays :\" + messageCount);\n\nif (count === 1)\n{\n    var index = msg.payload.Messages.Message.Index;\n    var msg1 = {\n        index: index\n    };\n\n    msg1.topic = msg.payload.Messages.Message.Phone;\n    msg1.payload = msg.payload.Messages.Message.Content;\n\n} else \n{\n    node.send([null, msg]);\n}\n\nreturn [msg1, null]\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":400,"wires":[["f68fda565295f77e","22fbd6a6c9f1eca0"],["df1c0fb3d38f0dd4","83cb1149efe5a502"]],"outputLabels":["Message","Delete single message"]},{"id":"df1c0fb3d38f0dd4","type":"debug","z":"b1c0d59a0b7da425","name":"Output Multible Index","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":360,"wires":[]},{"id":"f54ed3555a011101","type":"debug","z":"b1c0d59a0b7da425","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":180,"wires":[]},{"id":"22fbd6a6c9f1eca0","type":"delay","z":"b1c0d59a0b7da425","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":920,"y":400,"wires":[["5fdf91d2a0cc7b5d"]]},{"id":"c05ad4167bc80dce","type":"delay","z":"b1c0d59a0b7da425","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":340,"y":340,"wires":[["f0bb1ab630579117"]]},{"id":"b35b50e3fbb728d8","type":"function","z":"b1c0d59a0b7da425","name":"API - getSmsList","func":"let connection\n//async function getSms() {\n    try {\n        connection = new huaweiLteApi.Connection('http://admin:admin@192.168.8.1/')\n        await connection.ready\n        const result = await new huaweiLteApi.Sms(connection).getSmsList()\n        msg.payload = result\n        \n        //console.log('getSmsList result =', result)\n    } catch (error) {\n        console.error('Ended in error:')\n        console.error(error)\n    } finally {\n        const user = new huaweiLteApi.User(connection)\n        // finallyCode - Code block to be executed regardless of the try result\n        const resultLogout = await user.logout()\n        console.log('result from logout=', resultLogout)\n        connection = null\n    }\n//}getSms()\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"huaweiLteApi","module":"huawei-lte-api"}],"x":310,"y":140,"wires":[["f5a38b6b8cd119f3","edb8d07028a8e466"]]},{"id":"5f68fccb2fb2f2ad","type":"function","z":"b1c0d59a0b7da425","name":"API - deleteSms","func":"var index = msg.index\nnode.warn(index);\nlet connection\n//async function getSms() {\n    try {\n        connection = new huaweiLteApi.Connection('http://admin:admin@192.168.8.1/')\n        await connection.ready\n        const result = await new huaweiLteApi.Sms(connection).deleteSms(index)\n        msg.payload = result\n        \n        //console.log('getSmsList result =', result)\n    } catch (error) {\n        console.error('Ended in error:')\n        console.error(error)\n    } finally {\n        const user = new huaweiLteApi.User(connection)\n        // finallyCode - Code block to be executed regardless of the try result\n        const resultLogout = await user.logout()\n        console.log('result from logout=', resultLogout)\n        connection = null\n    }\n//}getSms()\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"huaweiLteApi","module":"huawei-lte-api"}],"x":300,"y":60,"wires":[["6339d13d376d009b"]]},{"id":"55aad345bc81a73e","type":"inject","z":"b1c0d59a0b7da425","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":460,"wires":[["c05ad4167bc80dce"]]},{"id":"83cb1149efe5a502","type":"change","z":"b1c0d59a0b7da425","name":"Move messages","rules":[{"t":"move","p":"payload.Messages.Message","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":480,"wires":[["e27fbfbc011773c3"]]},{"id":"e27fbfbc011773c3","type":"split","z":"b1c0d59a0b7da425","name":"Split messages","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":540,"y":480,"wires":[["93b38b09d72407ed"]]},{"id":"93b38b09d72407ed","type":"change","z":"b1c0d59a0b7da425","name":"","rules":[{"t":"move","p":"payload.Index","pt":"msg","to":"index","tot":"msg"},{"t":"delete","p":"topic","pt":"msg"},{"t":"move","p":"payload.Phone","pt":"msg","to":"topic","tot":"msg"},{"t":"move","p":"payload.Content","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"file","pt":"msg"},{"t":"delete","p":"filename","pt":"msg"},{"t":"delete","p":"size","pt":"msg"},{"t":"delete","p":"type","pt":"msg"},{"t":"delete","p":"parts","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":480,"wires":[["22fbd6a6c9f1eca0"]]},{"id":"e80555f57328e0b0","type":"debug","z":"b1c0d59a0b7da425","name":"Result of write file","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1010,"y":260,"wires":[]},{"id":"954e2e97e92e8526","type":"comment","z":"b1c0d59a0b7da425","name":"Watch for new files","info":"","x":130,"y":340,"wires":[]},{"id":"9161afa981b28184","type":"link in","z":"b1c0d59a0b7da425","name":"Delete In","links":["9c31e241b82ea1cc"],"x":155,"y":100,"wires":[["5f68fccb2fb2f2ad"]]},{"id":"9c31e241b82ea1cc","type":"link out","z":"b1c0d59a0b7da425","name":"Delete Out","mode":"link","links":["9161afa981b28184"],"x":1145,"y":520,"wires":[]},{"id":"760ca1fcaea46b11","type":"rbe","z":"b1c0d59a0b7da425","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":530,"y":400,"wires":[["3357b7670cb616b1"]]},{"id":"a460f6e89838da25","type":"debug","z":"b1c0d59a0b7da425","name":"Message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1120,"y":440,"wires":[]},{"id":"d8a02293147d4f06","type":"debug","z":"b1c0d59a0b7da425","name":"Delete","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":480,"wires":[]},{"id":"5fdf91d2a0cc7b5d","type":"function","z":"b1c0d59a0b7da425","name":"","func":"var index = msg.index;\nvar msg1 = {\n    index: index\n};\ndelete msg.index;\nmsg.topic = msg.topic;\nmsg.payload = msg.payload;\n\nreturn [msg, msg1]\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":460,"wires":[["a460f6e89838da25"],["d8a02293147d4f06","0e1231801d1eb33c"]],"outputLabels":["Message","Delete message index"]},{"id":"0e1231801d1eb33c","type":"delay","z":"b1c0d59a0b7da425","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"2","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":960,"y":560,"wires":[["64d5e2bd68343ca3","9c31e241b82ea1cc"]]},{"id":"7c7296aa07a424bc","type":"trigger","z":"b1c0d59a0b7da425","name":"","op1":"1","op2":"0","op1type":"val","op2type":"val","duration":"250","extend":"false","overrideDelay":"false","units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":440,"y":660,"wires":[[]]},{"id":"64d5e2bd68343ca3","type":"debug","z":"b1c0d59a0b7da425","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"index","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":560,"wires":[]}]

@zinen
Copy link
Owner

zinen commented Jan 23, 2023

I can't test this so this topic is going stale. Closing until new request and testers comes forward.

@zinen zinen closed this as completed Jan 23, 2023
@zinen zinen added stale enhancement New feature or request labels Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale
Projects
None yet
Development

No branches or pull requests

3 participants