14

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"
}
MithunS
  • 485
  • 7
  • 28
  • Can you please post your actual code as well? – Lucas Hendren Apr 26 '19 at 17:00
  • https://github.com/slackapi/template-announcement-approvals is the code I have on glitch, made no changes to the code. – MithunS Apr 26 '19 at 17:01
  • I have not been able to reproduce this error via Postman so far. It will occur if you restrict posting permissions for #general to "Workspace Owners and Admins only" but it works fine if its restricted to "Everyone, except guests". – Erik Kalkoken May 09 '19 at 13:31
  • Let me try this and get back to you. Thank you for your response. – MithunS May 09 '19 at 14:04
  • OK. It would help a lot if you could add more details and clarifications to the question. I asked also some questions below on my answer. The more details we have the easier it would be to reproduce the error and fix it. – Erik Kalkoken May 09 '19 at 14:22

2 Answers2

6

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.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Does the user adding the app has to be workspace admin or Primary owner? I am primary owner , but not workspace admin. Thats what I see when I go to `Administration-> Manage Members` – MithunS Apr 26 '19 at 22:07
  • Actually, let me edit the question to add the photo of how the workspace user permissions are. – MithunS Apr 26 '19 at 22:20
  • Primary Owner is the role with the highest rights and can not be restricted from posting in general. So I doubt the user that you authed your app with is primary owner of your workspace. – Erik Kalkoken Apr 27 '19 at 01:01
  • I uninstalled the app, created a new app, copy-pasted both oauth token and signing secret. And then tried, it is still the same. I still get the same error message. `PostMessage Error: restricted_action` – MithunS Apr 27 '19 at 14:23
  • So, any other ideas or places I can check. – MithunS Apr 29 '19 at 13:29
  • Would it help if I post the http headers for the request and reply for `chat.postMessage` call ? – MithunS Apr 29 '19 at 13:31
  • So, can anyone please help ? – MithunS Apr 30 '19 at 15:21
  • 1. Can you reproduce the behavior with an API call to `chat.postMessage` from [Postman](https://www.getpostman.com/)? 2. Which token type are you using exactly? (access token, bot token, legacy token, ...) 3. Are you calling with the API with JSON body or form body? – Erik Kalkoken May 09 '19 at 13:04
  • Updated question with the details you have asked. – MithunS May 14 '19 at 14:27
5

Bots are not full members so I had to use user token

xoxp-token

to post to chat.postmessage, with

as_user:false

and had to add a missing_scope that is

chat:write:user

And then I was able to make this work correctly. Credit goes to @girliemac for helping out on this one.

https://github.com/slackapi/template-announcement-approvals/issues/6

Thanks

MithunS
  • 485
  • 7
  • 28