How to get actionable notifications using Slack

I tried to install the Slack with your instructions but without success…
Can you give some more detailed instructions or some video tutorial :wink:
Thank you

Do your home assistant logs say anything? Otherwise, I have nowhere to start.

I configure Slack like in your instruction, place first code (rest_command…) in configuration.yaml, script code in script.ymal, !secret slackbot_token in secret.yaml…

But I nothing have of Slack in the log… :confused:

Ok. So let’s go through the components one by one and test them individually. Can you see your REST command as a service in the Dev Services page? Try calling it from there with appropriate parameters and see what happens.

I might be having the same issue as @marijandomi because I also don’t see much in the logs about it and it isn’t working.

I ran this sample curl command, which worked:

curl -X POST -H 'Authorization: Bearer ProperTokenHereBlahBlah' -H 'Content-type: application/json' --data '{"channel":"ProperChannelHereBlahBlah","text":"I hope the tour went well, Mr. Wonka."}' https://slack.com/api/chat.postMessage

I then tried testing the rest_command in the Services tab using the following, but it didn’t work:

rest_command:
  slack_api:
    url: https://slack.com/api/chat.postMessage
    verify_ssl: true
    method: 'post'
    content_type: 'application/json; charset=utf-8'
    headers:
      authorization: !secret slack_oauth_access_token
    payload: '{{ payload }}'
    timeout: 20

What I put in the Service Data area of the Service call to slack_api:

{
  "channel": "ProperChannelHereBlahBlah",
  "text": "This is the notification text"
}

The response from the logs:

2019-11-12 10:44:39 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=rest_command, service=slack_api2, service_data=channel=ProperChannelHereBlahBlah, text=This is the notification text>
2019-11-12 10:44:39 INFO (MainThread) [homeassistant.components.rest_command] Success call https://slack.com/api/chat.postMessage.

Am I missing anything obvious?

Edit: I don’t know how I missed that you tried curl, but I didn’t see it until after I made this post.

I hope I haven’t left something significant out of my steps. I think part of the problem may be that the REST Command doesn’t have great error reporting. Let’s try running this from the command line (I’m assuming you’re on some form of linux):

