slack icon indicating copy to clipboard operation
slack copied to clipboard

newline character not passing through to slack

Open Zxurian opened this issue 9 years ago • 25 comments

sending a chat message payload with \n in it should convert it to a newline within slack. Instead, the \n is coming across in the message as literal characters.

Zxurian avatar Jun 01 '15 20:06 Zxurian

passing "\n" within the message does properly insert the newline within the message, so it may be a problem with the slack documentation saying differently.

Zxurian avatar Jun 01 '15 20:06 Zxurian

@Zxurian Sorry for the late reply, did you find out if this is an issue on our end? I'm happy to contact the Slack staff and ask them about it.

If it's just a matter of wrapping every \n with double quotes ("\n"), we could make some regex that automatically does this for our users.

cleentfaar avatar Sep 20 '15 12:09 cleentfaar

yes, it was a case of wrapping it within double quotes. Not sure if you want to put a regex is your own message parser, or just make it clear in the documentation that if you want to add a new line to a message, needs to be wrapped in double quotes. Personally, I just wrap it in double quotes as it's more clear at the time your sending the message.

Zxurian avatar Sep 25 '15 14:09 Zxurian

Ok thanks will have a look at it asap

cleentfaar avatar Sep 30 '15 12:09 cleentfaar

did this actually get fixed? When using slack module on Ansible my code looks like

      - name: Send header message via Slack
       slack:
         token: 1234/1234/1234
         msg: '"\n"Connections reporting problems in network fabric'
         channel: #autoatuo
         username: "Sean's Auto-BGP Ansible Bot"
       delegate_to: localhost
       tags:
         - slack
       with_items:  "{{ipv6check}}"
       when: '"fe80" not in ipv6check[item][0]'
       tags:

and the output does this "\n"Connections reporting problems in network fabric

IPvSean avatar Jan 12 '17 22:01 IPvSean

This issue is still not fixed. Very frustrating when we are trying to automate messages.
How can we push to get this bug fixed?

yanmwalls avatar Feb 16 '17 17:02 yanmwalls

Same here.

Im using "\n" and PHP_EOL to insert the newline but seems like only 1 or 2 newlines is parsed !?!

Passing 5 newlines, get a message with 2 (middle 2)

louisluu avatar Mar 06 '17 04:03 louisluu

I was using single-quotes to wrap my message. When switching to double-quotes, the new line worked properly. Hope this helps.

bheltzel avatar May 09 '17 22:05 bheltzel

Still doesn't work 👎

jaxxreal avatar Jun 05 '17 13:06 jaxxreal

i got it working on PowerShell by adding the 0x0A character:

# adds a line feed...
$message += (0x0A -as [char])

...terrible and probably not cross-platform, but it works ! I guess this technique could be extended to other languages easily. hope this helps.

ramlongcat avatar Jun 09 '17 13:06 ramlongcat

doesnt work for me either. any updates here?

deepa2083 avatar Jun 23 '17 21:06 deepa2083

I'm also experiencing this issue. I have my message as follows and still get a literal output via slack:

msg: 'This is the first line."\n" This is the second line."\n" This is the third line'

The output still comes back as

This is the first line."\n" This is the second line."\n" This is the third line

iamteedoh avatar Jun 28 '17 12:06 iamteedoh

as i said, just cast the value 10 (or 0x0A in hexadecimal) as a character instead of \n and it should work

ramlongcat avatar Jun 30 '17 20:06 ramlongcat

@ramlongcat can you provide an example of how you are doing this in the playbook? This isn't working for me

iamteedoh avatar Jul 01 '17 21:07 iamteedoh

Just found this thread after experiencing something similar. Although I'm not actually using this library, this thread helped me solve my Slack formatting issue when posting with their API... Thought I'd pay it forward in case this is your issue too!

It appears that Slack is precious about whether you use single or double quotes:

Example 1: single quotes, no newline quotes $string = 'Line 1\nLine 2';

