Bots, Web

Microsoft Bot Framework – String to Markdown

TL;DR

What channels you’re using matters! Test out on all your desired platforms before publishing code.

Testing out the limits of the bot framework I tried to create multi-line responses for my bot. The Text property of replies was Markdown, so, I thought it should be easy enough to implement. However, I quickly realized it didn’t always look the way I wanted. Here’s some tips to getting your responses to look just right =).

These examples were all using the

reply = context.MakeMessage();

and implemented the PostAsync method since my responses are all Tasks.

  await context.PostAsync(reply);
  context.Wait(MessageRecieved). 

 
 

Multi-Line Repsonses

You must use \n\n in the string.

Input:

reply.Text = "Hi I'm one line \n\n " +
"I'm line two" +
"I'm line three?" ; 

Output Web:
NewLine-Web

Output Facebook:
NewLine-FB
 
 

Lists

In a list you must have the new line syntax \n\n as well as an ‘*’ with a space after it, be careful here since ‘*’ are also used for italicization. You can also see that the spacing is slightly different between the two channels.

Input:

reply.Text = "Hi I'm one line \n\n" +
"* Item 1 \n\n" +
"* Item 2 " ; 

Output Web:
List-Web

Output Facebook:
List-FB
 
 

Block Quote with Horizontal Rule

Quoted Text must have ‘>’ with one space after to denote that the next chunk of text will be a quote. The Horizontal Rule is marked by ‘—‘. We can especially see the limitations between channels even more in this example.

Input:

  reply.Text = "Block quote below bar \n\n" +
    "---" +
    "\n\n > Something about life. I'm an existential quote \n\n" + 
    "-BOT ";

Output Web:
HRQuote-Web

Output Facebook:
HRQuote-FB
 
 

Headers / Bold, Italics and Strike Throughs

This time – drastic differences between Facebook versus the Web. Note that with headers you must type in \n\n after the header text or the entire string will be part of the first header syntax. And typing ‘ *** ‘ will get bold italics. However Facebook does not register ANY of these.

Input:

  reply.Text = "# Don't know if I need new lines \n\n" +
     "~~You **don't** *need* new lines~~ \n\n" +
     "***yes you do***";

Output Web:
Headers-Web

Output Facebook:
Headers-FB
 
 

Links and Pictures in an Ordered List

Some more differences with Facebook and the Web, but less so. Remember to put \n\n after every item in your list and to leave a space after the ‘.’ following the number.

Input:

  reply.Text = "### List \n\n" +
     "1. Link: [bing](http://bing.com) \n\n" +
     "2. Image Link: ![duck](http://aka.ms/Fo983c)";

Output Web:
Links-Web

Output Facebook:
Links-FB

Hope this little guide helps.

Happy Hacking!

– TheNappingKat

Leave a Comment

Your email address will not be published. Required fields are marked *