Добавление html-ссылок на ответ чатбота - PullRequest
0 голосов
/ 04 июля 2019

я создаю чат-бота в узле js и могу получить ответ Но ссылки также отображаются в виде текста. я хочу отображать их как ссылки

 responseText = `You want to learn about ${agent.parameters.course}. 
                  Here is a link to the course: ${coupon.link}`;

Мне нужна помощь в преобразовании текста ссылки в HTML

я пробовал это `` `

     responseText = `You want to learn about ${agent.parameters.course}. 
                    Here is a link to the course: ` + `<a href='${coupon.link}'>${coupon.link}</a>`;

but this is not working .Response i am getting from chatbot is


You want to learn about chatbots. Here is a link to the course: <a href='https://www.udemy.com/user/jana-bergant'>https://www.udemy.com/user/jana-bergant</a>

**Is there any way of parsing the string (use regular expression) and when you find a link in the string, add the html tag (<a href="...) around it.?
**




Ответы [ 3 ]

0 голосов
/ 04 июля 2019

Попробуйте:

let course = 'https://www.google.com';
let coupon = 'https://www.stackoverflow.com';

const courseLink = `<a href='${course}'>${course}</a>`;
const couponLink = `<a href='${coupon}'>${coupon}</a>`;

responseText = `You want to learn about ${courseLink}. 
                  Here is a link to the course: ${couponLink}`;


document.body.innerHTML += responseText;
0 голосов
/ 06 июля 2019

На какой интеграции (Facebook Messenger, Google Assistant) вы тестируете это? Messenger должен делать ссылки кликабельными, но, к сожалению, Google Assistant не позволяет этого.

0 голосов
/ 04 июля 2019

Используйте метод Link для конвертации ссылок из строк. Например:

 var str = "This is link";
var link = str.link("https://www.somelink.com");
var msg = "Hi "+link;
document.write(msg);
 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...