Reputation: 69
I'm using Slack's API with Java. I have already discovered how to create a simple code to send messages using incoming Webhooks, but now I am interested in receiving a list of the available channels in the getChannels function.
The problem is that I do not find examples in Java about this.
Now, my code is:
package slack;
import com.github.seratch.jslack.Slack;
import java.io.IOException;
import com.github.seratch.jslack.api.methods.SlackApiException;
import com.github.seratch.jslack.api.webhook.*;
public class SlackManager {
private String token_="{myToken}";
private Slack slack_ = Slack.getInstance();
private String url_="{url}";
public void sendMessage(String text, String channel, String name) throws IOException, SlackApiException {
Payload payload = Payload.builder()
.channel("#"+channel)
.username(name)
.iconEmoji(":smile_cat:")
.text(text)
.build();
WebhookResponse response = slack_.send(url_, payload);
System.out.println(response.getMessage().toString());
}
public void getChannels(){
//I don't know how to get the channel list!!!
}
}
I'm try this:
public void getChannels() throws IOException, SlackApiException{
List<String> channels = slack_.methods().channelsList(ChannelsListRequest.builder().token(token_).build())
.getChannels().stream()
.map(c -> c.getId()).collect(Collectors.toList());
for (String string : channels) {
System.out.println(string);
}
}
but the result was a 'javaNullPointException'. Must the token be String?
Upvotes: 3
Views: 1178
Reputation: 16778
Slack's incoming webhooks won't be able to provide this functionality - you'll need to use Slack's Web API to get what you need.
Using the Web API, try following this example from the jslack
library you're using:
List<String> channels = slack.methods().channelsList(ChannelsListRequest.builder().token(token).build())
.getChannels().stream()
.map(c -> c.getId()).collect(toList());
Upvotes: 1