mishk0 / slack-bot-api

:rocket: Simple way to control your Slack Bot
MIT License
1.35k stars 176 forks source link

Cannot send messages to private channel #97

Open eBitzu opened 6 years ago

eBitzu commented 6 years ago

I've implemented a slackbot but it seems he can't find private channels even though the bot (app) is on the channel.

mauromazzei commented 6 years ago

Looking at https://api.slack.com/methods/chat.postMessage Seems that postMessage works fine posting on private channels... I've tested right now using postMessage() instead of postMessageToChannel() and it worked ;)

HugoKlepsch commented 6 years ago

I think this issue is related: https://github.com/18F/standup-slack-bot/issues/82 the TL;DR is that slack does not make private channel's existence public, you need to be summoned in the channel to know it exists.

devlemire commented 5 years ago

This is still broken and it's almost 2019. Even if my bot is invited manually to the private channel, it cannot find the private channel when using the https://slack.com/api/chat.postMessage endpoint. I'm using the OAuth Access Token for the authorization header.

Another thing that makes no sense... who in the world thought it was a good idea to return a 200 status code when the API doesn't send the message and literally returns this in the response data:

{
    "ok": false,
    "error": "channel_not_found",
    "warning": "missing_charset",
    "response_metadata": {
        "warnings": [
            "missing_charset"
        ]
    }
}

Ever heard of a 406 error code? 500?

Straight from the documentation (https://api.slack.com/methods/chat.postMessage) This method posts a message to a public channel, private channel, or direct message/IM channel.

???

For whatever reason, the endpoint only works when the creator of the App is in the channel. How is it even making that connection??

Restuta commented 5 years ago

@devlemire this is happening because you are doing this:

I'm using the OAuth Access Token for the authorization header.

OAuth Access Token represents the user who authorized the app, to send messages as the app (bot) you need to use Bot token. This way you should be able to post messages to any channel in which bot exist.

kilabyte commented 5 years ago

This still does not work even when you use the correct Bot token. Ive switched to https://github.com/xBytez/slackbotapi

Restuta commented 5 years ago

I don't think it's about the client library @kilabyte, but I am curious if you have any details, happy to be wrong, but official slack api can be tested manually, without any library. We faced this issue in production and got same exact error described for same exact endpoint.

quang-m-nguyen commented 5 years ago

was facing the issue issue (not able to get the private channel names and id). Got around it by using .getGroups() api

andrew-musoke commented 5 years ago

This issue is still existing. when i use postMessage using my user token, it works for both private and public. Using the bot token, it works only for private. Worse still, I added the bot explicitly via install app and then using the mention. It showed that the app wasnt installed. HAs anyone found a fix/work around yet??

lewkoo commented 5 years ago

This issue is still existing. when i use postMessage using my user token, it works for both private and public. Using the bot token, it works only for private. Worse still, I added the bot explicitly via install app and then using the mention. It showed that the app wasnt installed. HAs anyone found a fix/work around yet??

I did find an approach which worked for me. First of all, I had to add my bot user as an App to the private chat. You can do so by clicking on the channel gear icon next to the search bar and select 'Add App'. Also, my bot has groups:write permission.

Tzikas commented 5 years ago
bot.getGroupId('yourGroupName').then(id=>{
  console.log('id',id)
  bot.postMessage(id, 'cool beans')
})
This worked for me.  
minostro commented 4 years ago

@lewkoo I am facing the same issue, I granted that permission to my bot but when I add the bot to a direct message channel I see the error that the can’t be added to the conversation. Am I missing another permission? I was able to successfully add my bot to a public channel though.

lewkoo commented 4 years ago

@lewkoo I am facing the same issue, I granted that permission to my bot but when I add the bot to a direct message channel I see the error that the can’t be added to the conversation. Am I missing another permission? I was able to successfully add my bot to a public channel though.

@minostro I can not be certain whether your are missing a permission or not, especially since Slack have recently changed their permission structure entirely (see here and here). If I were you and just starting with this, I would upgrade right now since the "old-style" will be deprecated sometime in the future.

Also, I would double check your parameters for postMessage call. Particularly pay attention to the as_user parameter. More on that here, section Post to an IM channel.

minostro commented 4 years ago

@lewkoo yeah, I checked that but I am facing the same issue. The bot cannot post in direct message channels. I keep getting channel_not_found. I tried adding the bot to the direct message channel and slack says that an error happened when executing that operation. I am using the new slack app by the way, and I am using a slash command so users can interact with my bot.

thomas-latterner commented 4 years ago

How I fix this issue:

I removed all the incoming weedhook and all the rights. After I added all the write permission and I reinstall the app (see the screenshot bellow). Another information:

Now it works and I can send files and messages into my private channel. I hope it can help some of you!

image

minostro commented 4 years ago

@skududufru thanks for sharing this! Unfortunately, this does not fix the issue I am having :( with direct messages. So the thing that I want to accomplish is the following:

  1. skududufru opens a direct message with minostro --let's say the id of the channel is DF8GVP9TJ
  2. skududufru and minostro start talking in this direct message channel.
  3. minostro uses the "foo" slash command (which belongs to my slack app) to schedule a message in the future.
  4. my slack app delivers the message at some point in the future to channel DF8GVP9TJ

I was able to implement point 3 without any issues. The problem comes with point 4. My slack app does not have the rights to post into this direct message channel when using the bot token. It's successful though when using the user token (but I don't want to use that). The permissions granted to my slack app is as follows:

Screen Shot 2020-03-30 at 8 39 05 AM

When I installed the app adding the incoming-webhook scope, slack asked me what channel the bot should be able to post to, but I want my bot to be able to post to any direct message my slash command has been used.

the bot was invited to the private channel before reinstal it;

yes! I saw that too, but I still cannot post a message by using the bot token =(

I use the channel name, not the ID.

ah ha! I am still using the id of the channel (DF8GVP9TJ), what would be the channel name in your case? the name of one of the person in the direct message?

thomas-latterner commented 4 years ago

ah ha! I am still using the id of the channel (DF8GVP9TJ), what would be the channel name in your case? the name of one of the person in the direct message? Good question, I didn't try that! Maybe for your case, you need to keep the id.

I saw a lot of people facing this issue. I don't think I can help you anymore :/ For my case it was "why it doesn't work" and after "why now it works"?

I remember I remove all the incooming webhook before to reinstal.