slackapi / bolt-python

A framework to build Slack apps using Python
https://slack.dev/bolt-python/
MIT License
1.03k stars 237 forks source link

Error in chat_postMessage method #1016

Closed soilmo closed 6 months ago

soilmo commented 6 months ago

I tried the following code:

block = [{"type": "section","text": {"type": "mrkdwn","text": "Erro na consulta " + str(datetime.now())}}]
client_bot.chat_postMessage(channel=channel_id, blocks = block)

And i got the answer:

Failed to run listener function (error: The request to the Slack API failed. (url: https://www.slack.com/api/chat.postMessage)
The server responded with: {'ok': False, 'error': 'invalid_blocks', 'errors': ['invalid_url [json-pointer:/blocks/0/slack_file.url]'], 'response_metadata': {'messages': ['[ERROR] invalid_url [json-pointer:/blocks/0/slack_file.url]']}})

This same code ran successfully about four hours earlier, but now it has this error.

Can anyone help me?

filmaj commented 6 months ago

Hey @soilmo , we are investigating this. We are seeing elevated rates of this error; our engineering teams are looking into the issue. Once I have more info I'll post here again.

soilmo commented 6 months ago

Thanks! I'll be waiting for good news.

filmaj commented 6 months ago

@soilmo I can't reproduce the error you are seeing with the payload you provided in your original post. Are you still seeing this?

soilmo commented 6 months ago

@filmaj i tried again and got the same error.

Let me explain better about my situation:

I created a slack bot using Slack Bolt. It works well, but today after lunch, some functions doesn't work. All functions the send images happened this problem.

See print below to understand better:

image

filmaj commented 6 months ago

OK, we were very much struggling with how this error could happen without an image being included in the blocks payload!

Can you please post the blocks payload that is causing the error? Based on your screenshot it seems you are uploading a file. Specifically: what field of the uploaded file / file object are you passing into the what field of the image block?

soilmo commented 6 months ago

Sure!

The block is below:


say(

            blocks = [

                {
                    "type": "section",
                    "text": {
                        "type": "plain_text",
                        "text": 'Última atualização: '+str(infos[1])+' '+str(infos[2]) + ' | ITD do IPCA é o mesmo do FOCUS FIC FIA (2020-12-28)'
                    },
                    "accessory": {
                        "type": "image",
                        "slack_file": {
                            "url": infos[0]
                        },
                        "alt_text": "Rentabilidades"
                    }
                }
            ]
        )

Where:

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

filmaj commented 6 months ago

Whoa, you're already using "slack_file": { "url": infos[0] }? We just released that as a brand new feature, like, 4 hours ago?

filmaj commented 6 months ago

We believe the issue may be the pub_secret querystring param. Try dropping that? Also: where are you getting that URL from?

soilmo commented 6 months ago

Nice! Exactly! My problem was started about 4 hours ago.

My URL is been created like this:


        client = WebClient(token=SLACK_USER_OAUTH_TOKEN)

        result = client.files_upload(channel=channel_id,file=path, initial_comment = 'Carteira')
        result_url = client.files_sharedPublicURL(file = result['file']['id'])

        website = result_url['file']['permalink_public'].split("https://slack-files.com/")[1]
        team_id = website.split('-')[0]
        file_id = website.split('-')[1]
        pub_secret = website.split('-')[2]
        filename = result_url['file']['name']
        direct_link = "https://files.slack.com/files-pri/" + team_id + "-" + file_id + "/" + filename + "?pub_secret=" + pub_secret

Your suggest is do the following?


        client = WebClient(token=SLACK_USER_OAUTH_TOKEN)

        result = client.files_upload(channel=channel_id,file=path, initial_comment = 'Carteira')
        result_url = client.files_sharedPublicURL(file = result['file']['id'])

        website = result_url['file']['permalink_public'].split("https://slack-files.com/")[1]
        team_id = website.split('-')[0]
        file_id = website.split('-')[1]
        filename = result_url['file']['name']
        direct_link = "https://files.slack.com/files-pri/" + team_id + "-" + file_id + "/" + filename
filmaj commented 6 months ago

OK, according to our docs for slack_file:

slack_file.url | string | This URL can be the url_private or the permalink of the Slack file.

So try using permalink rather than permalink_public. Now with the slack_file support, you shouldn't need to explicitly call sharedPublicURL since the new "secure image" feature lets you use un-shared files as image sources within Slack.

Also curious: what was your solution here before the switch to slack_file? The reason I ask is that, if you experienced this issue, I assume other developers probably did, too. It would be good to see the before/after code to see where we can improve the docs / tools to guide developers better.

Appreciate you working through this with me 🙏

soilmo commented 6 months ago

Sure! I appreciate contribute into this task!

But i dont fix error yet. Now, i tried like this:

client = WebClient(token=SLACK_USER_OAUTH_TOKEN)

result = client.files_upload(channel=channel_id,file=path, initial_comment = 'Rentabilidades')
result_url = client.files_sharedPublicURL(file = result['file']['id'])

website = result_url['file']['permalink']
print(website)

And the block used was:


say(

            blocks = [

                {
                    "type": "section",
                    "text": {
                        "type": "plain_text",
                        "text": 'Última atualização: '+str(infos[1])+' '+str(infos[2]) + ' | ITD do IPCA é o mesmo do FOCUS FIC FIA (2020-12-28)\n'
                    },
                    "accessory": {
                        "type": "image",
                        "slack_file": {
                            "url": infos[0]
                        },
                        "alt_text": "Rentabilidades"
                    }
                }

            ]
        )

I got the same error:

image

Note that infos[0] = "https://indiecapitalworkspace.slack.com/files/U01HSA5NLH4/F06F1SJ08SC/rentabilidades.png"

filmaj commented 6 months ago

OK, different error. Looking into it...

filmaj commented 6 months ago

What kind of token is being used? A bot token or a user token? Does your app implement OAuth flow to retrieve user tokens to make actions on behalf of a user?

soilmo commented 6 months ago

I use a bot token to create WebClient object and Bot User OAuth Token when i upload image on slack cloud.

Maybe use these differents codes in the same bot can make this error.

filmaj commented 6 months ago

That's a bit of a confusing answer 😅 What I need to know is, do you use an xoxb-... or an xoxp-... token for each of the different Slack API calls? (files.upload, files.sharedPublicURL, chat.postMessage)

soilmo commented 6 months ago

Oh, sorry. But yes. Let me show you:

SLACK_BOT_TOKEN: xoxb-... SLACK_USER_OAUTH_TOKEN: xoxp-...

image

image

filmaj commented 6 months ago

OK great, that's what I needed to know. And for the chat.postMessage API, which token is used?

soilmo commented 6 months ago

Hi @filmaj ! Sorry my late.

So, i used say method to send:

image

Its curious the error staying at chat.postMessage.

soilmo commented 6 months ago

@filmaj i solved!! :)

I just use the same token at the following parts:

Starting bot at the beggining of code: image

Upload file (image) in slack cloud. image

And for send file, i used the method below:

image

filmaj commented 6 months ago

Glad you got it fixed!

FYI, we actually will be rolling back the new slack_file property / feature of image blocks today due to various issues it caused for customers (including the problem you experienced earlier). So please continue using the image_url property as I see you are doing in your last screenshot for the time being.

I will close this issue now but if you have further issues here, feel free to re-open or file a new issue.

soilmo commented 6 months ago

Hi!

The same error is happening again.

To remember, the situation is that i am trying send image from slack cloud.

image

Me 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