Search code examples
node.jsbotframework

Sending List Items in Bot Framework sdk v4 nodeJS


So I have this code : if(channelData.message.text == "test") { await stepContext.context.sendActivity("1. Who \n\r 2. Am \n\r 3. I "); }

Which gives me a message (thru FB Messenger Channel) in the image below:

message result

Question: Is there any template/method in bot framewwork sdk 4 (I'm using node JS) to print it in a clean list format? (removing that weird newline between 2. and 3.) Also, what if I have let c= ["Who", "am", "I"] is the any way to print it in a list format through .sendActivity? Thanks!


Solution

  • To make your list propagate correctly simply remove the new line character (\n) and the spaces in between the preceeding text and the return character (\r), like so: await stepContext.context.sendActivity('1. Who \r2. Am \r3. I');. After that, it will format as you wish.

    As for iterating thru an array of strings and formatting as a list in a single activity, the following will work:

    const msgArray = ['Who', 'Am', 'I'];
    const message = MessageFactory.text('');
    msgArray.forEach((val, index) => {
        if (!message.text) {
            message.text = `${ index + 1 }. ${ val }\r`;
        } else if (message.text) {
            console.log(message.text);
            message.text = message.text.concat(`${ index + 1 }. ${ val }\r`);
        }
    });
    return await stepContext.context.sendActivity(message);
    

    Both of the above methods produced the same outcome shown below for me.

    enter image description here

    Hope of help!