Adding Break Lines to Bot Framework while using Ja

2020-07-22 09:58发布

问题:

I use Microsoft bot Framework and deployed it to Web Chat, my bot was printing the messages in a correct format as wanted with Break Lines "\n\n", then I use the following tutorial https://github.com/microsoft/BotFramework-WebChat/blob/master/README.md to integrate JavaScript with my bot to remove the attachment icon (as shown in the figure below)

After using JavaScript and removing the attachment icon I found that Break Lines (\n) was stopped working,

Does anyone know what did happen?

The previous format (without using JavaScript):

line1

line2

The current format (using JavaScript):

line1
line2

The following code was added in "wwwroot" to remove the attachment icon:

<!DOCTYPE html>
<html lang="en-US">
<head>
    <script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
</head>
<body>
    <div id="webchat" role="main"></div>
    <script>
    (async function () {
            window.WebChat.renderWebChat(
                {
                    directLine: window.WebChat.createDirectLine({ token: "MY-TOKEN" }),
                    styleOptions: {
                        markdownRespectCRLF: true,
                        // hide upload button
                        hideUploadButton: true
                    }
                },
                document.getElementById('webchat')
            );

            document.querySelector('#webchat > *').focus();
        })().catch(err => console.error(err));
    </script>
</body>
</html>

My c# code to Send Activity:

string reply = "Line 1 \n\n Line 2";
await turnContext.SendActivityAsync(MessageFactory.Text(reply), cancellationToken);

回答1:

When you have trouble inserting a blank line in a Bot Framework channel, you can try sending a message like this to see how the channel interprets various whitespace strings: "1\n\n&nbsp;\n\n2\n\n\u00A0\n\n3\n\n\xA0\n\n4 \n5<br><br>6\n\n \n\n7 \r\n \r\n8<br/><br/>9\n&nbsp;\n10 \n11"



回答2:

Try using this:

IMessageActivity message = Activity.CreateMessageActivity();
message.Type = ActivityTypes.Message;
message.Text = "your \n text";
message.Locale = "en-Us";
message.TextFormat = TextFormatTypes.Plain;
await turnContext.SendActivityAsync(message);


回答3:

Try using this:

\n\n&nbsp;\n\n