2017-12-28 19 views
2

私はJavaでSlackのAPIを使用しています。私は、着信Webhooksを使用してメッセージを送信する簡単なコードを作成する方法をすでに発見しましたが、今はgetChannels関数で使用可能なチャネルのリストを受け取ることに興味があります。Slack APIからチャンネルリストを取得するには?

問題は、これについてJavaで例が見つからないということです。

さて、私のコードは次のとおりです。

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

が、結果は 'javaNullPointException' でした:

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

は私がよ、このてみてください。トークンをStringにする必要がありますか?

+0

を 'プライベート文字列トークン_ = "{} myToken" 何の目的;'サーブは? –

+0

いいえ、それが必要な場合のみ後で – user5872256

答えて

1

Slackの着信webhooksはこの機能を提供できません。必要なものを得るにはSlackのWeb APIを使用する必要があります。ウェブAPIを使用して

try following this example使用しているjslackライブラリから:

List<String> channels = slack.methods().channelsList(ChannelsListRequest.builder().token(token).build()) 
     .getChannels().stream()  
     .map(c -> c.getId()).collect(toList()); 
+0

私は試してみましたが、うまくいきません。投稿 – user5872256

+0

を編集すると、 'javaNullPointException'が返されます。問題はトークンである可能性がありますか? – user5872256

+0

可能性があります - 私の答えでコードを使用するには、APIトークン(webhookとは別)を生成する必要があります –

関連する問題