Android client for the Slack Web API, Incoming Webhooks, Slackbot Remote Control and RTM (Real Time Messaging) API
Switch branches/tags
Nothing to show
Clone or download
Latest commit ef50436 Jan 15, 2017

README.md

slack-api-android Release Build Status

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'
}

Differences

Usage

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());
    }
});

ProGuard

-dontwarn allbegray.slack.**
-keep class allbegray.slack.** {*;}
-dontwarn com.fasterxml.**
-keep class com.fasterxml.** {*;}
-dontwarn okio.**
-keep class okio.** {*;}