I am using this pipe to send slack message to my channel, and It works fine.
- pipe: atlassian/slack-notify:2.0.0
variables:
WEBHOOK_URL: $WEBHOOK_URL
MESSAGE: "Content for message preview in notifications"
My problem is that I get "The content can't be displayed" on iOS notification:
why this happen?
Hello @Mustafa Abdelhakam ,
Thank you for reaching out to Atlassian Community!
When using the atlassian/slack-notify pipe, and no value is provided to the PAYLOAD_FILE variable, the pipe will use its default payload structure, which looks like the following :
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": pretext
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
}
Source : atlassian/slack-notify/pipe/pipe.py#lines-69
Slack looks for the top-level message property (outside all the blocks definitions) to get the notification message. Since the pipe's default payload does not contain the message attribute outside the blocks section, Slack does not render the notification.
In order to solve this, you can use a custom payload structure that contains the message property, and then substitute the values of the env variables within the custom payload by using the command envsubst .
To use a custom payload you can follow the below instructions :
1. Create a file in the repository where you will store the payload (message structure). In this example we will use a file named payload.json.template with the following content:
{
"text": "Text here for notifications",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "$PRETEXT"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "$MESSAGE"
}
}
]
}
You can see that a text property was added outside the blocks definition. Also, the payload template expects to receive the value of variables $PRETEXT and $MESSAGE.
2. Update your YML file for the pipe to use the custom payload
- step:
name: Test Slack notification
script:
- apt-get update && apt-get install -y gettext-base
- export MESSAGE="My slack message"
- export PRETEXT="My slack pretext"
- envsubst < "payload.json.template" > "payload.json"
- pipe: atlassian/slack-notify:2.0.0
variables:
WEBHOOK_URL: $WEBHOOK_URL
PAYLOAD_FILE: payload.json
We first install the package gettext-base that contains the command envsubst. Then we export the variables MESSAGE and PRETEXT with the contents we want and use the envsubst command to replace the value of these variables in our payload.json.template, saving the result of substitution to a file named payload.json. Finally, we configure the pipe's variable PAYLOAD_FILE to use payload.json.
This should make Slack render the notification message with the text you have placed in the top-level text property of the payload.json.template.
You can also use Slack's block builder app to test different payload structures.
Hope that helps! Let me know in case you have any questions.
Thank you, @Mustafa Abdelhakam !
Patrik S
thanks @Patrik S
It woks fine.
but I am wondering, before I was setting the message with text that contains multiline character "\n" and it was working fine.
now, when I set the MESSAGE variable with multiline text, it gives me error:
✖ Failed to parse PAYLOAD_FILE payload.json: invalid JSON provided.
Ex:
this will work
- step:
name: Test Slack notification
script:
- echo "slack"
- pipe: atlassian/slack-notify:2.0.0
variables:
WEBHOOK_URL: $WEBHOOK_URL
MESSAGE: "$(git log -n 7 --pretty=format:' %s')"
this will NOT work
- step:
name: Test Slack notification
script:
- apt-get update && apt-get install -y gettext-base
- export MESSAGE="$(git log -n 7 --pretty=format:' %s') "
- export PRETEXT="My slack pretext"
- envsubst < "payload.json.template" > "payload.json"
- pipe: atlassian/slack-notify:2.0.0
variables:
WEBHOOK_URL: $WEBHOOK_URL
PAYLOAD_FILE: payload.json
how can I set the MESSAGE variable with text that contains multi lines?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Mustafa Abdelhakam ,
I think this is because JSON specification does not support newline characters.
I think you will need to use the sed and tr commands to replace the new lines in the content of the message with the literal '\n'.
I was able to get it working using the following commands :
export MESSAGE=$(git log -n 7 --pretty=format:' %s' | sed 's/$/\\n/' | tr -d '\n')
We're basically passing the output of git log to sed, which will add a '\n' at the end of every line, and then tr will remove the original new line characters.
Could you please try that method and let us know how it goes?
Thank you, @Mustafa Abdelhakam !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.