Page 1 of 1

Push notifications via Nextcloud

Posted: Monday 18 January 2021 11:45
by m147
Hi,

I think that a lot of users has installed Nextcloud server along with Domoticz and would be nice to use a Nextcloud as push notification server for Domoticz. Unfortunately I don't have enough skills to write plug-in for that.

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 13:26
by m147
Please can anyone help me how to configure domoticz.openURL function to act like this command?

Code: Select all

curl -H "OCS-APIREQUEST: true" -X POST   https://ADMIN:TOKEN@URL/ocs/v2.php/apps/admin_notifications/api/v1/notifications/USER   -d "shortMessage=test"
At least I am trying to make DzVents script for notification...

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 13:43
by waaren
m147 wrote: Tuesday 19 January 2021 13:26 Please can anyone help me how to configure domoticz.openURL function to act like this command?

Code: Select all

curl -H "OCS-APIREQUEST: true" -X POST   https://ADMIN:TOKEN@URL/ocs/v2.php/apps/admin_notifications/api/v1/notifications/USER   -d "shortMessage=test"
Something like

Code: Select all

domoticz.openURL({
    url = 'https://ADMIN:TOKEN@URL/ocs/v2.php/apps/admin_notifications/api/v1/notifications/USER',
    headers = { ['OCS-APIREQUEST'] = true },
    postData = { ['shortMessage'] = 'test' },
    method = 'POST',
    callback = 'Could be any string or left out completely',
})

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 14:10
by m147
waaren wrote: Tuesday 19 January 2021 13:43
m147 wrote: Tuesday 19 January 2021 13:26 Please can anyone help me how to configure domoticz.openURL function to act like this command?

Code: Select all

curl -H "OCS-APIREQUEST: true" -X POST   https://ADMIN:TOKEN@URL/ocs/v2.php/apps/admin_notifications/api/v1/notifications/USER   -d "shortMessage=test"
Something like

Code: Select all

domoticz.openURL({
    url = 'https://ADMIN:TOKEN@URL/ocs/v2.php/apps/admin_notifications/api/v1/notifications/USER',
    headers = { ['OCS-APIREQUEST'] = true },
    postData = { ['shortMessage'] = 'test' },
    method = 'POST',
    callback = 'Could be any string or left out completely',
})
The "-d" is missing in postData.
If i try to add it like this:

Code: Select all

postData = {"-d", ['shortMessage'] = 'test' },
This is result:
2021-01-19 14:06:00.077 Status: dzVents: Debug: OpenURL: method = POST
2021-01-19 14:06:00.077 Status: dzVents: Debug: OpenURL: post data = {"shortMessage":"test","1":"-d"}
2021-01-19 14:06:00.077 Status: dzVents: Debug: OpenURL: headers = !#OCS-APIREQUEST: true
2021-01-19 14:06:00.077 Status: dzVents: Debug: OpenURL: callback = Could be any string or left out completely
Thank you for your help.

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 14:18
by waaren
m147 wrote: Tuesday 19 January 2021 14:10 The "-d" is missing in postData.
The -d is no data but a control switch for curl.

Code: Select all

 -d, --data <data>   HTTP POST data
     --data-ascii <data> HTTP POST ASCII data
     --data-binary <data> HTTP POST binary data
     --data-raw <data> HTTP POST data, '@' allowed
     --data-urlencode <data> HTTP POST data url encoded
     --delegation <LEVEL> GSS-API delegation permission
     --digest        Use HTTP Digest Authentication

You don't use it with the openURL switch.

If you use the callback option you will see the result of the call (including errors (if any))

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 14:25
by m147
waaren wrote: Tuesday 19 January 2021 14:18
m147 wrote: Tuesday 19 January 2021 14:10 The "-d" is missing in postData.
The -d is no data but a control switch for curl.

Code: Select all

 -d, --data <data>   HTTP POST data
     --data-ascii <data> HTTP POST ASCII data
     --data-binary <data> HTTP POST binary data
     --data-raw <data> HTTP POST data, '@' allowed
     --data-urlencode <data> HTTP POST data url encoded
     --delegation <LEVEL> GSS-API delegation permission
     --digest        Use HTTP Digest Authentication

You don't use it with the openURL switch.

If you use the callback option you will see the result of the call (including errors (if any))
Of course you are right! Sorry.
But anyway the result is not same as posted curl command >> It doesn't work :(
2021-01-19 14:24:00.343 Status: dzVents: Debug: OpenURL: method = POST
2021-01-19 14:24:00.343 Status: dzVents: Debug: OpenURL: post data = {"shortMessage":"test"}
2021-01-19 14:24:00.343 Status: dzVents: Debug: OpenURL: headers = !#OCS-APIREQUEST: true
2021-01-19 14:24:00.343 Status: dzVents: Debug: OpenURL: callback = Could be any string or left out completely

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 14:31
by waaren
m147 wrote: Tuesday 19 January 2021 14:25 Of course you are right! Sorry.
But anyway the result is not same as posted curl command >> It doesn't work :(
What is you domoticz version?

