Skip to content

Instantly share code, notes, and snippets.

@haraki
Created September 30, 2015 20:57
Show Gist options
  • Save haraki/a16f96a1f858fbd50ae1 to your computer and use it in GitHub Desktop.
Save haraki/a16f96a1f858fbd50ae1 to your computer and use it in GitHub Desktop.
In Android, sample to send a message to Slack.(using "okhttp")
package com.example.mharaki.slack;
import android.os.AsyncTask;
import android.util.Log;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
/**
* Created by mharaki on 2015/09/27.
*/
public class AsyncSlack extends AsyncTask<String, Integer, Integer> {
@Override
protected Integer doInBackground(String... params) {
String message = params[0];
int ret = 0;
Log.d("TestLog", "slack : " + message);
final String BR = System.getProperty("line.separator");
RequestBody requestBody = new FormEncodingBuilder()
.add("token", "") // Your acquired Token.
.add("channel", "") // Channel(ex. #general, @username(Direct Message))
.add("text", message) // Message
.add("username", "test app") // Bot name
.build();
Request request = new Request.Builder()
.url("https://slack.com/api/chat.postMessage")
.post(requestBody)
.build();
OkHttpClient client = new OkHttpClient();
try{
Response response = client.newCall(request).execute();
String body = response.body().string();
Log.d("TestLog", body);
} catch (IOException ex) {
ex.printStackTrace();
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment