Page 1 of 1

Notification via nextcloud

Posted: Friday 22 January 2021 14:16
by m147
Hello,
Based on this topic viewtopic.php?f=31&t=35252 I have made simple script to use Nextcloud as notification server.
Thx again @waaren ;)

Code: Select all

return
{
    on =
    {
        customEvents = { "notification" },
        
   
    },

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

    execute = function(dz, item)
        
        local function notify()
            local ADMIN = "adminuser"
            local TOKEN = "secret-token"
            local USER = "targetuser"
            local MESSAGE = item.data
            dz.executeShellCommand(
            {
                command = 'curl -H "OCS-APIREQUEST: true" -X POST   https://'..ADMIN..':'..TOKEN..'@redandblack.cz/ocs/v2.php/apps/admin_notifications/api/v1/notifications/'..USER..'   -d "shortMessage='..MESSAGE..'"' ,
                timeout = 300,
            })
        end

        if item.isTimer or item.isDevice or item.customEvent then
            notify()
        else
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}
You can activate notification for example by MQTT:

Code: Select all

mosquitto_pub -h domoticz -t domoticz/in -m '{"command":"customevent", "event":"notification","data":"message"}'
Or another dzVents script where will be executed:

Code: Select all

domoticz.emitEvent('notification','message"')
I believe there are many users who uses Domoticz and also Nextcloud, so there will be no need to use 3rd party notification services like Pushbullet for them, since you can install Nextcloud app on any device.

Re: Notification via nextcloud

Posted: Friday 22 January 2021 15:25
by waaren
m147 wrote: Friday 22 January 2021 14:16 I believe there are many users who uses Domoticz and also Nextcloud, so there will be no need to use 3rd party notification services like Pushbullet for them, since you can install Nextcloud app on any device.
I added some checks and log messages to get the result visible

Code: Select all

scriptVar = 'nextcloudPost'
return
{
    on =
    {
         customEvents =
        {
            scriptVar,
        },

        shellCommandResponses =
        {
            scriptVar,
        }
    },

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

    execute = function(dz, item)

        local function notify()
            local ADMIN = "adminuser"
            local TOKEN = "secret-token"
            local USER = "targetuser"
            local MESSAGE = item.data or ''
            dz.executeShellCommand(
            {
                command = 'curl -H "OCS-APIREQUEST: true" -X POST   https://'..ADMIN..':'..TOKEN..'@redandblack.cz/ocs/v2.php/apps/admin_notifications/api/v1/notifications/'..USER..'   -d "shortMessage='..MESSAGE..'"' ,
                timeout = 300,
                callback = scriptVar,
            })
        end

        if item.isTimer or item.isDevice or item.isCustomEvent then
            notify()
        elseif item.statusCode ~= 0 then
            dz.log('Problem occurred with curl command. Message is ' .. item.errorText ..'. Returncode is ' .. item.statusCode, dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        elseif item.isXML then
            dz.utils.dumpTable(item.xml)
        end

    end
}