slackapi / bolt-python

A framework to build Slack apps using Python

Home Page:https://slack.dev/bolt-python/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in chat_postMessage method just for send images

soilmo opened this issue · comments

Hi!

I am facing a problem to send image from slack cloud. The same problem was solved at past week with help of @filmaj :)

image

My message with image is send using method say and block. The code is bellow:

image

Where infos[0] = "https://files.slack.com/files-pri/T01J6BY2VEU-F06EYTKJAJX/rentabilidades.png?pub_secret=bb2f7a0d27"

To complement, i used User Oauth Token to upload image.

image

@soilmo hello again 👋

We decided to move ahead with rolling out file-backed image support, which would break the approach you are currently using. In the end, reverse-engineering images uploaded to Slack and their public URLs - including public access tokens - is not an official supported way documented by Slack. It looks to me like you used this StackOverflow answer.

Please try the following:

  • use the same token to upload the file and to post the message
  • change your blocks payload to:
blocks = [{
  "type": "image",
  "title": {
    "type": "plain_text",
    "text": "This image block contains a reference to a Slack image file."
  },
  "block_id": "image4",
  "slack_file": {
    "id": "F0123456", // <-- get this from the response to the file upload
  },
  "alt_text": "Slack file object."
}]

@filmaj Thanks for the suggestion!

I tried by the following block:

image

Where infos[0] = "F06EW9V4V0V"

This id was got by:

image

After this attempt, i got the error bellow:

image

Can you help with this?

So you are uploading with a user token but are using say, which uses the bot token. Try using the same token for both operations.

Nice!

I configured like that:

image

Uploading the image:

image

In this attempt, I understand the i am using the same token for uploading and sending image.

That's correct. Did it resolve your issue?

No :(

With these settings, i got a new error:

image

I suppose the error occurs at line the following line, something related with channel_id:

image

Because print isn't printed. To support your knowledge my parameter channel_id is a channel id of a private chat of a specific user.

Ah yes so that makes sense. The bot is not a part of the private chat so the channel is unknown to the bot.

Can you explain your use case? What are you trying to accomplish? Where do you want to post the message w/ blocks that contain the uploaded file - to a private DM or to a public channel?

Sure!

I build a bot that answer private chat for each user in my company. So, for each user, bot has a specific channel id.

My problem is just about sending images/files and to answer private DM.

I think that a change parameter channel_id, i will solve this case. But i dont know what channel_it to use kkkkk

client = WebClient(token=SLACK_BOT_TOKEN)
result = client.files_upload(channel=channel_id,file=path, initial_comment = 'Rentabilidades')

So your bot is a member of a private chat (DM) with each user, yes? Does this private chat exist at the time of file uploading?

If my following assumptions are true:

  • a private DM exists between the user and the bot and already has at least 1 message between the bot and the user
  • the bot is to post a message to the private DM that includes the uploaded file as part of its blocks

Then, you should be able to:

  1. Call files_upload using the bot token and removing the channel argument. This makes the file 'private' to the bot - it will not be automatically shared to any channel. For context: calling files_upload with the channel automatically shares the file on-upload to the provided channel and makes the file shareable.
  2. Call post_message or say with a blocks argument that contains an image block with the slack_file.id argument pointing to the uploaded file. As of a few days ago, we support setting image block source to be a private, unshared file uploaded to Slack.

Got it!

I just remove channel argument in files_upload.

Yeah give it a try and let us know how it goes.

It works! Thanks!

About send HTML ou Excel files, how can i do that?

About send HTML ou Excel files, how can i do that?

As part of a message within Slack? It should be similar: upload a file and include a link to the file in the message you create. However, please remember the details around both file permissions and channel membership: by default, when you upload a file using files_upload and DO NOT include the channel parameter, then the file won't be viewable/visible by other users.

The following screenshot is an example of what I mean. In the screenshot, there are two users in a public channel, my app (BoltJsApp) and my user filmaj2. First, my bot uploaded a file WITHOUT the channel parameter. As my own user and not as the bot user, I posted the permalink of the uploaded file. You can see that in that first message from filmaj2, the file did not display! This is because the file is first private - only viewable by the user who uploaded it (the app user BoltJsApp).

Screenshot 2024-01-22 at 12 01 00 PM

However, I then posted the exact same message text as the bot user, which does have access to the (initially private) uploaded file. The image unfurled in that message! This is because the file has now become 'viewable' by everyone in this Slack workspace - it is no longer private, because a link to the private file was shared by the owner of the file within Slack. Now anyone can see it - and you can see too, if I post the same message as my own user filmaj2 with a link to the file, it now unfurls and displays without issue:

Screenshot 2024-01-22 at 12 03 19 PM

I hope that helps explain the details for file permissions, uploading process and sharing files within Slack.

If that solves your problem, please kindly close the issue.

Good luck and happy coding!

Thanks for answer!

It help me, but not completely.

Suppose you have a HTML file or Excel file and you would like to send it as a embedded link in a message. Like a "download message". I tried pass this "direct_link" in text answer, but doesn't work.

Following you can see steps:

image

And to send, i did like this:

image

The answer worked well, but link sent dont make download of file.

You are seeing the same permission issue as before: after you upload the file, it is not viewable/shareable until the owner of the file (the bot) shares the permalink of the file in a public channel. After that, you can use the url_private_download link as a direct link to download the file.

Good job!!

Thanks a lot! It's done!

Great! I will close this issue then.