Javascript 如何通过Watson对话在屏幕上显示多条消息
我正在使用Watson技术开发聊天机器人,更确切地说是使用对话服务。这是我在Bluemix上使用javascript的第一个应用程序,我的聊天机器人的显示消息有问题。我解释:目前我有以下代码:Javascript 如何通过Watson对话在屏幕上显示多条消息,javascript,ibm-cloud,watson-conversation,Javascript,Ibm Cloud,Watson Conversation,我正在使用Watson技术开发聊天机器人,更确切地说是使用对话服务。这是我在Bluemix上使用javascript的第一个应用程序,我的聊天机器人的显示消息有问题。我解释:目前我有以下代码: var text=response.output.text[0];//仅显示第一个值 我的功能displayMessage function displayMessage(text, user) { var chat = document.getElementById('chatBox'
var text=response.output.text[0];//仅显示第一个值
我的功能displayMessage
function displayMessage(text, user) {
var chat = document.getElementById('chatBox');
var bubble = document.createElement('div');
bubble.className = 'message'; // Wrap the text first in a message class for common formatting
// Set chat bubble color and position based on the user parameter
if (user === watson) {
bubble.innerHTML = "<div class='bot'>" + text + "</div>";
} else {
bubble.innerHTML = "<div class='user'>" + text + "</div>";
}
chat.appendChild(bubble);
chat.scrollTop = chat.scrollHeight; // Move chat down to the last message displayed
document.getElementById('chatMessage').focus();
return null;
}
还有一个循环,虽然和我的声明中一样,但它不起作用
有人能帮我吗
多谢各位
编辑编号1
要通知您这是创建我的选项卡时的第一个循环:
var tab = new Array(response.output.text[0]); // array with my different text
var tmp = 1;
while(response.output.text[tmp] != undefined){
tab[tmp] = response.output.text[tmp]; // only display first value if second is null
tmp = tmp + 1;
}
我找到了一个解决方案,一个litlle savage,但它目前正在工作,所以如果有人有更好的解决方案,我会接受它 我创建了一个数组,就像在我的帖子中那样,当我安装bubber.innerHTML时,我会这样做
bubble.innerHTML = "<div class='bot'>" + tab[0] + tab[1]+ tab[2] + tab[3] + tab[4] +tab[5] + tab[6]+ tab[7]+ "</div>";
bubble.innerHTML=“”+选项卡[0]+选项卡[1]+选项卡[2]+选项卡[3]+选项卡[4]+选项卡[5]+选项卡[6]+选项卡[7]+”;
因为我的数组总是高于7,所以有一种更简单的方法。JS允许您“连接”数组中的所有元素:
text = response.output.text.join(" ");
那就应该成功了 这就是我采用的方法,尽管我认为join只适用于数组,所以我首先使用
array.isArray(text)
进行检查。看起来@cpradeil正在处理我碰巧分出的示例项目,所以这里有一个链接,指向我在displayMessage
函数中的更新。。。
text = response.output.text.join(" ");