Page 1 of 2

Custom http notification

Posted: Wednesday 23 September 2020 21:38
by Varazir
Hello

I have written this and I just like to check if I could have done it in another way or if something is wrong.
Needed a bit mot flexible notification system.
API I'm using is from this site https://joaoapps.com/join/api/
I wised I could encrypt my message as well but then I need some thing i wrote in this script https://github.com/varazir/join/blob/ma ... message.pl ( top modules loaded ) and code
https://github.com/varazir/join/blob/11 ... ge.pl#L325

The API can be used to send command to my phone to execute and then I don't need the title and icon.

dzVents: global_data.lua

Code: Select all

	helpers = {
		join = function(dz, deviceId, command, title)
		    icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
		    toID = "&deviceId=" .. deviceId
		    if string.match(command, "=:=") then -- if command contains  "=:=" it's should not add title and a icon
		       message = "&text=" .. command
		    else
		        message = "&title=" .. title .. "&text=" .. command .. icon
		    end
		    message = message .. toID
			message = dz.urlEncode(message)
			dz.notify(message,dz.PRIORITY_NORMAL,dz.NSS_HTTP)
		end
	}

Re: Custom http nonfiction

Posted: Wednesday 23 September 2020 21:50
by waaren
Varazir wrote: Wednesday 23 September 2020 21:38 I have written this and I just like to check if I could have done it in another way or if something is wrong.
I have no real clue what this is doing but I do see that the parms of dz.notify are not correct.
NSS.png
NSS.png (59.2 KiB) Viewed 1845 times

Re: Custom http nonfiction

Posted: Wednesday 23 September 2020 21:57
by Varazir
waaren wrote: Wednesday 23 September 2020 21:50
Varazir wrote: Wednesday 23 September 2020 21:38 I have written this and I just like to check if I could have done it in another way or if something is wrong.
I have no real clue what this is doing but I do see that the parms of dz.notify are not correct.

NSS.png
Well it's a over complicated way to use the http notification function in Domoticz :)
I guess I could just use the openurl() as well but then I need to have the API key in the script.


Strange as this command works just fine

Code: Select all

dz.notify("Aktuell grupp",control[selectedGroupNumber],dz.PRIORITY_NORMAL,dz.NSS_HTTP)

Re: Custom http nonfiction

Posted: Wednesday 23 September 2020 22:03
by Varazir
Example could be that if I wish my phone to run a command it sends a message like this
&text=domoticz=:=RunMytask

The Tasker app on my phone will pick up that command and execute what I have set it up to run.

But if I just want a notification on my phone I need something like this

&title=warning&text=Window%20open&icon=UrltoIcon

Re: Custom http nonfiction

Posted: Wednesday 23 September 2020 22:07
by waaren
Varazir wrote: Wednesday 23 September 2020 21:57 Strange as this command works just fine

Code: Select all

dz.notify("Aktuell grupp",control[selectedGroupNumber],dz.PRIORITY_NORMAL,dz.NSS_HTTP)
It might look like its OK but what you send is

subject:"Aktuell grupp"
message:control[selectedGroupNumber]
priority: dz.PRIORITY_NORMAL
sound: dz.NSS_HTTP -- ignored for all subsystems except pushover
extra: nil -- ignored
subsystem: nil -- send to all configured subsystems

And for the notify in you helper
dz.notify(message,dz.PRIORITY_NORMAL,dz.NSS_HTTP)

subject:message
message:dz.PRIORITY_NORMAL -- a number
priority: dz.NSS_HTTP -- a string
sound: nil -- ignored for all subsystems except pushover
extra: nil -- ignored
subsystem: nil -- send to all configured subsystems

Re: Custom http nonfiction

Posted: Thursday 24 September 2020 18:52
by Varazir
waaren wrote: Wednesday 23 September 2020 22:07
Varazir wrote: Wednesday 23 September 2020 21:57 Strange as this command works just fine

Code: Select all

dz.notify("Aktuell grupp",control[selectedGroupNumber],dz.PRIORITY_NORMAL,dz.NSS_HTTP)
It might look like its OK but what you send is

subject:"Aktuell grupp"
message:control[selectedGroupNumber]
priority: dz.PRIORITY_NORMAL
sound: dz.NSS_HTTP -- ignored for all subsystems except pushover
extra: nil -- ignored
subsystem: nil -- send to all configured subsystems

And for the notify in you helper
dz.notify(message,dz.PRIORITY_NORMAL,dz.NSS_HTTP)

subject:message
message:dz.PRIORITY_NORMAL -- a number
priority: dz.NSS_HTTP -- a string
sound: nil -- ignored for all subsystems except pushover
extra: nil -- ignored
subsystem: nil -- send to all configured subsystems
I misread the wiki, is there a way to only send a subject or a message ?

