Slack: Calling WebHooks from Java

Setup Instructions

We’ll guide you through the steps necessary to configure an Incoming Webhook so you can start sending data to Slack.


Webhook URL

https://hooks.slack.com/services/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

Sending Messages

You have two options for sending data to the Webhook URL above:

  • Send a JSON string as the payload parameter in a POST request
  • Send a JSON string as the body of a POST request

For a simple message, your JSON payload could contain a text property at minimum. This is the text that will be posted to the channel.
A simple example:

payload={"text": "This is a line of text in a channel.\nAnd this is another line of text."}

This will be displayed in the channel as:


Adding links

To create a link in your text, enclose the URL in <> angle brackets. For example: payload={"text": "<https://slack.com>"} will post a clickable link to https://slack.com. To display hyperlinked text instead of the actual URL, use the pipe character, as shown in this example:

payload={"text": "A very important thing has occurred! <https://alert-system.com/alerts/1234|Click here> for details!"}

This will be displayed in the channel as:


Customized Appearance

You can customize the name and icon of your Incoming Webhook in the Integration Settings section below.
However, you can override the displayed name by sending "username": "new-bot-name" in your JSON payload. You can also override the bot icon either with "icon_url": "https://slack.com/img/icons/app-57.png" or "icon_emoji": ":ghost:".

Channel Override

Incoming webhooks have a default channel, but it can be overridden in your JSON payload. A public channel can be specified with "channel": "#other-channel", and a Direct Message with "channel": "@username".

Example #1

Putting this all together, here is a sample curl request for posting to a channel.

curl -X POST --data-urlencode 'payload={"channel": "#vship", "username": "webhookbot", "text": "This is posted to #vship and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/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

This will be displayed in the channel as:

Example #2

Using AngularJS to call your WebHook URL as a RESTful API

Message Attachments

Learn how to send richly-formatted messages to your Incoming Webhook.

Attachment Formatting

To display a richly-formatted message attachment in Slack, you can use the same JSON payload as above, but add in an attachments array. Each element of this array is a hash containing the following parameters:

{
	"fallback": "Required text summary of the attachment that is shown by clients that understand attachments but choose not to show them.",

	"text": "Optional text that should appear within the attachment",
	"pretext": "Optional text that should appear above the formatted data",

	"color": "#36a64f", // Can either be one of 'good', 'warning', 'danger', or any hex color code

	// Fields are displayed in a table on the message
	"fields": [
		{
			"title": "Required Field Title", // The title may not contain markup and will be escaped for you
			"value": "Text value of the field. May contain standard message markup and must be escaped as normal. May be multi-line.",
			"short": false // Optional flag indicating whether the `value` is short enough to be displayed side-by-side with other values
		}
	]
}

Please note that the fallback field is required, and is displayed whenever message attachments cannot be shown (ie. mobile notifications, desktop notifications, IRC).


Example

This example will post a detailed message attachment as though it were sent from a task management service:

{
   "attachments":[
      {
         "fallback":"New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",
         "pretext":"New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",
         "color":"#D00000",
         "fields":[
            {
               "title":"Notes",
               "value":"This is much easier than I thought it would be.",
               "short":false
            }
         ]
      }
   ]
}

This will be displayed in the channel as:

Notifications from StatusCake to Slack

You can now set-up StatusCake to send notifications to one or more of your Slack Channels. To get started is easy – follow these easy steps:

Inside Slack:

  • Go to “Your Account”
  • On the new top menu select “Integrations”
  • Scroll down and find “Incoming Web-hooks”
  • Add a new incoming web hook / select the channel.
  • Copy the URL with the Token in

Inside StatusCake:

  • Go to ‘Account’ then ‘3rd Party’
  • In add New Service selection, select ‘Slack’
  • Insert your Token URL, and optionally type a channel name to send slack to
  • Save and then add this 3rd party service to any contact group of your choice

StatusCake Key Stats to Slack

We have also added the ability to query your StatusCake accounts key statistics right from inside Slack. No need to go to StatusCake every time you want to see what sites (if any) are down from your account. To get started with this –

Inside StatusCake:

  • Go to ‘Account’ then ‘3rd Party’
  • Scroll down and in the Pull Service URLs copy the StatusCake URL for Slack

Inside Slack:

  • Go to “Your Account”
  • On the new top menu select “Integrations”
  • Scroll down and find “Outgoing Web-hooks”
  • Add New Outgoing Web-hook
  • Optionally pick channel (or use “All”)
  • Pick your Twitter Word(s), We’d suggest something like “StatusCake”
  • Put the URL you copied from StatusCake in the URL(s) box

Now when you type “StatusCake” (or whatever trigger word you used) in a channel that you have set-up the hook to act on, you’ll get key statistics!