Line 1\nLine 2

Example 2: single quotes with double for the newline $string = 'Line 1"\n"Line 2';

Line 1"\n"Line 2

Example 3: double quotes with single for the newline $string = "Line 1'\n'Line 2";

Line 1' 'Line 2

Example 4: double quotes, no newline quotes $string = "Line 1\nLine 2";

Line 1 Line 2

jasontrask avatar Jul 11 '17 00:07 jasontrask

@unix4linux See @jasontrask message that is well explained. If you outer wrap in double quotes and exclude quotes where you have the new line char (\n), you should be good to go.

bheltzel avatar Jul 12 '17 15:07 bheltzel

@jasontrask - that helped! Thank you!

I specifically used Example 4

Which provides the following output in my slack room:

A new computer is online with IP 192.168.1.100. SSH as the user test and run the command below ansible-playbook -i "localhost," -c local ~/dothis.yml

iamteedoh avatar Jul 17 '17 21:07 iamteedoh

The issue here does not seem to be a problem with the API, but a confusion between single and double quoted strings in PHP. A single quoted string will not interpret any special characters. '\n' will be printed out literally. A double quoted string on the other hand will interpret a number of special characters and also expand variables.

mundomatt avatar Dec 05 '17 17:12 mundomatt

/n/r worked for me.

'test1 /n/r test2'

result: test1 test2

lauriconeves avatar Jan 19 '18 21:01 lauriconeves

Thanks @lauriconeves, that worked for me after trying what others mentioned (I'm trying to use a plain old Slack agent). However, be advised you've got your forward slashes backwards: it's \n\r, and not /n/r, which will print literally in your message. For some reason in this context my eyeballs are awful at telling the difference at-a-glance without close examination.

Also note that when saving your event options JSON, Huginn will take care of escaping characters, so when you look at the options on the Agent Summary screen, it will actually appear as \\n\r.

Here's an example of my JSON:

{
  "webhook_url": "https://hooks.slack.com/services/{{my_token}}",
  "channel": "{{channel_name}}",
  "username": "{{bot_name}}",
  "message": "New alert: \\n\r *{{card.name}}* \\n {{card.link}}",
  "icon": "{{bot_icon}}"
}

Output in Slack:

New alert:

Card name
https://link-to-card.com

davidlinc1 avatar Jan 27 '18 01:01 davidlinc1

In case this helps others: I couldn't get this to work until I realized I needed to be toggling into the direct JSON editor mode in Huginn, as opposed to using the UI json tree UX. Then \n\r worked like a charm.

sisiwei avatar Feb 01 '18 23:02 sisiwei

For what it's worth after a few hours of pulling my hair out, I gave PHP_EOL a shot in a web app I'm working on, and it worked great. Assume there are equivalents in other languages.

$message = '*Error:* ' . $errorMessage . PHP_EOL . '*Name:* ' . $detailArray['toname'] . PHP_EOL . '*Email:* ' . $detailArray['to'] . PHP_EOL . '*Asset:* ' . $assetName . PHP_EOL . '*Asset URL:* ' . $assetURL;

shswanson avatar Feb 23 '18 00:02 shswanson

Hi,

I've got the same issue with curl :

curl -X POST -d channel=#chan -d 'text=test1 "\n" test2' -d token=key -d icon_emoji=:slack: https://slack.com/api/chat.postMessage

the ouput looks like :
test1 "\n" test2

any idea ?

hdep avatar Apr 09 '18 12:04 hdep

same he :( PostMessage="Line 1\nLine 2" resolved as

"message": {
    "text": "Line 1\\nLine 2"

glazeus avatar Apr 12 '18 10:04 glazeus

"There’s no bug anywhere, this is how newline char interpretation works in Bash + Slack API." more info: https://discuss.bitrise.io/t/slack-step-does-not-treat-n-as-a-newline-when-message-generated-by-a-bash-script-step/1776/5

glazeus avatar Apr 12 '18 10:04 glazeus