just add a extra , ?

Re: Custom http nonfiction

Posted: Thursday 24 September 2020 20:47
by waaren
Varazir wrote: Thursday 24 September 2020 18:52 I misread the wiki, is there a way to only send a subject or a message ?
Subject is the only required parameter. All other ones can be nil (implicit or explcit)

so

Code: Select all

domoticz.notify('subject') -- OK will send message to all configured subsystems
domoticz.notify('subject',nil,nil,nil,nil,dz.NSS_PUSHOVER) -- OK will send message to Pushover
domoticz.notify('subject',,,,,dz.NSS_PUSHOVER) -- NOT OK (lua does not recognize ,,)

Re: Custom http nonfiction

Posted: Thursday 24 September 2020 21:59
by Varazir
waaren wrote: Thursday 24 September 2020 20:47
Varazir wrote: Thursday 24 September 2020 18:52 I misread the wiki, is there a way to only send a subject or a message ?
Subject is the only required parameter. All other ones can be nil (implicit or explcit)

so

Code: Select all

domoticz.notify('subject') -- OK will send message to all configured subsystems
domoticz.notify('subject',nil,nil,nil,nil,dz.NSS_PUSHOVER) -- OK will send message to Pushover
domoticz.notify('subject',,,,,dz.NSS_PUSHOVER) -- NOT OK (lua does not recognize ,,)
Okay the wiki wasn't that clear about that.

I got it half way now.

This is the original url I have in the https notification settings

Code: Select all

https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?apikey=#FIELD1&title=#SUBJECT&text=#MESSAGE&deviceId=#TO&icon=#FIELD2
Works just fine but if I change it to

Code: Select all

https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?apikey=#FIELD1#SUBJECT
Nothing happens, I don't get any errors in the log.

The current script

Code: Select all

	helpers = {
		join = function(dz, deviceId, command, title)
		    icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
		    toID = "&deviceId=" .. deviceId
		    if string.match(command, "=:=") then 
		       message = "&text=" .. command
		    else
		        title = dz.urlEncode(title)
		        command = dz.urlEncode(command)
		        message = "&title=" .. title .. "&text=" .. command .. icon
		    end
		    message = message .. toID
			dz.notify(message,nil,nil,nil,dz.PRIORITY_NORMAL,dz.NSS_HTTP)
		end
	}
I call it like this:

Code: Select all

dz.helpers.join(dz,"group.all","domoticz","SovrumGN")
It looks okay in the log as far as I can understand it. would love the to see the whole https command

Re: Custom http nonfiction

Posted: Thursday 24 September 2020 23:15
by waaren
Varazir wrote: Thursday 24 September 2020 21:59 It looks okay in the log as far as I can understand it. would love the to see the whole https command
I guess I miss your point but if your objective is to send a url why taking the detour via dz.notify?
You might want to try the domoticz.openURL() method instead.

Re: Custom http nonfiction

Posted: Friday 25 September 2020 6:37
by Varazir
waaren wrote:
Varazir wrote: Thursday 24 September 2020 21:59 It looks okay in the log as far as I can understand it. would love the to see the whole https command
I guess I miss your point but if your objective is to send a url why taking the detour via dz.notify?
You might want to try the domoticz.openURL() method instead.
Been thinking of that. My concern is to have the api key in the script.

Skickat från min EML-L29 via Tapatalk


Re: Custom http nonfiction

Posted: Friday 25 September 2020 9:30
by waaren
Varazir wrote:Been thinking of that. My concern is to have the api key in the script.
You could store the key in a domoticz uservariable and retrieve it during script execution. You might want to use base64 code/decode for that to ensure the password is not direct readable.
(or encryption / decryption if you really want to make it fancy)

Re: Custom http nonfiction

Posted: Sunday 27 September 2020 9:44
by Varazir
waaren wrote: Friday 25 September 2020 9:30
Varazir wrote:Been thinking of that. My concern is to have the api key in the script.
You could store the key in a domoticz uservariable and retrieve it during script execution. You might want to use base64 code/decode for that to ensure the password is not direct readable.
(or encryption / decryption if you really want to make it fancy)
That is a solution, I'll tak a look at that.

Do Domoticz have PBKDF2, CBC/AES encryption support ?

Re: Custom http nonfiction

Posted: Sunday 27 September 2020 11:49
by waaren
Varazir wrote: Sunday 27 September 2020 9:44 That is a solution, I'll tak a look at that.
Do Domoticz have PBKDF2, CBC/AES encryption support ?
Don't know but dzVents has not natively. You could use external Lua to do that but for this purpose I guess you could also use
dz.utils.toBase64 / dz.utils.fromBase64 and add your own key to the coded string before storing (and remove it before using) to obscure it even more.

