2016-08-30 3 views
1

불쌍한 영어로 죄송합니다.하지만 최선을 다합니다. 나는 약간의 질문이 있는데 바로 대답을 찾기를 바랍니다. html/css/js에 오프라인 chatbot을 만들고 프로그램 Intel XDK를 사용하고 싶습니다.Chatbot 응답/응답 시간 js 코드

  1. 채팅 봇에 특별한 명령을 내리는 방법을 알고 있습니까? 그래서, 그는 특별한 단어/주제에 대해 대답합니다. : 다음은 예입니다 "나 : 야" "봇 : 안녕하세요/안녕하세요"

2. 다음 문제는, 특별한 단어/과목에서 응답 시간을 만들 수 있습니다. 예 : "hey"라고 말하면 봇봇 (Chatbot)의 응답 시간은 "1 분"이어야합니다.

는 I이 코드하는 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; 
    } 

답변

0

에게 메시지 응답 및 대기 시간과 연관 배열을 설정이를 달성하는 가장 간단한 방법을 사용한다. 마찬가지로 :

var Fake = { 
    "Hi": ['Hi there, I\'m Fabio and you?', 6000] 
} 

한 다음 위의 코드의 개선, 당신이에 잘 반응하는 메시지에 Fake 배열 .indexOf 각 키상의 $.each 루프를 사용할 수 있습니다

var msg = Fake[$('.message-input').val()]; 

처럼 fakeMessage 내부 메시지를받을 "Hi there"또는 "Hi Fabio"와 같은 메시지가 표시됩니다.