Post to Slack

wanted to post my work on how I integrated slack to openhab.

There are a couple options available that I’ll link to for reference… First, and recent, Rino developed an Action for posting to slack. I ended up just using Openhab’s HTTP Post action to hit Slack’s API… this gives me a bit more flexability then the action Rino is developing… but overtime there’s no reason an Action couldn’t do the few tweaks I used.

Rino’s work: New: slack action

Also… there’s a slack bot integration for Openhab on github (https://github.com/openhab/openhab1-addons/wiki/Slack-integration). This is ok… In principal great … in my case though, I don’t know my item’s names off hand. There’s a naming convention, but it’s not something I have memorized.

Ok… so what I did for posting status out from Rules within my system (basically anywhere I had notifications setup, I also setup a slack post):

		val String SlackURL = 'https://hooks.slack.com/services/MYAPIKEYPROVIDEDBYSLACK'
		var String MessagePayload = '{
		    "channel": "#home", 
			"username": "Ashburn Plants", 
			"text": "Plants need water downstairs", 
			"icon_url": "https://openclipart.org/image/2400px/svg_to_png/170344/eco-gsangri4-green-house-2.png"
		}'
		sendHttpPostRequest(SlackURL, "application/x-www-form-urlencoded", MessagePayload)

You can see I can specify the channel, a username (which does not need to be pre-registered with slack… just make them up in Openhab), an Icon image, and then the text… There are a lot more customizations I don’t make use of you can find on slack’s documentation.

This is basically how it shakes out for me:

In this slack channel I also have calendar alerts, notebook updates, bank account alerts, home deliveries … all streamed chronologically in a chat channel that my wife and I share.

Where I’d like to go next is build my slack bot (similar to the github post above), but instead of sending specific commands… I plan to just post the full string similar to a voice command and let my voice command interpretation rule handle it. I’ve already built out all that logic for when I speak commands, might as well let me type it (IE - “close the kitchen blinds” or “turn off the lights”). Shouldn’t be too difficult. I’ll post when I get around to it.

Beyond that, if you look at slack’s documentation, the messages with a bot can also be interactive with options… I think there will be opportunity here for my openhab server to post an alert to the channel with options on what to do with it… which will send back a command to my server. someday…

4 Likes

Moved to Tutorials and Examples.

Nice tutorial. Thanks for posting!

Can you explain more how you have used the state within the code?

val String SlackURL = 'https://hooks.slack.com/services/MYAPIKEYPROVIDEDBYSLACK'
	var String MessagePayload = '{
	    "channel": "#home", 
		"username": "Ashburn Plants", 
		"text": "Plants need water downstairs", 
		"icon_url": "https://openclipart.org/image/2400px/svg_to_png/170344/eco-gsangri4-green-house-2.png"
	}'
	sendHttpPostRequest(SlackURL, "application/x-www-form-urlencoded", MessagePayload)

If you are not familiar to Slack this url may be useful to you