Re: Custom http nonfiction

Posted: Sunday 27 September 2020 18:01
by Varazir
waaren wrote: Sunday 27 September 2020 11:49
Varazir wrote: Sunday 27 September 2020 9:44 That is a solution, I'll tak a look at that.
Do Domoticz have PBKDF2, CBC/AES encryption support ?
Don't know but dzVents has not natively. You could use external Lua to do that but for this purpose I guess you could also use
dz.utils.toBase64 / dz.utils.fromBase64 and add your own key to the coded string before storing (and remove it before using) to obscure it even more.
Hmm okay, is there a way to call a external perl script and get value back ?

Re: Custom http nonfiction

Posted: Sunday 27 September 2020 22:17
by waaren
Varazir wrote: Sunday 27 September 2020 18:01 Is there a way to call a external perl script and get value back ?
Yes if you are on the latest Beta you can use

Code: Select all

returnValue, returnCode = dz.utils.osCommand('perl /pathToScript/scriptName.pl')
If on an older version

use

Code: Select all

        local function osCommand(cmd)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

returnValue, returnCode = osCommand('perl /pathToScript/scriptName.pl'

Re: Custom http nonfiction

Posted: Monday 28 September 2020 14:22
by Varazir
waaren wrote: Sunday 27 September 2020 22:17 Yes.
Ya I'm using beta

Something like this then ?

Code: Select all

return {
	-- global persistent data
	data = {
		-- myGlobalVar = { initial = 12 }
	},
	-- global helper functions
	helpers = {
		join = function(dz, deviceId, command, title)
		    icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
		    toID = "&deviceId=" .. deviceId
		    JoinKey = "apikey=" .. dz.variables('JoinKey')
		    JoinURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"
		    if string.match(command, "=:=") then 
		       message = "&text=" .. command
		    else
		        title = dz.urlEncode(title)
		        command = dz.urlEncode(command)
		        message = "&title=" .. title .. "&text=" .. command .. icon
		    end
		    returnMessage, returnCode = dz.utils.osCommand('perl /opt/join/encode.pl' .. message )
		    message = JoinKey .. returnMessage .. toID
		    
		    dz.openurl( JoinURL .. message )
		    
			-- dz.notify(message,nil,nil,nil,dz.PRIORITY_NORMAL,dz.NSS_HTTP)
		end
	}
}

Re: Custom http nonfiction

Posted: Monday 28 September 2020 15:58
by waaren
Varazir wrote: Monday 28 September 2020 14:22
waaren wrote: Sunday 27 September 2020 22:17 Yes.
Ya I'm using beta

Something like this then ?
Some typos in your code... See my remarks

Code: Select all

return 
{
	-- global helper functions
	helpers = {
		join = function(dz, deviceId, command, title)
		    icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
		    toID = "&deviceId=" .. deviceId
		    JoinKey = "apikey=" .. dz.variables('JoinKey').value -- You only need the value; not the complete variable object 
 		    JoinURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"
		    if string.match(command, "=:=") then 
		       message = "&text=" .. command
		    else
		        title = dz.utils.urlEncode(title) -- urlEncode is a method in utils 
		        command = dz.utils.urlEncode(command)
		        message = "&title=" .. title .. "&text=" .. command .. icon
		    end
		    returnMessage, returnCode = dz.utils.osCommand('perl /opt/join/encode.pl ' .. message ) -- mind the space before the message parm 
		    message = JoinKey .. returnMessage .. toID
		    
		    dz.openurl( JoinURL .. message )
		    
		end
	}
}

Re: Custom http nonfiction

Posted: Wednesday 30 September 2020 19:09
by Varazir
waaren wrote: Monday 28 September 2020 15:58
Varazir wrote: Monday 28 September 2020 14:22
waaren wrote: Sunday 27 September 2020 22:17 Yes.
Ya I'm using beta

Something like this then ?
Some typos in your code... See my remarks

Code: Select all

return 
{
	-- global helper functions
	helpers = {
		join = function(dz, deviceId, command, title)
		    icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
		    toID = "&deviceId=" .. deviceId
		    JoinKey = "apikey=" .. dz.variables('JoinKey').value -- You only need the value; not the complete variable object 
 		    JoinURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"
		    if string.match(command, "=:=") then 
		       message = "&text=" .. command
		    else
		        title = dz.utils.urlEncode(title) -- urlEncode is a method in utils 
		        command = dz.utils.urlEncode(command)
		        message = "&title=" .. title .. "&text=" .. command .. icon
		    end
		    returnMessage, returnCode = dz.utils.osCommand('perl /opt/join/encode.pl ' .. message ) -- mind the space before the message parm 
		    message = JoinKey .. returnMessage .. toID
		    
		    dz.openurl( JoinURL .. message )
		    
		end
	}
}
Hmm, I got stuck

I'm getting

Code: Select all

2020-09-30 19:05:25.258 Error: dzVents: Error: (3.0.12) An error occurred when calling event handler test
2020-09-30 19:05:25.258 Error: dzVents: Error: (3.0.12) ...moticz/scripts/dzVents/generated_scripts/global_data.lua:31: attempt to call a nil value (field 'osCommand'

Re: Custom http nonfiction

Posted: Wednesday 30 September 2020 23:01
by waaren
Varazir wrote: Wednesday 30 September 2020 19:09 Hmm, I got stuck
I'm getting

Code: Select all

2020-09-30 19:05:25.258 Error: dzVents: Error: (3.0.12) An error occurred when calling event handler test
2020-09-30 19:05:25.258 Error: dzVents: Error: (3.0.12) ...moticz/scripts/dzVents/generated_scripts/global_data.lua:31: attempt to call a nil value (field 'osCommand'
dz.utils.osCommand is available in dzVents versions >= 3.0.13 (domoticz version 2002.2 build 12394) If you cannot upgrade to that Beta version yet you will have to include the function I posted earlier (see below to see how.

Code: Select all

return
{
    -- global helper functions
    helpers = 
    {
        join = function(dz, deviceId, command, title)

            local function osCommand(cmd)
                dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

                local fileHandle = assert(io.popen(cmd))
                local commandOutput = assert(fileHandle:read('*a'))
                local returnTable = {fileHandle:close()}

                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
                return commandOutput,returnTable[3] -- rc[3] contains returnCode
            end

            icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
            toID = "&deviceId=" .. deviceId
            JoinKey = "apikey=" .. dz.variables('JoinKey').value -- You only need the value; not the complete variable object
             JoinURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"
            if string.match(command, "=:=") then
               message = "&text=" .. command
            else
                title = dz.utils.urlEncode(title) -- urlEncode is a method in utils
                command = dz.utils.urlEncode(command)
                message = "&title=" .. title .. "&text=" .. command .. icon
            end
            returnMessage, returnCode = osCommand('perl /opt/join/encode.pl ' .. message ) -- mind the space before the message parm
            message = JoinKey .. returnMessage .. toID

            dz.openurl( JoinURL .. message )

        end
    }
}

Re: Custom http nonfiction

Posted: Thursday 01 October 2020 7:35
by Varazir
waaren wrote:
Varazir wrote: Wednesday 30 September 2020 19:09 Hmm, I got stuck
I'm getting

Code: Select all

2020-09-30 19:05:25.258 Error: dzVents: Error: (3.0.12) An error occurred when calling event handler test
2020-09-30 19:05:25.258 Error: dzVents: Error: (3.0.12) ...moticz/scripts/dzVents/generated_scripts/global_data.lua:31: attempt to call a nil value (field 'osCommand'
dz.utils.osCommand is available in dzVents versions >= 3.0.13 (domoticz version 2002.2 build 12394) If you cannot upgrade to that Beta version yet you will have to include the function I posted earlier (see below to see how.

Code: Select all

return
{
    -- global helper functions
    helpers = 
    {
        join = function(dz, deviceId, command, title)

            local function osCommand(cmd)
                dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

                local fileHandle = assert(io.popen(cmd))
                local commandOutput = assert(fileHandle:read('*a'))
                local returnTable = {fileHandle:close()}

                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
                return commandOutput,returnTable[3] -- rc[3] contains returnCode
            end

            icon = "&icon=https://raw.githubusercontent.com/domoticz/domoticz/master/www/images/logo.png"
            toID = "&deviceId=" .. deviceId
            JoinKey = "apikey=" .. dz.variables('JoinKey').value -- You only need the value; not the complete variable object
             JoinURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"
            if string.match(command, "=:=") then
               message = "&text=" .. command
            else
                title = dz.utils.urlEncode(title) -- urlEncode is a method in utils
                command = dz.utils.urlEncode(command)
                message = "&title=" .. title .. "&text=" .. command .. icon
            end
            returnMessage, returnCode = osCommand('perl /opt/join/encode.pl ' .. message ) -- mind the space before the message parm
            message = JoinKey .. returnMessage .. toID

            dz.openurl( JoinURL .. message )

        end
    }
}
I'm running

About Domoticz
Version: 2020.2 (build 12393)
Build Hash: 46660897d-modified
Compile Date: 2020-09-21 13:52:00
dzVents Version: 3.0.12
Python Version: 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]


See it now, well I have to wait for next beta release.

Skickat från min EML-L29 via Tapatalk