对不起我的英语不好,但我会尽力而为:) 我有一些问题,我希望我就在这里找到答案。 我想在 html/css/js 中创建一个离线聊天机器人并使用英特尔 XDK 程序。
2.下一个问题是,在特殊词/主题上创建响应时间。 举个例子:如果我说“嘿”,那么Chatbot的响应时间一定是“1分钟”。
我使用这个 index.js 代码
var $messages = $('.messages-content'),
d, h, m,
i = 0;
$(window).load(function() {
$messages.mCustomScrollbar();
setTimeout(function() {
fakeMessage();
}, 100);
});
function updateScrollbar() {
$messages.mCustomScrollbar("update").mCustomScrollbar('scrollTo', 'bottom', {
scrollInertia: 10,
timeout: 0
});
}
function setDate(){
d = new Date()
if (m != d.getMinutes()) {
m = d.getMinutes();
$('<div class="timestamp">' + d.getHours() + ':' + m + '</div>').appendTo($('.message:last'));
}
}
function insertMessage() {
msg = $('.message-input').val();
if ($.trim(msg) == '') {
return false;
}
$('<div class="message message-personal">' + msg + '</div>').appendTo($('.mCSB_container')).addClass('new');
setDate();
$('.message-input').val(null);
updateScrollbar();
setTimeout(function() {
fakeMessage();
}, 1000 + (Math.random() * 20) * 100);
}
$('.message-submit').click(function() {
insertMessage();
});
$(window).on('keydown', function(e) {
if (e.which == 13) {
insertMessage();
return false;
}
})
var Fake = [
'Hi there, I\'m Fabio and you?',
'Nice to meet you',
'How are you?',
'Not too bad, thanks',
'What do you do?',
'That\'s awesome',
'Codepen is a nice place to stay',
'I think you\'re a nice person',
'Why do you think that?',
'Can you explain?',
'Anyway I\'ve gotta go now',
'It was a pleasure chat with you',
'Time to make a new codepen',
'Bye',
':)'
]
function fakeMessage() {
if ($('.message-input').val() != '') {
return false;
}
实现这一点的最简单方法是设置一个包含消息、响应和超时的关联数组。喜欢:
var Fake = {
"Hi": ['Hi there, I\'m Fabio and you?', 6000]
}
然后在里面收到你的消息
fakeMessage
喜欢
var msg = Fake[$('.message-input').val()];
作为上述代码的改进,您可以在
$.each
数组上使用 Fake
循环和 .indexOf
消息中的每个键,以很好地响应“你好”或“你好法比奥”之类的消息
这里有一个聊天机器人生成器 https://htmljstemplates.com/utilities/chatbot-builder。这使您可以创建预定义的问题和答案以及建议。