How To Integrate With The Slack Api Using Web Hooks

1 minute read

Slack has made integration extremely easy! This tutorial will show you how to create a Slack app that will send messages to a specific Slack channel.

Pre-requisites

  1. This tutorial assumes you have already setup your Slack workstations and that you have admin privileges.

Setup

  1. Navigate to https://api.slack.com/apps and click on the “Create new App” button.

  2. Choose a name for your new app. and the workstation that you want to associate it with.

  1. Once created click on the “Incoming Webhooks” link under the “Add features and functionality” section of the “Basic Information” page for the newly created app.

  2. Activate the “Incoming Webhooks” functionality, select the channel you wish messages from this app. to be posted to, and then click “Authorize” button.

That’s it!

Now all you need to do is post data to the Webhook URI that was created. Here are a couple of different methods for doing this:

  1. Using Curl:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/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
  1. Using JAVA:
package com.crm.api.slack;

import com.crm.utils.HttpUtils;
import junit.framework.TestCase;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SlackApiManualTest extends TestCase {

  private static final String CHANNEL_URL_MONITOR = "https://hooks.slack.com/services/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";

  public void testSendMessage() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json");
  
    try {
      HttpUtils.post(CHANNEL_URL_MONITOR, "{\"text\":\"A message\"}", headers, HttpUtils.METHOD_POST);
  
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
  
  }
}