Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to make a slackbot only respond to DM with itself through the app.message method? #2323

Open
4 of 10 tasks
lincoln19922 opened this issue Nov 6, 2024 · 3 comments
Open
4 of 10 tasks
Labels
question M-T: User needs support to use the project

Comments

@lincoln19922
Copy link

lincoln19922 commented Nov 6, 2024

Description

I'm having a problem and I don't know how to deal with it.

I have several bots connected to my Slack that perform different actions. However, what these bots have in common is that people should be able to send messages to them via DM (Open a conversation with the bot), and through these messages each bot should be able to perform something.

Today, to read these messages, I have the following code:

app.message('', async ({ message, say, body }) => {
  if((message.channel_type === "im") && (message.channel.startsWith('D'))){
    const channelInfo = await slack.getChannelData(app, message.channel)
    const userInfo  = await slack.getUserData(app, message.user)

    if ((typeof channelInfo != 'undefined') && (channelInfo !== null)) {
      if((channelInfo.channel.is_im == true) && (channelInfo.channel.num_members == 2) && (channelInfo.channel.is_open == false)){
            await say({ text: "resposta", thread_ts: message.ts});
      }
      else {
          await say({ text: "Sem Resposta", thread_ts: message.ts});
      }
    }
  }
});

The problem I'm facing is that many times the message sent to Bot A is received by Bot B, the message sent to Bot C is received by Bot A, and this way the bot is not able to respond to the message.

My question is, how can I make a bot listen to events of messages received only from its DM, without having to use any words or anything like that. Do you have any ideas?

Reproducible in:
@slack/bolt": "^3.22.0",
node version: v20.13.1

OS version(s): MACOS 14.6.1 (23G93)

Thank you!

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.
@seratch seratch added question M-T: User needs support to use the project needs info An issue that is claimed to be a bug and hasn't been reproduced, or otherwise needs more info and removed untriaged labels Nov 6, 2024
@seratch
Copy link
Member

seratch commented Nov 6, 2024

Hi @lincoln19922, thanks for asking the question.

The problem I'm facing is that many times the message sent to Bot A is received by Bot B, the message sent to Bot C is received by Bot A, and this way the bot is not able to respond to the message.

This situation usually doesn't occur, so it might be something specific to your app.

If you're managing a few apps and their bot users communicate with one another (meaning the DMs you mentioned are 1:1 DMs among the bots, with no human interactions), it's possible that both Bot A and Bot B could receive 1:1 DM message events. In this scenario, there's no way to ensure that only one of them (for example, only Bot B) receives the message.

If this isn't the case, please provide more details about your apps and how they are operated.

@lincoln19922
Copy link
Author

lincoln19922 commented Nov 6, 2024

Hi @seratch , thanks for your answer

This is not the case =/

The communications are between a regular user and a bot, but another bot is also able to receive them.

This problem may occur due to some access that my bots have that they shouldn't (To test some features I granted excessive permissions). See the permissions section of my app manifest below.

        "scopes": {
            "user": [
                "admin.teams:read",
                "admin.users:read",
                "im:history",
                "reactions:read",
                "team.billing:read",
                "team.preferences:read",
                "team:read",
                "usergroups:read",
                "users.profile:read",
                "users:read",
                "users:read.email",
                "chat:write",
                "mpim:read",
                "im:read",
                "im:write",
                "channels:history",
                "mpim:history"
            ],
            "bot": [
                "app_mentions:read",
                "bookmarks:write",
                "calls:read",
                "calls:write",
                "channels:history",
                "channels:join",
                "channels:manage",
                "channels:read",
                "chat:write",
                "chat:write.customize",
                "chat:write.public",
                "files:read",
                "files:write",
                "groups:history",
                "groups:read",
                "groups:write",
                "im:history",
                "im:read",
                "im:write",
                "incoming-webhook",
                "links:read",
                "links:write",
                "mpim:history",
                "mpim:read",
                "mpim:write",
                "pins:read",
                "pins:write",
                "reactions:read",
                "reactions:write",
                "reminders:read",
                "reminders:write",
                "team.billing:read",
                "team:read",
                "usergroups:read",
                "usergroups:write",
                "users.profile:read",
                "users:read",
                "users:read.email",
                "users:write",
                "workflow.steps:execute",
                "conversations.connect:read",
                "commands"
            ]
        }
    },
    "settings": {
        "event_subscriptions": {
            "user_events": [
                "message.channels",
                "message.im",
                "message.mpim"
            ],
            "bot_events": [
                "app_home_opened",
                "app_mention",
                "message.groups",
                "message.im"
            ]
        },

Regarding the applications, each one has its own code and its own bolt object, but both run on the same VM instance. The code to capture the message from both bots is exactly the same as the one I published in the first message.

@seratch
Copy link
Member

seratch commented Nov 6, 2024

@lincoln19922 OK, I figured out the cause. The "message.im" event in "settings.event_subscriptions.user_events" is likely the reason for your situation. Please remove the event subscription from all of your apps. Subscribing to user events means that those apps receive message events for your human account (because you've granted the apps to subscribe to your own message events). This is why you notice that a few apps receive the same message events, even when some of them are not involved in the DM with you.

@seratch seratch removed the needs info An issue that is claimed to be a bug and hasn't been reproduced, or otherwise needs more info label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

2 participants