Slack message notification on iOS
Asked by Mustafa Abdelhakam:

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?  

![WhatsApp Image 2023-02-14 at 3.06.47 PM.jpeg](https://community.atlassian.com/t5/image/serverpage/image-id/240190i4C77D4408DACEB2C/image-size/large?v=v2&px=999 "WhatsApp Image 2023-02-14 at 3.06.47 PM.jpeg")


Answers:
author: Patrik S
body: Hello [@Mustafa Abdelhakam](/t5/user/viewprofilepage/user-id/5088579) ,

Thank you for reaching out to Atlassian Community!

When using the [atlassian/slack-notify](https://bitbucket.org/test-workspace-4/pipelines_stage/src/master/) 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](https://bitbucket.org/atlassian/slack-notify/src/aa19f81b6436fc527e47437abf278c1236174e0a/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](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) .

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](https://app.slack.com/block-kit-builder/) app to test different payload structures.

Hope that helps! Let me know in case you have any questions.

Thank you, [@Mustafa Abdelhakam](/t5/user/viewprofilepage/user-id/5088579) !

Patrik S

comments: author: Mustafa Abdelhakam
body: thanks [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)   

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')"
```

<br />

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
```

<br />

how can I set the MESSAGE variable with text that contains multi lines?


author: Patrik S
body: Hello [@Mustafa Abdelhakam](/t5/user/viewprofilepage/user-id/5088579) ,

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](/t5/user/viewprofilepage/user-id/5088579) !

Patrik S


author: Mustafa Abdelhakam
body: Hi [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)   

It works   

Thank you


Tags: bitbucket-cloud

cloud

ios

slack