slackapi / java-slack-sdk

Slack Developer Kit (including Bolt for Java) for any JVM language
https://slack.dev/java-slack-sdk/
MIT License
571 stars 213 forks source link

bot responding to all threads, I want it to respond only to the thread bot was mentioned #1173

Closed mallik30 closed 1 year ago

mallik30 commented 1 year ago

bot responding to all threads, I want it to respond only to the thread bot was mentioned

Reproducible in:

mvn dependency:tree | grep com.slack.api
gradle dependencies | grep com.slack.api
java -version
sw_vers && uname -v # or `ver`

The Slack SDK version

attached actual GitHub codebase url above

Java Runtime version

java version "13" 2019-09-17

OS info

(Paste the output of sw_vers && uname -v on macOS/Linux or ver on Windows OS) ProductName: macOS ProductVersion: 13.3.1 ProductVersionExtra: (a) BuildVersion: 22E772610a Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64

Steps to reproduce:

(Share the commands to run, source code, and project settings (e.g., pom.xml/build.gradle))

  1. create a thread with the bot mentioned
  2. create a regular thread
  3. please check the screenshots

Expected result:

respond to only Messages in the thread the bot was mentioned @MyBot create

Actual result:

responds to all thread

Requirements

Please make sure if this topic is specific to this SDK. For general questions/issues about Slack API platform or its server-side, could you submit questions at https://my.slack.com/help/requests/new instead. :bow:

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you agree to those rules.

mallik30 commented 1 year ago
1 2
mallik30 commented 1 year ago

below is the sample code that executes for all the threads, I tried to use an event.getBotId() but it is null so I can't avoid the response in regular threads.

public void messageEvent() {
    app.event(MessageEvent.class, (payload, ctx) -> {
        MessageEvent event = payload.getEvent();
        // Listen only thread replies, if ThreadTs is present it is a reply
        if (event.getThreadTs() != null) {
            // my code executes for all thread
        }
        return ctx.ack();
    });
}

https://github.com/mallik30/actionbot/blob/f0efe1788322a58760523a7820bd429ecfa60de2/src/main/java/com/arjun/slack/api/ActionBotManager.java#L37

seratch commented 1 year ago

Hi @mallik30, thanks for asking the question!

As you've already done, checking the existence of thread_ts in a message is the right way to know if the message is posted in a thread. To know whether your app's bot user is mentioned in the thread, the only feasible way is to call conversations.replies API method and find a string part like <@{your bot's user ID here}> in the replies' text. To know your bot's user ID, you can call either auth.test API or bots.info API.

I hope this helps.

mallik30 commented 1 year ago

hi @seratch thank you