2016年09月04日

JavaでSlackにMessageを投稿する最小限のコード

メッセージを飛ばすだけの用事で依存ライブラリを増やすのもなんだったので、最小限のメッセージを飛ばすだけのコードを書く。戻り値はレスポンスを文字列にしたもの。

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class SimpleMessageSender {

	private static final String postUrl = "https://slack.com/api/chat.postMessage";

	public static String send(String token, String channel, String userName, String text)
			throws UnsupportedEncodingException, IOException {

		byte[] postData = String.format("token=%1s&channel=%2s&username=%3s&text=%4s", token,
				channel, userName, URLEncoder.encode(text, "utf-8")).getBytes();

		HttpURLConnection conn = (HttpURLConnection) new URL(postUrl).openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
		try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
			wr.write(postData);
		}

		try (Scanner s = new Scanner(conn.getInputStream())) {
			return s.useDelimiter("\\A").hasNext() ? s.useDelimiter("\\A").next() : "";
		}
	}
}