The Header section does not look right.
Can you send me your script so I can help you adding a response section? It might help in identifying what happens.

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 14:46
by m147
waaren wrote: Tuesday 19 January 2021 14:31
m147 wrote: Tuesday 19 January 2021 14:25 Of course you are right! Sorry.
But anyway the result is not same as posted curl command >> It doesn't work :(
What is you domoticz version?

The Header section does not look right.
Can you send me your script so I can help you adding a response section? It might help in identifying what happens.
Version: 2020.2 (build 12738)

Well the "script" is so far just openUrl function. I wanted to figure it out before I move forward.

Code: Select all

return {
	on = {
--		devices = {},
		timer = {'every minute'},
	},
	logging = {
        level = domoticz.LOG_DEBUG,
    },
	execute = function(domoticz, triggeredItem)
	    
	domoticz.openURL({
        url = 'https://admin:token@url/ocs/v2.php/apps/admin_notifications/api/v1/notifications/user',
        headers = { ['OCS-APIREQUEST'] = 'true' },
        postData = {['shortMessage'] = 'test' },
        method = 'POST',
        })
	end
}

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 15:11
by waaren
m147 wrote: Tuesday 19 January 2021 14:46 Well the "script" is so far just openUrl function. I wanted to figure it out before I move forward.
Below version should give some more info..

Code: Select all

local scriptVar = 'nextCloud'

