slackapi / java-slack-sdk

Slack Developer Kit (including Bolt for Java) for any JVM language
https://slack.dev/java-slack-sdk/
MIT License
571 stars 212 forks source link

Unable to set a SlackFile in Block #1349

Closed Whizyyy closed 3 weeks ago

Whizyyy commented 4 weeks ago

I successfully upload a file to Slack, then I want to display it in a block using "SlackFile" but I get an error without any detail to help me. It is actually working with imageUrl so the issue is for sure the SlackFile and not the block itself.

The Slack SDK version

1.40.3

Java Runtime version

jdk-21.0.2

OS info

1 SMP Debian 5.10.149-2 (2022-10-21)

Steps to reproduce:

final FilesUploadV2Response uploadResponse =
        this.app.getApp().client().filesUploadV2(b -> b.filename("report.png").fileData(bytes));
if (!uploadResponse.isOk()) {
    // Never happens during my tests.
    return Response.error(500);
}

final WebhookResponse webhookResponse = context.respond(r -> r.replaceOriginal(true)
        .blocks(asBlocks(
                image(i -> i.blockId("image_test_block")
                        .altText("Report temp text")
                        .slackFile(SlackFileObject.builder().id(uploadResponse.getFile().getId()).build())
                        .title(plainText("Report")))
        )));

LOGGER.info("{}", JsonOps.toJsonString(webhookResponse));
// {"code":404,"message":"","body":"{\"ok\":false,\"error\":\"invalid_blocks\"}"}

Expected result:

No error, the message with the block will be sent.

Actual result:

Nothing happens

zimeg commented 4 weeks ago

Hey @Whizyyy! 👋 Uploading a file with filesUploadV2 is an asynchronous process that continues even after the ok response is received in response, so the invalid_block error might be caused from use of an "unfinished" file ID.

It is actually working with imageUrl so the issue is for sure the SlackFile and not the block itself.

Just for my understanding is this an image URL from the uploaded file?

When testing something similar, and without delay after uploading, I'm finding the following error when using a file ID:

{"ok":false,"error":"invalid_blocks","errors":["invalid slack file [json-pointer:\/blocks\/0\/slack_file.id\/slack_file]"],"response_metadata":{"messages":["[ERROR] invalid slack file [json-pointer:\/blocks\/0\/slack_file.id\/slack_file]"]}}

And I'm also finding that a pause before using this file ID often fixes this. A polling approach (shown earlier in that thread) might offer a more robust solution, but I'm curious if either this error seems familiar or if this change removes the error?

Whizyyy commented 4 weeks ago

Hey @zimeg, No it works with an image from google (using imageUrl). Is there a way to know if the file is correctly uploaded? Anyway even after waiting 30 seconds, I got the same error.

zimeg commented 4 weeks ago

@Whizyyy Good to know it wasn't the same file working in some cases but not others!

Can you try logging the result of files.info before attempting to post it via webhook? This should give hints at a complete upload. I'm curious if certain metadata file information like thumbnails appear in this response 🤔

I'm also curious if including a .title() during the upload is needed to and this is perhaps related to #1345?

Whizyyy commented 3 weeks ago

Here are the 2 results, "direct result" is debug just after the upload responded OK and "delayed result" is after waiting for 10 seconds direct-result.json delayed-result.json

zimeg commented 3 weeks ago

@Whizyyy I'm having trouble downloading the files for some reason - would it be possible to share this inline?

Whizyyy commented 3 weeks ago

Sure @zimeg,

Direct Result:

{
   "ok":true,
   "file":{
      "id":"F07GX5F33P1",
      "created":1723883018,
      "timestamp":1723883018,
      "name":"report.png",
      "title":"report.png",
      "mimetype":"",
      "filetype":"",
      "pretty_type":"",
      "user":"U07AB3S42TZ",
      "user_team":"T0567GQPSN8",
      "mode":"hosted",
      "editable":false,
      "non_owner_editable":false,
      "file_access":"visible",
      "is_external":false,
      "external_type":"",
      "username":"",
      "size":1300068,
      "url_private":"https://files.slack.com/files-pri/T0567GQPSN8-F07GX5F33P1/report.png",
      "url_private_download":"https://files.slack.com/files-pri/T0567GQPSN8-F07GX5F33P1/download/report.png",
      "permalink":"https://translatorforslack.slack.com/files/U07AB3S42TZ/F07GX5F33P1/report.png",
      "permalink_public":"https://slack-files.com/T0567GQPSN8-F07GX5F33P1-efbfe42f3c",
      "has_rich_preview":false,
      "media_display_type":"unknown",
      "preview_is_truncated":false,
      "has_more":false,
      "sent_to_self":false,
      "is_public":false,
      "public_url_shared":false,
      "display_as_bot":false,
      "channels":[

      ],
      "groups":[

      ],
      "ims":[

      ],
      "shares":{

      },
      "has_more_shares":false,
      "is_channel_space":false,
      "favorites":[

      ],
      "is_starred":false,
      "comments_count":0
   },
   "is_truncated":false,
   "comments":[

   ],
   "response_metadata":{
      "next_cursor":""
   }
}

