slackapi/bolt-js

Unable to upload a file in a direct message using files.completeUploadExternal

Closed this issue ยท 5 comments

When we use files.completeUploadExternal to upload a file in a direct message using Slack userId it throws an error.

Error: invalid_arguments
input must match regex pattern: ^[CGDZ][A-Z0-9]{8,}$ [json-pointer:/channel_id]

To reproduce:

  1. Make an API call to files.getUploadURLExternal.
  2. Make a POST API call to the URL received from the above API call.
  3. Make an API call to files.completeUploadExternal using a Slack userId as the channel_id.

Have you tried using the encoded D1234ABCD direct message ID instead of the U1234ABCD user ID to the channels list? As the error states, it does not allow passing user IDs into channel_id; this parameter only accepts encoded IDs for public channels , group and direct msgs (IDs that start with C, G, D and Z).

In the Slack client, if you click on a direct message header's user name and scroll all the way down, you should be able to find the D... encoded ID for a direct message that this API accepts:

Screenshot 2024-05-15 at 9 21 51โ€ฏAM

Thanks for the clarification @filmaj. I'm aware of this functionality. However, my concern is that the deprecated method files.upload allowed us to use Slack userId to post a file in a direct message. So, why are we no longer supporting it in the new API? Can we not update the regex to include U?

If no, then is it possible to retrieve a direct message ID using the Slack user ID via an API?

@theshdb totally understand, and I mirror your sentiment and I have raised it internally. However, I am only one voice internally and many considerations go into what we do or do not do with the APIs. Priority and product decisions are made by leadership above my paygrade, unfortunately, however, leadership does pay attention to customer feedback that comes in via emails to feedback@slack.com. Please consider emailing there and telling them you have a feature request / complaint about the deprecated API and what you'd like to see as a customer with regards to the new API.

As for retrieving the DM ID, you can use the conversations.open API and provide the U.... encoded user ID as part of the users argument. That should return a payload that looks like:

{
    "ok": true,
    "no_op": true,
    "already_open": true,
    "channel": {
        "id": "D05TKNSGFL3"
    }
}

Got it @filmaj. Thanks for providing the solution on how to get the direct message ID.

This is what I'm using now as an alternate solution to post a file to a DM using a Slack user ID.

if (channelId.startsWith("U")) {
          const conversationsResponse = await web.conversations.open({
            users: channelId,
          });
          channelId = conversationsResponse.channel.id;
        }

Great, glad you got it working. I will close this issue now but if there are any more questions here feel free to re-open or file a new issue.