4

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 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()}]")

On the screenshot below you can see Packages Difference and it showed only on the half of the page.

enter image description here

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

It 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 somebody help me with it? I don't use any Bot User, I just want to send the output of Jenkins to Slack.

Thanks in advance.

  • Does this answer your question? [How to make slack api block kit take up the entire width of the slack window?](https://stackoverflow.com/questions/57438097/how-to-make-slack-api-block-kit-take-up-the-entire-width-of-the-slack-window) (i.e. no, you can't change the width) – Diego Sep 17 '20 at 20:40
  • @Diego thanks for your help. I checked the mentioned post but looks like, that there issue with showing information inside Slack Block Kit Builder. I've already solved this issue and post answer there. But I have a problem with sending info from Jenkins to Slack. I don't have an opportunity to use the whole page, only half. – Игорь Канивец Sep 18 '20 at 11:03

0 Answers0