curl `
   -X POST `
   -H "Content-Type: application/json; charset=utf-8" `
   -H "Authorization: Bearer [slack-api-token]" `
   -d "{\"channel\":\"[channelId]\", \"text\":\"Test notification.\"}" `
   https://slack.com/api/chat.postMessage

Obviously, replace all the fields in square brackets [ ]. Hopefully that either works or gives you a useful error message.

One thing I saw in your REST command is that the “Authorization” header didn’t have the ‘A’ capitalized. I don’t know if it’s case sensitive or not. And since I can’t see your auth token, make sure the header starts with “Bearer”.

So if curl worked, the only difference I can see is the capitalization of “Authorization” in your REST Command. You have it capitalized in your curl command, but not in the REST Command YAML.

Edit: Capitalization is not the problem. tried lowercase in curl and it still works.

Have you left out “Bearer” from the secret that contains your slack token? Part of the problem is that if the call fails, the Slack API will still return success, but the result will contain JSON like this:

{"ok":false,"error":"not_authed"}

You never see that JSON with the REST Command.

I’ve tried both with and without the Bearer and get the same thing. I’ve also tried having “content_type” in the headers but no change.

To confirm, if you try testing it in the Services tab, does it work? And what do you put there?

Yeah, it works for me :expressionless:

My service YAML looks identical to how I showed it in the first post of the thread (including the “api” parameter). The parameters passed to it were the following:

api: chat.postMessage
payload: '{ "channel": "[my channel ID]", "text": "This is a test from dev-services." }'

Aha! I was missing the “payload” part beforehand.

payload: '{ [stuff] }'

Now the rest_command works from the Services tab. I’ll keep testing further.

Yeah, the payload wasn’t meant to be the parameters to the Home Assistant REST Command service. It’s meant to be the literal payload that gets sent to the Slack API.

This was part of the reason why I have the script that calls the REST Command for me.

Glad you got it working.

If it helps, I found and copied a workaround for having a secret in a template. You just need to create an input_text and populate it at startup:

input_text:
  slack_app_verification_token:
    initial: !secret slack_app_verification_token
    mode: password

Then the automation condition you had in your original post would look like this:

      - 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') }}

I’m not sure of the security aspect, but it does work.

2 Likes

I’ve been trying to get this sorted without much luck.
I am getting stuck at the service call bit, I can’t seem to get a message out via hassio.
I have HTML5 and telegram messages working, and use duckdns https and nginx. I just cant seem to get this working which is a shame, because it seems to be the look I want in notifications.
The logs seems to be showing success, and if I try a message via slack test it will send a message to my slack channel.

from the dev services page I have tried various formats, including my channel id directly.
I have tried via nodered service call also and get similar results, e.g. a successful call out in the logs but no actual message in slack.
slack%20snip

2019-11-13 16:32:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1908785648] Received {'type': 'call_service', 'domain': 'rest_command', 'service': 'slack_api', 'service_data': {'api': 'chat.postMessage', 'payload': '{ "channel": "[!secret slack_ch]", "text": "This is a test from dev-services." }'}, 'id': 33}
2019-11-13 16:32:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=rest_command, service=slack_api, service_data=api=chat.postMessage, payload={ "channel": "[!secret slack_ch]", "text": "This is a test from dev-services." }>
2019-11-13 16:32:57 INFO (MainThread) [homeassistant.components.rest_command] Success call https://slack.com/api/chat.postMessage.
2019-11-13 16:32:57 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1908785648] Sending {'id': 33, 'type': 'result', 'success': True, 'result': None}
core-ssh:~#

I have this is my config

rest_command:
  slack_api:
    url: https://slack.com/api/chat.postMessage
    content_type: 'application/json; charset=utf-8'
    verify_ssl: true
    method: 'post'
    timeout: 20
    headers:
      Authorization: !secret slack_bot
    payload: '{{ payload }}'

I have tried it with url: https//slack.com/api/{{ api }}

I cant seem to get any joy.

The first issue I see here is that I don’t think you can use !secret values in the dev service tool. I’m not sure if that was your literal test, or you censored it for posting here, but for your test, try replacing that with the actual value and see if it works.

I don’t think the {{ api }} parameter bit has anything to do with the failure.

If you can get it working with curl like I described in this post, you should definitely be able to get it working using the REST Command service.

Good luck!

Edit: LInked directly to curl example post.

1 Like

Ok I think I got it. I was leaving the bearer out, and the square brackets in I think, definitely didn’t have bearer in my secret anyway. You are right about the !secret in call service too, no can do.

Thanks for your help!

Out of curiosity, any reason you didn’t go with blocks? It seems that is what Slack are moving towards.

HassOS SSH curl syntax for future peoples.

curl \
   -X POST \
   -H "Content-Type: application/json; charset=utf-8" \
   -H "Authorization: Bearer xoxb-789768131872-82fake472725-oc1RQADHSHEHUoNBUEYtLlK" \
   -d "{\"channel\":\"CPfakeTXC\", \"text\":\"Test notification.\"}" \
   https://slack.com/api/chat.postMessage

I hope that’s not your actual slack token.

nah its fake one.

Ahh they are quite similar looking to telegram messages. Did you manage to get them display the buttons when the notifications arrive, as in, so you don’t have to go into the app to see the buttons?

Yeah, they are similar to telegram…that’s what I was using before Slack. It is a bummer that nothing seems to display the actions on the actual notification, but I haven’t found anything that does this.

Still Slack ticked a lot of boxes for me that Telegram didn’t, so I moved over.

Great guide! Going through the REST API directly rather than this https://www.home-assistant.io/integrations/slack/ is much more straightforward and flexible

Thanks for your very detailed guide! I agree with @mot359, definitely very flexible. I took your concept and moved the outbound messaging and inbound actions over to node red for better visibility, debugging and manipulation capabilities. The only thing I noticed was that I needed to specify a name for each attachment action (your example listed the “Leave It” as blank) otherwise it didn’t appear in the message :+1: