Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slack bot fails to send message to restricted general channel via chat.postMessage

This is my first time trying to create a slack bot and I am following this template code to the word, I have not made any changes, and just remixed on glitch, copy-pasted the auth tokens correctly, things worked just fine. That is until I made the #general channel restricted for Full Member users.

This is the error I see in the logs at glitch.

PostMessage Error: restricted_action

Is there an additional scope that I need to set, other than bot ?enter image description here

Here is the workspace user permissions, I am the owner for this workspace. Workspace User Permissions

Here is the code:

const postAnnouncementToChannel = (user, announcement) => {
  const { title, details, channel } = announcement;

  let announcementData = {
    token: process.env.SLACK_ACCESS_TOKEN,
    channel: channel,
    text: `:loudspeaker: Announcement from: <@${user}>`,
    attachments: JSON.stringify([
      {
        title: title,
        text: details,
        footer: 'DM me to make announcements.'
      }
    ])
  };
  send(announcementData, user);
}


const send = async(data) => { 
  data.as_user = true; // send DM as a bot, not Slackbot
  const result = await axios.post(`${apiUrl}/chat.postMessage`, qs.stringify(data))
  try {
    if(result.data.error) console.log(`PostMessage Error: ${result.data.error}`);
  } catch(err) {
    console.log(err);
  }
}

Testing it via

https://api.slack.com/methods/chat.postMessage/test using bot-token says

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

Testing this using xoxp-token gives this:-

{
    "ok": false,
    "error": "missing_scope",
    "needed": "chat:write:user",
    "provided": "identify,bot"
}
like image 349
MithunS Avatar asked Apr 26 '19 16:04

MithunS


People also ask

Can slack BOT post to private channel?

Post to a private channelAs long as the authenticated user is a member of the private channel, pass the channel's ID ( C123456 ) to the channel parameter and the message will be posted to that channel. The private channel's ID can be retrieved through the conversations. list API method.

Can slack BOT send direct message to user?

You may see Slackbot in channels throughout your workspace, delivering reminders and automated messages for you and your teammates. When there's a reminder or a message just for you, Slackbot will send you a DM. You can access your DM with Slackbot the same way you would find a DM with anyone else in your workspace.

Can a slack app post to multiple channels?

Option 1 - Slack Scheduler Slack scheduler is a quick tool that is designed to free up time and let people use Slack at the most efficient time. It comes with a built in advanced scheduler that allows you to schedule messages to multiple channels, users or conversations.


1 Answers

No. You are not missing any scopes. Its just that the user you used to auth your app can not post into the general channel. Apparently admins have restricted who can post messages in that channel, e.g. to admins only.

Either use a user that has posting rights for that channel to auth your app or switch to a different channel for your testing.

like image 141
Erik Kalkoken Avatar answered Sep 20 '22 13:09

Erik Kalkoken