113

I'm starting out with the Slack API and trying to just get a list of messages.

Here are my steps:

  1. Created a Slack app and gave it channels:read and channels:history scope (also re-installed it)
  2. Queried the list of channels with conversations.list (this worked fine)
  3. From the output of conversations.list, I found a channel that I use and copied the id
  4. Used the conversations.history api with the channelid from step 3

Result:

{ "ok": false, "error": "not_in_channel" }

I'm not at all sure what is happening here. I definitely have messages in the channel, and the documentation page for that api does not say anything about this "not_in_channel" error code.

What am I doing wrong?

4
  • Hello @eric-jorgensen, did you got any solution for adding bot or user to channel by using API. Please let me know. Thank You Mar 13, 2021 at 12:03
  • 1
    I found the solution, first a bot need to join the channel with conversations.join and after that a bot can able to send the message to a channel. Mar 13, 2021 at 12:11
  • @AbhilashSharma, definitely I spent 3-4 hours to find out same, and then added slack app-permissions, as chat:write.public and it worked. May 22, 2021 at 11:52
  • 1
    It seems like as of now (March 2023) chat:write:public doesn't help
    – Kazuki
    Mar 12 at 14:18

7 Answers 7

186

After a long time of investigations (~2 hours), I found an easy approach. For Caleb's answer, I didn't understand how to invite a Bot to the channel. Hence, I am posting this answer.

Go to your Slack Channel and type the following as a message.

/invite @BOT_NAME

Eg: If your Bot name is SRE Incident Manager the command would be as follows.

/invite @sre_incident_manager

As soon as you start typing @, Slack will automatically suggest. So it becomes easy. For this, the Bot needs to be added to your Slack Workspace.

PS: Original answer.

2
  • 5
    I added the bot to my channel multiple times using "Reinstall App" button on the app settings page but that didn't help. But then I used /invite @BOT_NAME and it worked. May 17, 2020 at 11:57
  • Is there a way to do this through the SDK?
    – AliAvci
    May 21, 2021 at 18:33
65

The error not_in_channel has the exact meaning, your custom Slack app should be added to the channel.

Exact solution 1

To resolve the error, in the Web Slack interface:

  • Open channel settings
  • Click on the Integrations tab
  • Click Add apps and find your custom app.

Slack app might have different interface, see Iryna Vernik's answer. enter image description here


Alternative solution 2

Give access to the bot to all channels by adding workspace wide scope, for example, chat:write.public. Depends on your needs and security requirements.

Alternative solution 3

To access the channel chat from API specify Incoming webhook. Slack will generate a unique URL with the token per each channel. Only convenient for a few channels.

3
  • 1
    Thank you. The chat:write + chat:write.public helped and it was exactly the solution I needed Dec 13, 2021 at 6:39
  • @Naoyoshi Aikawa's answer also points out an alternative where you can grant the conversations.join scope to your app, and have your code use the /conversations.join?channel=<id> API before hitting the /conversations.list?channel=<id> API. This keeps a read-only scope, but, like your 2nd alternative, avoids any change to architecture or manual intervention requirement.
    – Sean
    Jul 19 at 19:38
  • @AlexSorokoletov that also fixed my issue! Nov 18 at 13:04
43

This error arises when you are using the bot oauth token and the bot is not invited to the channel. To solve this you need to

  • Invite the bot(slack app) to join the channel.
  • Use the OAuth Access Token instead

To add Bot to your channel you need to write /invite @Bot_name in the slack channel

2
  • does this mean no need to invite if using OAuth Access Token?
    – Justin Lin
    Dec 7, 2020 at 2:45
  • My app was able to post messages to a channel without problems, but kept getting the not_in_channel error when it tried adding reactions to those messages. This fixed it.
    – miken32
    Jul 14, 2021 at 18:45
18

I also didn't understand how to invite a Bot to the channel. Way that was proposed by Caleb and Keet was not clear for me or not working. From my side, 'invite' work after

  • open channel
  • in Details tab, choose a 'More'clause
  • in dropdown menu, chouse an 'add app'
  • in pop-up look for you app (bot)

Also i was use Bot User OAuth Access Token, because i need this functionality in private channel (additionaly, you should add for bot groups:history scope)

0
4

As all others said, you need to join each channel.

The bot can join channel programmatically by using API below:

https://api.slack.com/methods/conversations.join

Don't forget to add permission of conversations.join

2

I'm getting started with the Slack API as well, and I've come to realize that not_in_channel simply means that the user/bot you are using the token for hasn't joined that particular channel you're trying to perform an action on.

Think of it this way: if you're using Slack on the web-browser or web-app, you wouldn't be able to post a message on a channel you haven't either joined or was invited to.

☝️ You'll also never run into this issue through the Slack UI/UX because you're not even able to access the channels UNTIL you are invited or join it.

Click to see png example of a slack message stating my bot being added to a channel

However, because we're using the API we can essentially skip some steps, and in this case we skipped the step where a user/bot has joined the channel before doing the action we're trying to perform (writing a message, grabbing information, etc).

💪 How to address this

There's probably plenty of ways to do it that I'm not versed in, but if you're just concerned about a specific channel or two without the concern of scaling to x channels I'll list the way that worked for me.

📇 /invite Slash Command

As others have mentioned, putting /invite in the message box lets you use Slack's slash command shortcut to add users. What's important is this way also allows us to invite bots to the channel.

Putting "@" triggers Slack to start auto-suggesting, which is why it then becomes easy to find your bot name in the list.

Click here to see screenshot example of the /invite command with @bot_name_here

Hope this helps answer people's question on why it's happening, and thank you to the original posts that got me out of my initial mess. 🙏

0

FOr me, instead of invite a user/bot, I invite the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.