return
{
    on =
    {
        devices =
        {

        },
        timer =
        {
            'every minute'
        },
        httpResponses =
        {
            scriptVar,
        },
    },


    logging = {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local function sendTo()
            dz.openURL(
            {
                url = 'https://admin:token@url/ocs/v2.php/apps/admin_notifications/api/v1/notifications/user',
                headers = { ['OCS-APIREQUEST'] = 'true' },
                postData = {['shortMessage'] = 'test' },
                method = 'POST',
                callback = scriptVar,
            })
        end

        if item.isTimer or item.isDevice then
            sendTo()
        else
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 16:47
by m147
waaren wrote: Tuesday 19 January 2021 15:11
m147 wrote: Tuesday 19 January 2021 14:46 Well the "script" is so far just openUrl function. I wanted to figure it out before I move forward.
Below version should give some more info..

Here is output:
Spoiler: show
[/code]
2021-01-19 16:38:00.321 Status: dzVents: Info: nextCloud: ------ Start internal script: notifikace:, trigger: "every minute"
2021-01-19 16:38:00.321 Status: dzVents: Debug: nextCloud: OpenURL: url = https://admin:###@###/ocs/v2.php/apps/a ... fications/###
2021-01-19 16:38:00.321 Status: dzVents: Debug: nextCloud: OpenURL: method = POST
2021-01-19 16:38:00.321 Status: dzVents: Debug: nextCloud: OpenURL: post data = {"shortMessage":"test"}
2021-01-19 16:38:00.322 Status: dzVents: Debug: nextCloud: OpenURL: headers = !#OCS-APIREQUEST: true
2021-01-19 16:38:00.322 Status: dzVents: Debug: nextCloud: OpenURL: callback = nextCloud
2021-01-19 16:38:00.322 Status: dzVents: Info: nextCloud: ------ Finished notifikace
2021-01-19 16:38:00.322 Status: EventSystem: Script event triggered: /home/michal/domoticz/dzVents/runtime/dzVents.lua
2021-01-19 16:38:00.445 Status: dzVents: Info: Handling httpResponse-events for: "nextCloud"
2021-01-19 16:38:00.445 Status: dzVents: Info: nextCloud: ------ Start internal script: notifikace: HTTPResponse: "nextCloud"
2021-01-19 16:38:00.447 Status: dzVents: Debug: nextCloud: {["xmlVersion"]="1.0", ["headers"]={["Content-Type"]="application/xml; charset=utf-8", ["Date"]="Tue, 19 Jan 2021 15:38:00 GMT", ["Feature-Policy"]="autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'", ["X-Download-Options"]="noopen", ["X-Permitted-Cross-Domain-Policies"]="none", ["X-Content-Type-Options"]="nosniff", ["X-Robots-Tag"]="none", ["Content-Length"]="170", ["Server"]="Apache/2.4.25 (Debian)", ["Cache-Control"]="no-cache, no-store, must-revalidate", ["X-Frame-Options"]="SAMEORIGIN", ["Expires"]="Thu, 19 Nov 1981 08:52:00 GMT", ["Pragma"]="no-cache", ["Strict-Transport-Security"]="max-age=15552000", ["Content-Security-Policy"]="default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'", ["Referrer-Policy"]="no-referrer", ["X-XSS-Protection"]="1; mode=block"}, ["isDevice"]=false, ["protocol"]="HTTP/1.1", ["_contentType"]="application/xml; charset=utf-8", ["isJSON"]=false, ["isHardware"]=false, ["ok"]=false, ["xml"]={["ocs"]={["data"]={}, ["meta"]={["statuscode"]="997", ["status"]="failure", ["message"]="Current user is not logged in"}}}, ["isVariable"]=false, ["trigger"]="nextCloud", ["isSystem"]=false, ["baseType"]="httpResponse", ["isCustomEvent"]=false, ["isSecurity"]=false, ["callback"]="nextCloud", ["isScene"]=false, ["data"]="<?xml version="1.0"?>
2021-01-19 16:38:00.447 <ocs>
2021-01-19 16:38:00.447 <meta>
2021-01-19 16:38:00.447 <status>failure</status>
2021-01-19 16:38:00.447 <statuscode>997</statuscode>
2021-01-19 16:38:00.447 <message>Current user is not logged in</message>
2021-01-19 16:38:00.447 </meta>
2021-01-19 16:38:00.447 <data/>
2021-01-19 16:38:00.447 </ocs>
2021-01-19 16:38:00.447 ", ["isTimer"]=false, ["dump"]=function, ["isHTTPResponse"]=true, ["isGroup"]=false, ["statusCode"]=401, ["statusText"]="Unauthorized", ["isXML"]=true}
2021-01-19 16:38:00.447 Status: dzVents: Info: nextCloud: ------ Finished notifikace [/code]
Authorization failure... If I use same user and token in curl, it woks. I have also tried token generated especially for this purpose and still same issue :(

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 17:06
by waaren
m147 wrote: Tuesday 19 January 2021 16:47 Authorization failure... If I use same user and token in curl, it woks. I have also tried token generated especially for this purpose and still same issue :(
Could it be something with a string that needs url encoding?


Anyway can you test this one? ( After updating to a later version )

Code: Select all

-- Requires dzVents >= 3.1 (domoticz build >= 12771)

local scriptVar = 'nextCloud'

return
{
    on =
    {
        devices =
        {

        },
        timer =
        {
            'every minute',
        },
        shellCommandResponses =
        {
            scriptVar,
        },
    },


    logging = {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local function sendTo()
            dz.executeShellCommand(
            {
                command = 'curl -H "OCS-APIREQUEST: true" -X POST   https://ADMIN:TOKEN@URL/ocs/v2.php/apps/admin_notifications/api/v1/notifications/USER   -d "shortMessage=test"',
                timeout = 300,
                callback = scriptVar,
            })
        end

        if item.isTimer or item.isDevice then
            sendTo()
        else
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 17:21
by m147
Could it be something with a string that needs url encoding?
I don't really understand.
Anyway can you test this one? ( After updating to a later version )
I am afraid I have to wait until the build issue will be solved.
https://github.com/domoticz/domoticz/issues/4546

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 18:08
by waaren
m147 wrote: Tuesday 19 January 2021 17:21 I don't really understand.
Probably a long shot but the characters '!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]" in a string are not very well interpreted when sending in a url. To convert these chars to something that can be part of a url, the function dz.utils.urlEncode() is available. So if ADMIN:TOKEN does contain any of these chars you could try to urlEncode the string first.
I am afraid I have to wait until the build issue will be solved.
OK. @gizmocuz is working that now so should not take much longer

Re: Push notifications via Nextcloud

Posted: Tuesday 19 January 2021 22:58
by waaren
m147 wrote: Tuesday 19 January 2021 17:21 I am afraid I have to wait until the build issue will be solved.
If you have a windows system in your network you could test this now.
Installing domoticz on windows can be done in a couple of minutes. Only other thing needed would then be curl

Re: Push notifications via Nextcloud

Posted: Wednesday 20 January 2021 15:53
by m147
waaren wrote: Tuesday 19 January 2021 22:58
m147 wrote: Tuesday 19 January 2021 17:21 I am afraid I have to wait until the build issue will be solved.
If you have a windows system in your network you could test this now.
Installing domoticz on windows can be done in a couple of minutes. Only other thing needed would then be curl
Ok I have tried install last beta on windows and it works!
Probably a long shot but the characters '!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]" in a string are not very well interpreted when sending in a url. To convert these chars to something that can be part of a url, the function dz.utils.urlEncode() is available. So if ADMIN:TOKEN does contain any of these chars you could try to urlEncode the string first.
Token contains doesn't contain any of these characters. Only weird character there is "-" .