slack-plugin icon indicating copy to clipboard operation
slack-plugin copied to clipboard

how to send message to whole width in slack?

Open kanivetsi opened this issue 4 years ago • 10 comments

Your checklist for this issue

  • [Jenkins 2.222.4] Jenkins version

  • [Slack Notification Plugin 2.3 ] Plugin version

  • [centos-7] OS

Description

I have a question related to sending a message to Slack from Jenkins (groovy script). Currently, I use SlackNotificationPlugin for sending messages to Slack and I'm trying to send a line for the whole width of Slack, but instead of it, I can use only half of Slack width.

I use code below:

          msg = readJSON text: """
          {
            "pretext": "<placeholder>",
            "text": "<placeholder>",
            "color": "good",
            "mrkdwn_in": [
              "pretext",
              "text"
            ],
            "fields": [
                {
                    "title": "A field's title",
                    "value": "This field's valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa valueaaaaaaaaaaaa",
                    "short": false
                },
                {
                    "title": "A short field's title",
                    "value": "A short field's value",
                    "short": true
                },
                {
                    "title": "A second short field's title",
                    "value": "A second short field's value",
                    "short": true
                }
            ],
            "blocks": [
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*Alternative hotel options*"
                }
              }
            ]
          }
          """
        msg.pretext = pretext.inspect() 
        msg.text = text.inspect()
        slack_channel = partner_channel
        slackSend(channel: slack_channel, color: 'good', attachments: "[${msg.toString()}]")

I found a solution for this case and I tested it in Slack Block Kit Builder:

{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "{8}" } } ] } https://app.slack.com/block-kit-builder/T025QN6JG#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22%60%60%60%7B8%7D%60%60%60%22%7D%7D%5D%7D

In Slack Block Kit Builder, I can successfully send a message and use the whole space of slack: enter image description here

In official documentation for Jenkins Slack Plugin (https://plugins.jenkins.io/slack/), I found that for sending blocks we can use the next construction:

blocks = [ [ "type": "section", "text": [ "type": "mrkdwn", "text": "Hello, Assistant to the Regional Manager Dwight! Michael Scott wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n Please select a restaurant:" ] ], [ "type": "divider" ], [ "type": "section", "text": [ "type": "mrkdwn", "text": "Farmhouse Thai Cuisine\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here" ], "accessory": [ "type": "image", "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg", "alt_text": "alt text for image" ] ] ]

slackSend(channel: "#general", blocks: blocks)

The mentioned code doesn't work for me I have nothing in Slack when I use block construction. Can you, please help me with it? I don't use any Bot User, I just want to send the output of Jenkins to Slack.

Below you can find links to stackoverflow with related issues. https://stackoverflow.com/questions/57438097/how-to-make-slack-api-block-kit-take-up-the-entire-width-of-the-slack-window https://stackoverflow.com/questions/63945799/how-to-send-message-to-whole-width-in-slack?noredirect=1#comment113076714_63945799

Thanks in advance.

kanivetsi avatar Sep 18 '20 09:09 kanivetsi

Hi @kanivetsi ! did you find some solution to this problem ?

puneet-kr-sh avatar Mar 18 '21 03:03 puneet-kr-sh

Hi @puneet-kr-sh! Yes, but looks like it isn't a solution, better say workaround. So, currently, I'm sending messages to Slack with the command below: message = """ { "text":"Some text" } """ curl -X POST -H 'Content-type: application/json' --data '${message}' ${slack_send_key} During the creation Slack App, I got a key, and base on it, I can successfully send a message. Also, I contacted Slack support, they answered me, that they don't support 'Slack Block Kit Builder' construction when you are sending messages to the Slack Application. The only way, which I described abowe.

Regards, Ihor

kanivetsi avatar Mar 18 '21 12:03 kanivetsi

Hi, just replace New-PoshBotCardResponse -Type Normal -Text $Result to New-PoshBotTextResponse -Text $Result -AsCode you will see the message of $Result in Full width in Slack

mengdahuang avatar Dec 11 '21 07:12 mengdahuang

It's still impossible to post a long-width message, @timja any idea about this case? Is it a slack-plugin issue or a Slack API limitation?

benipeled avatar Nov 30 '23 14:11 benipeled

I'd suggest posting it directly using the slack API (postMessage I think) skipping the plugin to see if the issue can be reproduced the

timja avatar Nov 30 '23 14:11 timja

Long msg sent by slack-plugin

longMessage = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s."
slackSend(channel: "@<my-user-id>", message: longMessage, tokenCredentialId: '<slack-token>')

Result: image

Long msg sent by Slack API

curl -X POST -H "Content-type: application/json" -H "Authorization: Bearer $SLACK_TOKEN" -d '{ "channel": "<my-user-id>", "text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s." }' https://slack.com/api/chat.postMessage

Result: image

benipeled avatar Nov 30 '23 15:11 benipeled

I'm not sure if it's related somehow - slackSend() posts messages as attachments (see the block quotes format) by default - why not regular format msg similar to the postMessage endpoint?

benipeled avatar Nov 30 '23 15:11 benipeled

try set sendAsText to true

timja avatar Nov 30 '23 15:11 timja

try set sendAsText to true

Holy moly, it works! thanks! I'd document it on the README and make sure it appears on the Jenkins Snippet Generator as a boolean option, The only place mentioned this option is regarding Unfurling Links (and the code of course)

benipeled avatar Nov 30 '23 15:11 benipeled

Yeah I saw it in the code, a documentation PR would be good

timja avatar Nov 30 '23 16:11 timja