问题描述
我正在使用Watson技术(更确切地说是通过对话服务)开发聊天机器人.这是我在Bluemix上使用javascript的第一个应用程序,并且我的聊天机器人的显示消息有问题.我解释:目前我有这个代码:
I'm working on a chatbot with Watson technology and more precisely with conversation service. It's my first application on Bluemix and with javascript and i have an issue with the display message of my chatbot. I explain: currently I have this code :
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;
}
当前的问题是该函数仅显示我的第一个值,但是在某些情况下,我必须显示n条消息(n始终< 10)因此,我考虑创建一个array [10]来用 while(array [i]!= undefined)
存储我的消息.但是我的错误是函数displayMessage中的显示-我不知道如何显示多条消息.我试过了:
The problem currently is that the function only displays my first value, but in some cases I have to display n message (n is always < 10)So I think about the creation of an array[10] to store my message with while (array[i] != undefined)
. But my error is with the display in the function displayMessage - I don't know how display several messages. I tried this:
bubble.innerHTML = "<div class='bot'>" + tab[tmp] + "</div>";
和一个循环,虽然与我的声明相同,但没有用.
and a loop while same as in my declaration but it didn't work.
有人可以帮助我吗?
谢谢
编辑第1个通知您,这是我创建标签页时的第一个循环:
Edit n°1To inform you this is my first loop while creating my tab :
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;
}
推荐答案
有一种更简单的方法.JS允许您连接"数组中的所有元素:
There is a much easier approach. JS allows you to 'join' all the elements in an array:
text = response.output.text.join(" ");
那应该可以解决问题!
这篇关于如何使用Watson Conversation在屏幕上显示几条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!