app_mention not firing in private message - slack

I have a bot with event scopes app_mention and messages.im. It also has OAuth scopes app_mentions.read, channels.history, chat.write, groups.history, and im.history.
If I # mention my bot in a public channel, a thread off of a public channel, or directly message my bot I get an event. But if I'm in a private message with someone else and # mention my bot, I don't get any events from the #mention.
What is the correct event scope to enable to get the event?

you can't unfortuantly. app_mention only works in conversations the bot would have access to. You would need each user to directly give you access to im history and monitor every message event for a mention of the bot itself (probably either some regex or similar to look for the <#bot_name>.
I know there was a link_names part of the json object being sent for message events but you'd still need to check the message body to make sure its your bot being mentioned and not another user

Related

Slack Events API - Only able to subscribe to messages posted in public channels?

I just finished integrating a Slack app into my Slack workspace, and now I'm trying to simply subscribe to events so that I can respond to certain types of messages. However, I noticed that the POST request from the Slack Event Subscription process is only occurring when a message in a public channel is being posted, not private channels.
The bot user is in both the public and the private channel, so I'm not quite sure why this is happening. The only thing that I can see in the documentation (https://api.slack.com/apis/connections/events-api) is this:
Permission model
The Events API leverages Slack's existing object-driven OAuth scope
system to control access to events. For example, if your app has
access to files through the files:read scope, you can choose to
subscribe to any or none of the file-related events like file_created
and file_deleted.
You will only receive events that users who've authorized your app can
"see" on their workspace (that is, if a user authorizes access to
private channel history, you'll only see the activity in private
channels they are a member of, not all private channels across the
workspace).
Bot users may also subscribe to events on their own behalf. The bot
scope requested when workspaces install your bot covers events access
for both the Events API and the Real Time Messaging API.
but this still doesn't clarify why it's not subscribing to private channels, only public channels.
Assuming you are subscribed to 'Message' event.
To receive 'message' event from different conversation types, you need different scopes.
https://api.slack.com/events/message
Required scopes : channels:history, groups:history, im:history, mpim:history
Scope required for Private channel is groups:history

Slack event message.im not being triggered

I'm trying to get the event to be triggered when someone sends a message on a private conversation in slack, I've already registered the event subscription (message.im) in the subscribe to bot events section, made sure that the needed scope was added (im:history), and reinstalled the app on my workspace.
Also tried signing out of the workspace and logging in again.
Yet, even if it is the admin sending a message to a user or a user sending a message to the admin (or in any other case with other users) the Event is not being triggered.
Any help or suggestion of why could this be happening?
Thanks!
You receive this event when a user sends a direct message to your bot/app.
You wouldn't receive this event when a user sends a message to another user, because the bot is not a part of that conversation.

Slack App API - Can I subscribe to and monitor all #username mentions for the current user?

When someone mentions a specific username by their handle in Slack, the user typically gets notified about that mention by Slack. I would like to make a Slack app that subscribes to those notifications for the current user. Any time a user gets notified that they were directly mentioned, I'd like to be informed of that in my app, along with the contents of that message.
I've looked through the API docs for quite a while and I can't seem to find support for this functionality. Am I missing something? Can this be done?
There is no event in the Slack Event API that handles mentions of users (although if you create a slack bot you can handle mentions of the slack bot's user with the app_mention event).
You can still listen for the message event and manually look through the text for any #UserMentions and handle them there. Note that this will only work for channels that the Slackbot has been granted access to.

How to make Slack Bot not respond to all the messages in the private group?

I want my bot to respond only if anyone mentions its name like #mybot.
How to achieve this?
For that you need to use the Events API and subscribe to the event app_metion.
That way your app will receive all message that directly mention your bot with #mybot.
Those messages will be sent as request from Slack to the endpoint provided for receiving events.

How do I receive only the messages that are direct messages to my bot user?

I have successfully setup my slack bot app, have enabled events etc. I want to receive any direct messages that the members of my slack team send to my bot. For this, I have only enabled Bot Events and No Team Events like below
However, I do not get any event on my webhook on this setting.
If I enable message.im event under Team Events, then I start getting events. But then, I get every message that the user (who has installed the app) sends to any other user in the team.
How do I get only the messages that are sent to my bot user?
Update 1
Based on this SO question I created a private channel with the bot user. Even then, the messages in the private channel do not arrive in my webhook with the above event subscriptions.
Update 2
After reinstalling the app, it seems to be working fine. I have only used it for few minutes after the reinstall so far. I will keep posting here how it goes. It would still be interesting to know what went wrong where.
It is normal behavior that your script receives all messages (for every channel your bot is a member of), not only the messages sent directly to your bot. At this time you can not filter further in the event configuration. So you need to filter out any unwanted message in your script handling the event.
See also this answer.

Resources