Port of allbegray's slack-api. Used in the production app Amp.
https://jitpack.io/#pschroen/slack-api-android
Add it to your build.gradle with:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
and:
dependencies {
compile 'com.github.pschroen:slack-api-android:c66cc8d997'
}
- Compiled with Java 7.
- Commons Logging replaced with Android Logging.
org.apache.http
classes replaced with HttpClient for Android maintained by Marek Sebera.org.asynchttpclient
classes replaced with OkHttp Web Sockets and Socket.IO's WebSocket.java as a reference.
final String slackToken = mSharedPref.getString("slack_token", "");
mWebApiClient = SlackClientFactory.createWebApiClient(slackToken);
String webSocketUrl = mWebApiClient.startRealTimeMessagingApi().findPath("url").asText();
mRtmClient = new SlackRealTimeMessagingClient(webSocketUrl);
mRtmClient.addListener(Event.HELLO, new EventListener() {
@Override
public void onMessage(JsonNode message) {
Authentication authentication = mWebApiClient.auth();
mBotId = authentication.getUser_id();
System.out.println("User id: " + mBotId);
System.out.println("Team name: " + authentication.getTeam());
System.out.println("User name: " + authentication.getUser());
}
});
mRtmClient.addListener(Event.MESSAGE, new EventListener() {
@Override
public void onMessage(JsonNode message) {
String channelId = message.findPath("channel").asText();
String userId = message.findPath("user").asText();
String text = message.findPath("text").asText();
if (userId != null && !userId.equals(mBotId)) {
Channel channel;
try {
channel = mWebApiClient.getChannelInfo(channelId);
} catch (SlackResponseErrorException e) {
channel = null;
}
User user = mWebApiClient.getUserInfo(userId);
String userName = user.getName();
System.out.println("Channel id: " + channelId);
System.out.println("Channel name: " + (channel != null ? "#" + channel.getName() : "DM"));
System.out.println("User id: " + userId);
System.out.println("User name: " + userName);
System.out.println("Text: " + text);
// Copy cat
mWebApiClient.meMessage(channelId, userName + ": " + text);
}
}
});
mRtmClient.connect();
If you need to catch the close and failure events you can add listeners:
mRtmClient.addCloseListener(new CloseListener() {
@Override
public void onClose() {
System.out.println("Connection closed");
}
});
mRtmClient.addFailureListener(new FailureListener() {
@Override
public void onFailure(Throwable t) {
Exception e = (Exception) t;
System.out.println("Failure message: " + e.getMessage());
}
});
-dontwarn allbegray.slack.**
-keep class allbegray.slack.** {*;}
-dontwarn com.fasterxml.**
-keep class com.fasterxml.** {*;}
-dontwarn okio.**
-keep class okio.** {*;}