Delayed Result:

{
   "ok":true,
   "file":{
      "id":"F07GX5F33P1",
      "created":1723883018,
      "timestamp":1723883018,
      "name":"report.png",
      "title":"report.png",
      "mimetype":"image/png",
      "filetype":"png",
      "pretty_type":"PNG",
      "user":"U07AB3S42TZ",
      "user_team":"T0567GQPSN8",
      "mode":"hosted",
      "editable":false,
      "non_owner_editable":false,
      "file_access":"visible",
      "alt_txt":"temp alt txt",
      "is_external":false,
      "external_type":"",
      "username":"",
      "size":1300042,
      "url_private":"https://files.slack.com/files-pri/T0567GQPSN8-F07GX5F33P1/report.png",
      "url_private_download":"https://files.slack.com/files-pri/T0567GQPSN8-F07GX5F33P1/download/report.png",
      "thumb_64":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_64.png",
      "thumb_80":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_80.png",
      "thumb_160":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_160.png",
      "thumb_360":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_360.png",
      "thumb_360_w":"360",
      "thumb_360_h":"203",
      "thumb_480":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_480.png",
      "thumb_480_w":"480",
      "thumb_480_h":"270",
      "thumb_720":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_720.png",
      "thumb_720_w":"720",
      "thumb_720_h":"405",
      "thumb_800":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_800.png",
      "thumb_800_w":"800",
      "thumb_800_h":"450",
      "thumb_960":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_960.png",
      "thumb_960_w":"960",
      "thumb_960_h":"540",
      "thumb_1024":"https://files.slack.com/files-tmb/T0567GQPSN8-F07GX5F33P1-bb3c25f082/report_1024.png",
      "thumb_1024_w":"1024",
      "thumb_1024_h":"576",
      "thumb_tiny":"AwAbADCtGcxgHtSNjoKIgRGGAOc4qVkHdHBPqKq99BESLucLnGT1qxLboiFkk3e1LGqo0bhT6Zx3pJZFfhRux1bGKllIaSEXA61Tfl2q2oJRyUJ44OOlVG+8abdxWLlq5SPBXcp7VKZtqtuQlPQmmWnKc1JdcWzY9qrlVrhcroxz85G1qeQp4T5u+CaqqTinbmXlTgioAfvZWZHyM8cdqrvw5pxJL5JyTzRccTtimB//2Q\u003d\u003d",
      "original_w":"1280",
      "original_h":"720",
      "permalink":"https://translatorforslack.slack.com/files/U07AB3S42TZ/F07GX5F33P1/report.png",
      "permalink_public":"https://slack-files.com/T0567GQPSN8-F07GX5F33P1-efbfe42f3c",
      "has_rich_preview":false,
      "media_display_type":"unknown",
      "preview_is_truncated":false,
      "has_more":false,
      "sent_to_self":false,
      "is_public":false,
      "public_url_shared":false,
      "display_as_bot":false,
      "channels":[

      ],
      "groups":[

      ],
      "ims":[

      ],
      "shares":{

      },
      "has_more_shares":false,
      "is_channel_space":false,
      "favorites":[

      ],
      "is_starred":false,
      "comments_count":0
   },
   "is_truncated":false,
   "comments":[

   ],
   "response_metadata":{
      "next_cursor":""
   }
}
seratch commented 3 weeks ago

The file needs to be shared in the channel where you're trying to post a message including the image block. Your filesUploadV2 method call must have channelId to grant access to the channel members before embedding it as part of message blocks. Your current code is trying to use an image file, which is not shared with the channel yet. In other words, if you don't pass channelId, only your bot user can access the file.

Whizyyy commented 3 weeks ago

Alright, it would be nice if this kind of problem could be printed somehow, instead of "invalid blocks". Thanks for the clarification