Slack `member_joined_channel` - how to check if my bot was invited?

Viewed 50

I want to send a message to any public channel that my bot gets added to. I notice there's the member_joined_channel event as documentated here, however I'm not sure how to figure out when to determine if my bot was the invited member. I know there's the user property, but I don't want to hardcode my bot's user ID.

2 Answers

I'm not familiar with the slack API, but i found this endpoint : https://api.slack.com/types/conversation

In the response, you have a flag is_member which contains what you need I think :

is_member indicates whether the user, bot user or Slack app associated with the token making the API call is itself a member of the conversation.

EDIT: then, you can retreive the list of public channel and have this flag on every channel accessible : https://api.slack.com/methods/conversations.list

If you are writing a WebSocket-based "RTM" bot, you can listen for the channel_joined event, which is sent only for your own user/bot.

For the more typical Webhook-based bot, the best choice is to listen for the member_joined_channel event and compare the user field if you want an event-based implementation. Hardcoding or otherwise storing your bot's user id is a necessity.

Otherwise, as suggested in the previous answer, you can periodically query all conversations with the conversations.list method and check if you have become a member using the is_member field.

In case one of these methods does not provide the is_private field that you need to determine whether a channel is public, you can use the conversations.info method, which returns a channel object with the is_private field.

Related