However, I am trying to accomplish this in node-red and I have hit one snag: When clicking an action in Slack I route this to a webhook-node in node-red but the message I receive is really strange encoded, this is the payload being received:
I realize it is JSON encoded some way but also it contains the “payload=” in the beginning.
Any ideas? Is there a setting in Slack that I can look at, is maybe my original Slack message badly formed or should I just accept it and convert it as is?
just strip payload= from the start of the string
and then use a html decode function to convert the html encoded characters back to regular characters to reveal the json string. %7B -> { etc
Is anyone else having issues with the callback after the latest release (2021.4.3)? In particular, it seems like this change may have broken the callback from above: 2021.4: For our advanced users ❤️ - Home Assistant
Previous to April 4th I was seeing the callback message in slack, but now the message is edited in slack to be empty and this error is showing in HA logs for all my callbacks I had setup and working:
Template variable warning: 'message' is undefined when rendering '{ "replace_original": {{ replace_original if (replace_original is defined) else 'true' }}, "text": "{{ message }}", "attachments": {{ attachments if (attachments is defined) else '[]' }} }'
I changed {{ message }} to {{ message | default(’’)}} in chat.postMessage and the slack response, which got rid of the warning about empty message, however, now I’m seeing this:
Error. Url: https://hooks.slack.com/actions/[channel_id]/[remaining_slack_url]. Status code 404. Payload: b'{\n "replace_original": True,\n "text": "",\n "attachments": [{\'pretext\': \'The Garage lights were left on for 10 minutes.\', \'title\': "Handled with \'turn_off\' by <@[slack_bot_id]>", \'color\': \'#03a9f4\'}]\n}
I wonder if it has something to do with the return URL being /actions/ . The channel ID in my URL is the correct one. Also as a side note, I tried switching my automation to use blocks with the Slack integration, but I could only get the basic text area to display. The post seemed to fail if I tried adding in action elements such as buttons. I didn’t have time to do more verbose logging to see what the problem was. I had copied the configuration directly from Slack’s block builder and transformed it to yaml using a json to yaml converter. As soon as I removed the action elements, the text section would display in Slack again.
Set up a callback to perform actions
On https://api.slack.com , when looking at your app, click on “Interactive components” and turn on “Interactivity” . In the “Request URL” box, you’re going to enter the address of a webhook that you’ll create in Home Assistant (something like https://[your domain]/api/webhook/[webhook_id]). When you click one of those buttons in your notifications, Slack will POST a bunch of JSON to that URL (see the bottom of this post for an example of what Slack sends to this webhook). We can set up an automation to deal with that.
I don’t see a Interactive components tab. This is the closes I can find. Is there where I need to create the webhook url?
Ok. That is how I set it up, but it is not working. I am up to this part where I can view the buttons. However, when I press close it, nothing happens and I do not see any errors in log.
Do I need to setup anything else for the webhook in HA? I’ve added in config file this line, but I don’t think it is needed?
webhook:
Now, I am starting to get some error after I added quotes between 'Verification Token'
Errors:
Error while executing automation automation.slack_webhook: Error rendering service name template: UndefinedError: 'trigger' is undefined
7:29:00 PM – Automation (ERROR) - message first occurred at 3:05:48 PM and shows up 3 times
slack_webhook: Error executing script. Error for call_service at pos 1: Error rendering service name template: UndefinedError: 'trigger' is undefined
7:29:00 PM – Automation (ERROR) - message first occurred at 3:05:48 PM and shows up 3 times
cover_left_open_callback: Error executing script. Error for call_service at pos 1: Error rendering data template: JSONDecodeError: Expecting value: line 1 column 1 (char 0)
7:29:00 PM – Script (ERROR) - message first occurred at 3:03:44 PM and shows up 5 times
Error. Url: https://hooks.slack.com/actions/T01U5CCJJDHAA/19720984849124/zwQrAMAd9KDNCD3UG3JKVBVEV. Status code 404. Payload: b'{\n "replace_original": True,\n "text": "",\n "attachments": [{\'pretext\': \'The temperature outside is above 32c\', \'title\': "Handled with \'Close it\' by <@U01UQQG357B>", \'color\': \'#03a9f4\'}]\n}'
7:29:00 PM – RESTful Command (WARNING) - message first occurred at 7:16:05 PM and shows up 2 times
Yeah me and mat @mat1583 have the same issue, see above your initial post. Something changed recently, as in this month, not sure what yet and how to fix.
@duceduc I would remove the webhook: from configuration. It’s not needed. I think you have a couple issues going on - one of which is the webhook automation. See my comments below. The other issue is the same one @tinuva and I are having, which is the response_url problem after the button is clicked. When I click one of the buttons, HA is correctly handling the click and turning off/closing the entity, but when it tries to respond back to Slack to update the message, I get that 404 that you’re seeing. This was not the case before around 4/4. I would correctly see it change the message.
Here’s my current setup for the webhook automation. Note that I used the input_text idea from another commenter so I could throw my verification token into secrets:
alias: Slack Webhook for handling Slack interactions
description: 'Webhook for Slack -> Home Assistant. DO NOT DELETE OR EDIT
'
trigger:
- platform: webhook
webhook_id: <my_slack_webhook_id>
condition:
- condition: template
value_template: >
{% set payload = trigger.data.payload | from_json %}
{{ (payload.token == states('input_text.slack_app_verification_token')) and (payload.type == 'interactive_message') }}
action:
- service_template: >
{%- set payload = trigger.data.payload | from_json -%}
script.{{ payload.callback_id }}
data_template:
payload: '{{ trigger.data.payload }}'
mode: parallel
I figured it out! What I did to troubleshoot was post the message from postman to the URL provided with the exact payload. The 404 respond was actually “invalid_payload”. The invalid part is this:
“replace_original”: True
capitalized, it won’t work. It should be all lowercase. I’m not sure what caused the sudden change from true to True, but for now I changed the slack response to just be replace_original: ‘true’ instead of the check for defined. I’ll look into it further when I have the time.
Surround the replace_original value in single quotes like this:
{{ replace_original if (replace_original is defined) else ‘true’ }}
I think what was happening is that it was being translated to a python boolean which is either True or False.
Edit: I spoke too soon about putting single quotes around the replace original. I don’t think I had re-loaded everything before testing. It still returns True in the payload, so for now I’ve just hardcoded the response to set replace_original to true.
ok. For now, I just changed it to lower case true and I can see the reply.
"replace_original": true,
However, the undefineError: trigger is still there. The automation isn’t triggering.
error:
Error while executing automation automation.slack_webhook: Error rendering service name template: UndefinedError: 'trigger' is undefined
7:29:00 PM – Automation (ERROR) - message first occurred at 3:05:48 PM and shows up 3 times
slack_webhook: Error executing script. Error for call_service at pos 1: Error rendering service name template: UndefinedError: 'trigger' is undefined
7:29:00 PM – Automation (ERROR) - message first occurred at 3:05:48 PM and shows up 3 times
cover_left_open_callback: Error executing script. Error for call_service at pos 1: Error rendering data template: JSONDecodeError: Expecting value: line 1 column 1 (char 0)
7:29:00 PM – Script (ERROR) - message first occurred at 3:03:44 PM and shows up 5 times
More troubleshooting:
I think I have found the cause, but am not sure where to look to fix it. It is in this script, entity_id. If I just add the actual entity id, which is cover.living_room_curtains, the curtain closes when I click on the Close it button in slack.
The only place that has this entity id is this automation, which I have stated the entity id.
- alias: Temperature above 32
trigger:
platform: state
entity_id: input_boolean.guest_mode
to: 'on'
action:
- service: script.notify_slack
data_template:
channel: !secret slack_channel_id
message: "The temperature outside is above 32c"
attachments:
- title: Turn on the Living Room AC?
callback_id: cover_left_open_callback
color: '#03a9f4'
attachment_type: default
actions:
- text: Close it
value: Close it
name: 'cover.living_room_curtains'
type: button
- text: Leave it
value: Leave it
name: 'anything_just_not_blank'
type: button
I’m trying to get actionable notifications working for Slack as well, but I cannot get it sorted.
For me the RESTful command does not result in a message being posted in Slack.
The Slack notification service works for me, so I have the Slack app and API token set up correctly.
For getting the channel ID I went to the slack browser app and went to my message channel.
The URL has the following format: https://app.slack.com/client/T12345ABCDE/C12345ABCDE/C12345ABCDE
So I figured C12345ABCDE is the channel ID.
Calling the following service however result in no success:
service: rest_command.slack_api
data:
api: chat.postMessage
payload:
channel: C12345ABCDE
text: "My amazing test message"
All seems to be OK except that no message is posted.
Has anything changed recently causing this method to break?
Any other clues of what I could have done wrong?
Having an issue with the slack_webhook automation, I can see that when I press the button, the webhook has been received, but the condition and action are not triggering.
In the original, the action was - service_template: which has been deprecated, when googled, u can now run it under - service: instead, but I’m not sure if that’s whats causing the automation to not run.
service: script.notify_slack
data_template:
channel: "C041HSRR887"
message: "The Virtual switch is on."
attachments:
- title: Turn off the switch?
callback_id: webhook_id1
color: '#03a9f4'
attachment_type: default
actions:
- text: Close it
value: Close it
name: 'switch.virtual_switch_1'
type: button
- text: Leave it
value: Leave it
name: 'anything_just_not_blank'
type: button
Edit 2: So i have tried the og with cover entities instead and that seems to work, is there a way to make it work with different entity type like switch. instead of only cover.? (managed to find out, just had to change the last service part of the script.)