Posting Message to Slack via Webhooks Integration

Posted on Friday, March 20, 2015


How can I post to slack via a restful call? 

I wrote up an article on how to have Sensu send messages to HipChat http://www.whiteboardcoder.com/2014/11/getting-sensu-to-talk-to-hipchat.html

I want to figure out how to do this in Slack.




Incoming Webhooks Integration

You can do this by using the Incoming Webhooks Integrations.
This is going to be my first integrations set up in Slack.







From Slack click open team settings








Click on Menu










Click on Integrations









 

Look for the Incoming Webhooks Integration.  Click Add.  (remember you are limited to 5 integrations for the free version of Slack)




 

Select a channel to post to.  (don't worry too much about this you can actually override it later)









Click Add Incoming Webhooks Integration



  

Nice!   They provide some how to information.




Copy the Webhook URL.   For the purposes of this write up I will just list mine as

https://hooks.slack.com/services/XXX

Then run the following curl, as a test (Change the URL to your own and the name to your own)


  > curl -H "Content-type: application/json" -X POST -d '{"text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/XXX





It worked!






Customizing the name/icon


I want to customize the name/icon. How do you do that?

All you need to do is change the payload.  Add username.


{
    "username": "Sensu",
    "text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."
}


Run this curl (replace the URL with your own)



  > curl -H "Content-type: application/json" -X POST -d '{"username": "Sensu", "text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/XXX




That changed the name, now what about the icon?


They have two ways of doing it you can use icon_url or icon_emoji
I am going to use icon_emoji, as I believe I can upload custom emoji's



{
    "username": "Sensu",
    "icon_emoji": ":ghost:",
    "text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."
}





  > curl -H "Content-type: application/json" -X POST -d '{"username": "Sensu", "icon_emoji": ":ghost:",
"text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/XXX




There is my ghost emoji.





Upload a custom Emoji





From your team page click Menu











Click on Customize.









Enter a name, upload a file click Save new Emoji.








Oops my file was too big, it can only be 128x128 pixels











There we go.
Now run this curl



  > curl -H "Content-type: application/json" -X POST -d '{"username": "Sensu", "icon_emoji": ":sensu:",
"text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/XXX





That worked nicely. J






Sending to another room


You can use this one webhook to send message to other rooms.



I created a new room called sensu.  To send messages to this room I need to change the JSON I am sending.


{
    "username": "Sensu",
    "icon_emoji": ":sensu:",
    "channel": "#sensu",
    "text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."
}




Now run this curl



  > curl -H "Content-type: application/json" -X POST -d '{"username": "Sensu", "icon_emoji": ":sensu:", "channel": "#sensu",
"text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/XXX










Sending to a specific user


Now how do I send a message to a specific user?  Just use the channel and send it to @username.



{
    "username": "Sensu",
    "icon_emoji": ":sensu:",
    "channel": "@patrick",
    "text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."
}



Now run this curl



  > curl -H "Content-type: application/json" -X POST -d '{"username": "Sensu", "icon_emoji": ":sensu:", "channel": "@patrick",
"text": "Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/XXX






It became a direct message from the slackbot.







Direct message!  Nice very nice I can use this J






Getting Fancier … Attachments


I was putting using a Pagerduty integration and noticed




This nice little attention grabbing line...  How do I do this?

You can do it, and more with attachments https://api.slack.com/docs/attachments [1]




{
    "username": "Sensu",
    "icon_emoji": ":sensu:",
    "channel": "#sensu",
    "attachments": [
        {
            "color": "danger",
            "fields": [
                {
                    "title": "Event",
                    "value": "This is an emergency",
                    "short": false
                }
            ]
        }
    ]
}



Now run this curl



  > curl -H "Content-type: application/json" -X POST -d '{"username": "Sensu","icon_emoji": ":sensu:","channel": "#sensu","attachments": [{"color": "danger","fields": [{"title": "Event","value": "This is an emergency","short": false}]}]}' https://hooks.slack.com/services/XXX






You can change the color


            "color": "good",








            "color": "warning",





Or you can use HEX colors


            "color": "#FF69B4",





You can have multiple attachments.


{
    "username": "Sensu",
    "icon_emoji": ":sensu:",
    "channel": "#sensu",
    "attachments": [
        {
            "color": "danger",
            "fields": [
                {
                    "title": "Danger Event",
                    "value": "This is an emergency",
                    "short": false
                }
            ]
        },
        {
            "color": "warning",
            "fields": [
                {
                    "title": "Warning Event",
                    "value": "This is a warning",
                    "short": false
                }
            ]
        },
        {
            "color": "good",
            "fields": [
                {
                    "title": "Good Event",
                    "value": "This is good",
                    "short": false
                }
            ]
        }
    ]
}





There is a lot of other cool things you can do with attachments see https://api.slack.com/docs/attachments [1]  for some ideas.

References


[1]        Slack API
            Accessed 3/2015






7 comments:

  1. Great article, I was wondering if you could help me. When I try the simple curl script:

    curl -H "Content-type: application/json" -X POST -d '{"text":"Patrick This is a line of text in a channel.\nAnd this is another line of text."}' https://hooks.slack.com/services/xxx

    I get invalid_payload

    I was hoping you could help.

    ReplyDelete
  2. @Ronny - Did you substitute the URL - hooks.slack.com/services/XXX with your actual incoming-webhook endpoint? Once you integrate the Incoming Webhooks with your specific channel they will display a specific Webhook URL that you send the payload to

    ReplyDelete
    Replies
    1. I see only single URL, hooks.slack.com/services/XXX. I don't see different URL's until and unless I create a different webhooks for each channel. Or update the channel through drop-down list in webhook and not providing any channel while api call.

      Delete
  3. curl -H "Content-type: application/json" -X POST -d '{"username": "xxx", "channel": "#general2",
    "text": "This is test message from cURL"}' https://hooks.slack.com/services/XXXXXX

    I am doing the above curl command , but the message always goes to channel #general instead of #general2 I gave in the json.

    Any suggestions on why?

    ReplyDelete
  4. Is it possible to pass an variable into the attachment ?
    For example instead of this "This is an emergency", I wanted to pass an $log which I wanted to pass from my script.

    I tried below but its not working :
    curl -H "Content-type: application/json" -X POST -d '{"username": "Bot","channel": "@potula","attachments": [{"color": "danger","fields": [{"title": "Event","value": "$log","short": false}]}]}' https://hooks.slack.com/services/xxx/xxx/xxxx

    ReplyDelete