17

When I enter markdown format in MS Teams (```), how do I create a newline in a string? When a human types in markdown format, pressing Enter results in a newline. When I send a string with <br>, \n or \r\n in text to the connector, it does not create a newline. I've found that I can escape markdown, enter a <br>, and enter markdown again, but then it reformats each line to remove whitespace.

So how can I create a newline and maintain my spacing?

If this matters, I'm using pymsteams connector.

Edit: adding code example

import pymsteams
msg = pymsteams.connectorcard('...')
txt = '``` some text \n second line ```'
msg.text(txt)
msg.send()

I've also tried txt = '``` some text \r\n second line ```' and txt = '``` some text <br> second line ```', but none of the 3 options insert a newline between "some text" and "second line".

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
user3450049
  • 825
  • 1
  • 10
  • 20

2 Answers2

37

If you're trying to just send a connector card w/ text in markdown, there's no need to enclose the text in triple backticks (unless you specifically want preformatted text). We automatically treat the text in a connector card as markdown, unless you set the markdown property to false.

To add line break in markdown, end the line with 3 spaces + linebreak. For example:

msg.text("Line 1   \nLine 2")

Connector cards sent by a bot:

If you added those triple backticks to get preformatted text, then you need to place the backticks on their own lines to allow multiline text. For example:

msg.text("```\nLine 1\nLine 2\n```")

Connector cards sent by a Connector:

If you're developing a connector or using the incoming webhook connector, that uses a different parser that doesn't convert "```" markdown to a <pre> tag. It's best to just fall back to HTML, as Bill suggested below. For example:

msg.text("<pre>Line 1\nLine 2</pre>")

HTML tags work inside a <pre> element, so as you found out, this is equivalent:

msg.text("<pre>Line 1<br>Line 2</pre>")
Adrian Solis
  • 901
  • 7
  • 9
  • Thanks for your help. Is this true if I post to an incoming webhook? I just tested your examples via webhook, and I don't find that I'm formatted in markdown automatically or that the backticks on their own lines allowed me to give a newline between them :( – user3450049 Oct 07 '18 at 04:36
  • 1
    Oh, good question! I assumed this was a bot posting a connector card to a conversation. Incoming webhooks go through a different path, so it could behave differently too. In that case it might be easier to use simple HTML tags, e.g. `br` for simple line break, and `pre` for preformatted text. The built-in connectors (e.g., Bing News, Twitter) use simple HTML tags. – Adrian Solis Oct 08 '18 at 21:42
  • This worked! Thanks. You can make it your answer and I'll accept it. `
     line 1 
    line 2
    `
    – user3450049 Oct 10 '18 at 04:11
  • Terrific! I edited my answer to note the difference between cards sent by bots and connectors. – Adrian Solis Oct 11 '18 at 18:04
  • Using `
    Code
    ` solved my problem but anyone knows the problem with underscores??
    – Wasif Khan Feb 26 '20 at 10:13
5

You didn't provide any code, so it's hard to say exactly what you're trying, but if I set the text value in the card to this:

"text": "text": "There is a problem with Push notifications.<br><br>They don't seem to be picked up by the connector."

This how it's rendered:

enter image description here

Bill Bliss - MSFT
  • 3,553
  • 1
  • 14
  • 17
  • And because they didn't provide any code, the question is off topic and you should be voting to close it, not answer it. [Should one advise on off topic questions?](https://meta.stackoverflow.com/questions/276572/should-one-advise-on-off-topic-questions) – Rob Oct 04 '18 at 02:28
  • I was trying to work in markdown (string enclosed by triple backticks ```) – user3450049 Oct 04 '18 at 16:19
  • 2
    This is a very relevant question regardless of it including code or not. – Konrad Feb 05 '19 at 18:11