Need help to get DZVents posting to Slack working.
Posted: Thursday 04 June 2020 0:21
Hi guys,
I'm really breaking my head why the following script does not work. This is to post a message to Slack using the Bot-API.
I have a similar script working with the webhook API, but that only works on a single channel, whereas I would like to post to different channels. Therefore I would like to get this working.
The problem is also that I do not get proper error logging as well (even with the level set to Debug in settings), the log-line after the "if (item.isHTTPResponse) then" does not seem to get hit?
However If I move that part to separate script, I can catch the HTTPResponse, which shows that Slack says ""invalid_form_data".
If I change the header info to say that the content-type is "application/x-www-form-urlencoded" I get an error "not_authed"
Can anybody see what is wrong in this script?
I'm really breaking my head why the following script does not work. This is to post a message to Slack using the Bot-API.
I have a similar script working with the webhook API, but that only works on a single channel, whereas I would like to post to different channels. Therefore I would like to get this working.
The problem is also that I do not get proper error logging as well (even with the level set to Debug in settings), the log-line after the "if (item.isHTTPResponse) then" does not seem to get hit?
However If I move that part to separate script, I can catch the HTTPResponse, which shows that Slack says ""invalid_form_data".
If I change the header info to say that the content-type is "application/x-www-form-urlencoded" I get an error "not_authed"
Can anybody see what is wrong in this script?
Code: Select all
return {
on = {
devices = {'TestContact' }
},
httpResponses = {
'slackrequest'
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "Slack-script"
},
execute = function(domoticz, item)
local function SendSlackMessage (message)
local token = 'xoxb-my-bot-token'
local channel = 'sensors'
domoticz.openURL({
url = 'https://slack.com/api/chat.postMessage',
method = 'POST',
headers = { ['content-Type'] = 'application/json' },
postData = { ['text'] = message , ['token'] = token, ['channel'] = channel },
callback = 'slackrequest',
})
end
if (item.isDevice) then
local results = SendSlackMessage ('This is a test message')
end
if (item.isHTTPResponse) then
domoticz.log('OK we got a response',domoticz.LOG_DEBUG)
if (item.ok) then
domoticz.log('Ok result. Slack response is: Ok',domoticz.LOG_DEBUG)
else
domoticz.log('Slack response is: Not OK', domoticz.LOG_DEBUG)
end
end
end
}