Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

newline character not passing through to slack #21

Open
Zxurian opened this issue Jun 1, 2015 · 25 comments
Open

newline character not passing through to slack #21

Zxurian opened this issue Jun 1, 2015 · 25 comments

Comments

@Zxurian
Copy link
Contributor

Zxurian commented Jun 1, 2015

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
Copy link
Contributor Author

Zxurian commented Jun 1, 2015

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.

@cleentfaar
Copy link
Owner

@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.

@Zxurian
Copy link
Contributor Author

Zxurian commented Sep 25, 2015

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.

@cleentfaar
Copy link
Owner

Ok thanks will have a look at it asap

@IPvSean
Copy link

IPvSean commented Jan 12, 2017

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

@yanmwalls
Copy link

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

@louisluu
Copy link

louisluu commented Mar 6, 2017

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)

@bheltzel
Copy link

bheltzel commented May 9, 2017

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

@jaxxreal
Copy link

jaxxreal commented Jun 5, 2017

Still doesn't work 👎

@ramlongcat
Copy link

ramlongcat commented Jun 9, 2017

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.

@deepa2083
Copy link

doesnt work for me either. any updates here?

@iamteedoh
Copy link

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

@ramlongcat
Copy link

ramlongcat commented Jun 30, 2017

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

@iamteedoh
Copy link

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

@jasontrask
Copy link

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

@bheltzel
Copy link

@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.

@iamteedoh
Copy link

iamteedoh commented Jul 17, 2017

@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

@mundomatt
Copy link

mundomatt commented Dec 5, 2017

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.

@lauriconeves
Copy link

/n/r worked for me.

'test1 /n/r test2'

result: test1
test2

@davidlinc1
Copy link

davidlinc1 commented Jan 27, 2018

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

@sisiwei
Copy link

sisiwei commented Feb 1, 2018

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.

@shswanson
Copy link

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;

@hdep
Copy link

hdep commented Apr 9, 2018

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 ?

@glazeus
Copy link

glazeus commented Apr 12, 2018

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

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

@glazeus
Copy link

glazeus commented Apr 12, 2018

"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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests