Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot post a message as a user via an application/bot #2092

Open
jpventura opened this issue Apr 13, 2024 · 7 comments
Open

Cannot post a message as a user via an application/bot #2092

jpventura opened this issue Apr 13, 2024 · 7 comments
Labels
auto-triage-stale question M-T: User needs support to use the project

Comments

@jpventura
Copy link

jpventura commented Apr 13, 2024

(Filling out the following with as much detail as you can provide will help us solve your issue sooner.)

Reproducible in:

 "@slack/bolt": "^3.17.1"

The Slack SDK version

(Paste the output of)

Not relevant

or refer to your package.json

Node.js runtime version

node v20.11.1

OS info

Ubuntu macOS
Ubuntu 22.04.4 LTS v13.6.4

(Paste the output of sw_vers && uname -v on macOS/Linux or ver on Windows OS)

Steps to reproduce:

Same issue was reported at bolt-python when trying to use Bolt to post a channel message an specific user:

const result = await app.client.chat.postMessage({
      token: process.env.SLACK_BOT_TOKEN,
      channel: channel_id,
      text: "All your bases are belong to us", 
      as_user: true,
      username: 'username',
      icon_url: "https://i.pinimg.com/736x/b5/13/5f/b5135f5280b8ccb7b48c0bb9eaedc158.jpg"
});

Expected result:

Bolt Slack bot should post a message by the specified username and with a different profile picture,

Actual result

As reported at bolt-python, this feature is not working at their framework, neither bolt-js.

What the chat:write.customize scope enables developers is to customize the message appearance (icon, name) while the message is still by a bot user.

If your situation here is that the username argument for a customized bot message does not work as you expect, please let us know. We'll try to reproduce the situation on our end.

I also does not work at chat.postMessage test page.

I would recommend modify the API to use the user_id instead of username, considering the last one may be changed.

@seratch
Copy link
Member

seratch commented Apr 14, 2024

Hi @jpventura, thanks for asking the question! What "chat:write.customize" scope gives to your app might be confusing, but as mentioned at slackapi/bolt-python#611, this bot scope allows your app to send a "bot" message with a personalized icon and username. This feature is supposed to be used for visualizing bot and various user interactions within a Slack thread conversation.

For your use case, your app needs to serve the OAuth flow for app installation and ask each your end user to grant your app to post a message on behalf of them. More specifically, chat:write in user_scope in the OAuth flow is necessary. Behaving as a human user is so critical that your app needs to receive approval from each user to do so.

I hope this helps.

@jpventura
Copy link
Author

@seratch I tried exactly this call and the icon and name change does not happen. The permission was added to the bot, but it does not seems to work on new apps.

@seratch
Copy link
Member

seratch commented Apr 15, 2024

Just in case, I verified there is no such issue with a newly created app. I create a new app and confirmed the app works for me without any issues. To narrow your issue down, I'd suggest making sure the following:

  • Your app needs to have not only chat:write but also chat:write.customize scope
  • If you add chat:write.customize after the app installation, you need to re-install the app to grant the addiional scope
  • If you're using a user token (xoxp- prefix), it's not possible to customize name and icon for it

I hope this helps.

@jpventura
Copy link
Author

jpventura commented Apr 22, 2024

@seratch Thank you for your attention.

We discovered the problem. According to the documentation, as_user parameter is required only for legacy apps. Including it into new apps causes the unexpected behavior.

The following code works as expected:

const result = await app.client.chat.postMessage({
      channel: channel_id,
      icon_url: 'https://i.pinimg.com/736x/b5/13/5f/b5135f5280b8ccb7b48c0bb9eaedc158.jpg',
      text: 'All your bases are belong to us', 
      token: process.env.SLACK_BOT_TOKEN,
      username: 'CATS',
});

However it does not seem to work with app.client.files.upload function. Is it an API restriction or should it work as well?

@jpventura
Copy link
Author

jpventura commented Apr 22, 2024

I just checked the code and compared the app.client.files.upload arguments:

export interface FilesUploadArguments extends FileUpload, WebAPICallOptions, TokenOverridable {
}

interface FileUpload {
    channels?: string;
    content?: string;
    file?: Buffer | Stream | string;
    filename?: string;
    filetype?: string;
    initial_comment?: string;
    thread_ts?: string;
    title?: string;
}

and app.client.chat.postMessage

export interface ChatPostMessageArguments extends WebAPICallOptions, TokenOverridable {
      channel: string;
      text?: string;
      as_user?: boolean;
      attachments?: MessageAttachment[];
      blocks?: (KnownBlock | Block)[];
      icon_emoji?: string;
      icon_url?: string;
      metadata?: MessageMetadata;
      link_names?: boolean;
      mrkdwn?: boolean;
      parse?: 'full' | 'none';
      reply_broadcast?: boolean;
      thread_ts?: string;
      unfurl_links?: boolean;
      unfurl_media?: boolean;
      username?: string;
}

So I would guess that this feature is unsupported and it would require a patch into the library.

@seratch
Copy link
Member

seratch commented Apr 22, 2024

Hi @jpventura, I overlooked you were trying to use as_user option, but you're right that the option should not be used along with username and icon for the latest permission. As for files.upload API, it does not support the parameters. However, you can do the following instead:

  • Upload files using files.uploadV2 without channel_id and initial_comment parameters (at this point, the files are uploaded but not yet shared)
  • Post a message with uploaded file permalink URLs in a channel along with username and icon using chat.postMessage API

I hope this helps.

Copy link

👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-triage-stale question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

2 participants