最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add new line (n) in Text Response from API.ai - Stack Overflow

programmeradmin4浏览0评论

While using a Node.js module (apiai), I tried to add new lines (\n) in API.ai text response given a query result but it doesn't seem to work when I save the response in a variable from the callback like this:

request.on('response', function (response) {
  var textResponse = response.result.fulfillment.speech;
  // ...
})

While using a Node.js module (apiai), I tried to add new lines (\n) in API.ai text response given a query result but it doesn't seem to work when I save the response in a variable from the callback like this:

request.on('response', function (response) {
  var textResponse = response.result.fulfillment.speech;
  // ...
})
Share Improve this question asked Jul 2, 2017 at 5:43 Arafat HusaynArafat Husayn 692 silver badges13 bronze badges 7
  • how you tried? after adding \n what you have to do with that? – Dinesh undefined Commented Jul 2, 2017 at 5:48
  • It gave me the \n literally. I tried by sending it to Facebook Messenger directly. It shows like this: sample text \n sample text but it should take a new line there. – Arafat Husayn Commented Jul 2, 2017 at 5:52
  • i answered check my answer. – Dinesh undefined Commented Jul 2, 2017 at 5:56
  • try \u000A instead <\br> – Dinesh undefined Commented Jul 2, 2017 at 6:11
  • No luck with that too. – Arafat Husayn Commented Jul 2, 2017 at 6:19
 |  Show 2 more ments

4 Answers 4

Reset to default 2

Finally I solved it like this:

var textResponse = response.result.fulfillment.speech;
textResponse = textResponse.replace(/\\n/g, '\n');

The input was like: I'm a chatbot. \n built with ❤

use Break-line <\br> instead of \n

For example:

<div>This is with break line<br>this is after break line</div>
<div>This is with break line \n this is after new line</div>

I was using the agent.add() method in the dialogflow inline editor and wanted a newline between some text. so I used " \n"( Two spaces followed by \n), and it gave the output as required on my dialogflow messenger integration.

eg.

agent.add(firstName + " " + lastName + " \n" + phoneno);

When entering a response, press Shift + Enter to input a newline or just Enter to input another text response.

The following limitations apply:

Max of 300 text entries per text response

发布评论

评论列表(0)

  1. 暂无评论