[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
ただいまコメントを受けつけておりません。
Webhock だけでもアレなので Bot に発言をさせてみようとする。Slack Bot の token は次のような形式である。
xoxb-000000000000-000000000000-17afdxySLsIgJkUMu0eJHGMq
Bot は Slack api の Your Apps のページから作る。Add features and functionality から Bots を設定し、 OAuth & Permissions から token を取得する。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class SlackTokenNotificationClient {
private final String token;
private final String userName;
public SlackTokenNotificationClient (String token, String userName) {
this.token = token;
this.userName = userName;
}
public void sendNotify(String message, String channelName) throws IOException {
HttpURLConnection con = null;
try {
String baseUrl = String.format("https://slack.com/api/chat.postMessage?token=%s&channel=%s&text=%s&username=%s",
token,
channelName,
URLEncoder.encode(message.replaceAll(" ", "%20"), "UTF-8").replaceAll("%2520", "%20").replaceAll("%7E", "~"),
URLEncoder.encode(userName.replaceAll(" ", "%20"), "UTF-8").replaceAll("%2520", "%20").replaceAll("%7E", "~")
);
URL url = new URL(baseUrl);
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("GET");
con.getOutputStream();
if(con.getResponseCode() != HttpURLConnection.HTTP_OK) {
StringBuilder sb = new StringBuilder();
try(InputStream is = con.getInputStream()) {
String encoding = con.getContentEncoding();
if( encoding == null ) {
encoding = "UTF-8";
}
try( InputStreamReader isReader = new InputStreamReader(is, encoding);
BufferedReader bufReader = new BufferedReader(isReader); ) {
String line = null;
while((line = bufReader.readLine()) != null) {
sb.append(line);
}
}
}
throw new IOException("Failed to access :" + con.getResponseCode() + " / Result: " + sb.toString());
}
} catch (IOException ioe) {
throw ioe;
}
}
}
SlackTokenNotificationClient client = new SlackTokenNotificationClient (
// 上述の手順で取得した token
"xoxb-000000000000-000000000000-17afdxySLsIgJkUMu0eJHGMq",
// 発言する名前
"My Notification Bot" );
// 第二引数は発言するチャネル
client.sendNotify("発言だよー発言だよー", "notification");
ただいまコメントを受けつけておりません。