Skip to content
Advertisement

Slack Events are not being triggered for private channels

I am working on a slack bot that tracks team activity on a workspace. The bot is added as an integration to multiple channels, all but one of them are private. Whenever, a message is sent to the public channel, the callback function for handling messages is invoked but the function doesn’t get invoked when a message is sent to a private channel.

Here is my main method.

public static void main(String[] args) throws Exception {

    init();
    app.message(Pattern.compile(".*", Pattern.DOTALL), (payload, ctx) -> onMessage(payload,ctx));
    
    try {
        log.info("Server started");
        System.out.println("Server started");
        socketModeApp.start();
    }
    catch(Exception e){
        log.error(e.toString());
    }
}

This is the onMessage method.

static Response onMessage(EventsApiPayload<MessageEvent> payload, EventContext ctx) {
    app.executorService().execute(() -> {

        try {
            MessageEvent event = payload.getEvent();
            String text = event.getText();
            
            String userid = event.getUser();

            String channelId = event.getChannel();
            String ts = event.getTs().substring(0,10);
            
            log.info(userid+" sent a message on "+channelId);
            
            // code to track user activity.
        
        }
        catch(Exception e) {
            log.error(e.toString());
        }

    });
    return ctx.ack();
}

So, when a message is sent to a public channel an entry is created in the log file but the same doesn’t happen when it is sent in the private channel. Even though the bot is added to both.

Answer

So, I figured out what was going wrong. I had given OAuth scopes to the bot to read messages from private channels under OAuth & Permissions tab in the Slack API dashboard.

I also needed to add the groups:history permissions to Subscribe To Bot Events dropdown under the Event Subscriptions tab. Once you do that, event will be triggered for the messages that are sent in private channels.

Advertisement