slack-github-action icon indicating copy to clipboard operation
slack-github-action copied to clipboard

Filetype is not available immediately after uploading a file

Open samsul-kabir-cihub opened this issue 9 months ago โ€ข 2 comments

Hi All,

I am trying to send slack message with image from github action, but it is failing everytime. First let me explain, what I am trying to achieve:

  • Upload an image in slack
  • Extract the permalink of the uploaded image
  • Then add that permalink into slack message and send

All these steps are working fine, except it fails to send the message with image. Let me share the code:

Upload image and extract permalink:

`      # ๐Ÿ“ค Upload Doughnut Chart to Slack & Get Image URL
      - name: Upload Chart to Slack
        if: always()
        run: |
          response=$(curl -s -X POST \
            -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
            -H "Content-Type: application/x-www-form-urlencoded" \
            --data "filename=test_results.png&length=$(stat -c%s test_results.png)" \
            https://slack.com/api/files.getUploadURLExternal)

          echo "Slack Upload URL Response: $response"

          upload_url=$(echo "$response" | jq -r '.upload_url')
          file_id=$(echo "$response" | jq -r '.file_id')

          if [ "$upload_url" == "null" ] || [ "$file_id" == "null" ]; then
            echo "โš ๏ธ Failed to get upload URL from Slack."
            exit 1
          fi

          # Step 2: Upload the file to the received URL
          upload_response=$(curl -X POST \
            -H "Content-Type: image/png" \
            -F file=@test_results.png \
            "$upload_url")

          echo "Slack File Upload Response: $upload_response"

          # Step 3: Complete the upload and get the file permalink
          complete_response=$(curl -s -X POST \
            -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d '{
                  "files": [{"id": "'"$file_id"'", "title": "Test Results"}]
                }' \
            https://slack.com/api/files.completeUploadExternal)

          echo "Slack Complete Upload Response: $complete_response"

          image_url=$(echo "$complete_response" | jq -r '.files[0].permalink')

          if [ "$image_url" == "null" ]; then
            echo "โš ๏ธ Failed to upload image to Slack."
            exit 1
          else
            echo "โœ… Image uploaded: $image_url"
            echo "IMAGE_URL=$image_url" >> $GITHUB_ENV
          fi`

The output it displays:

Image

Send slack message with image:

`# ๐Ÿ“ข Send Cypress Results to Slack (With Image & Test Summary)
      - name: Send Cypress Results to Slack
        if: always()
        run: |
          curl -X POST \
            -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d '{
              "channel": "XXXXXXX",
              "blocks": [
                {
                  "type": "header",
                  "text": {
                    "type": "plain_text",
                    "text": "๐Ÿงช API Test Report",
                    "emoji": true
                  }
                },
                {
                  "type": "rich_text",
                  "elements": [
                    {
                      "type": "rich_text_section",
                      "elements": [
                        {
                          "type": "text",
                          "text": "'"${{ steps.cypress-results.outputs.failed == '0' && 'โœ… All Tests Passed!' || (steps.cypress-results.outputs.failed != '0' && steps.cypress-results.outputs.passed != '0') && 'โš ๏ธ Some Tests Failed' || 'โŒ All Tests Failed!' }}"'",
                          "style": {
                            "bold": true,
                            "italic": true
                          }
                        }
                      ]
                    }
                  ]
                },
                {
                  "type": "divider"
                },
                {
                  "type": "section",
                  "fields": [
                    {
                      "type": "mrkdwn",
                      "text": "*Total:* `${{ steps.cypress-results.outputs.total }}`\nโœ… Pass: `${{ steps.cypress-results.outputs.passed }}`\nโŒ Fail: `${{ steps.cypress-results.outputs.failed }}`\nโญ๏ธ Skip: `${{ steps.cypress-results.outputs.skipped }}`"
                    },
                    {
                      "type": "mrkdwn",
                      "text": "โฑ๏ธ Duration: `${{ steps.cypress-results.outputs.duration }} sec`\n๐Ÿ“ˆ Success Rate: `${{ steps.cypress-results.outputs.success_rate }}%`"
                    }
                  ],
                  "accessory": {
                    "type": "image",
                    "image_url": "${{ env.IMAGE_URL }}",
                    "alt_text": "Test Results Doughnut Chart"
                  }
                },
                {
                  "type": "context",
                  "elements": [
                    {
                      "type": "mrkdwn",
                      "text": "๐Ÿ“‹ *Reports:* โ€ข ๐Ÿ” <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|GitHub Actions Run> โ€ข ๐Ÿ“Š <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts/${{ env.ARTIFACT_ID }}|Download HTML Report>"
                    }
                  ]
                }
              ]
            }' \
            https://slack.com/api/chat.postMessage`

The Output is displaying:

Image

As per the error it is saying that filetype is missing. If you see the output after uploading the image, then you will also see filetype is missing. I do not know if that is the issue. If that is issue, why image filetype is not setting when uploaded by slack api? Any idea or solution?

samsul-kabir-cihub avatar Mar 19 '25 09:03 samsul-kabir-cihub