2

Sorry for my bad English but I give my best :) I have some question and I hope I'm right here to find a answer. I want to create a offline chatbot in html/css/js and use the Program Intel XDK.

  1. I dont know, how to give the chatbot a special command ? So, that he answer on special words/subjects. Here's an example: "Me: hey" "Bot: hey/hi"

2.The next problem is, to create a response time at special words/subjects. An example: If I say "hey", must be the response time from Chatbot of "1min".

I use this index.js code

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;
  }
Tony
  • 21
  • 4

1 Answers1

0

The most simple way of achieving this is set an associative array with messages, responses and timeouts. Like:

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

And then get your message inside fakeMessage like

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

As improvement of above code, you may use an $.each loop on Fake array and .indexOf each key into your message to respond well to messages like "Hi there" or "Hi Fabio"

Denis Sheremet
  • 2,453
  • 2
  • 18
  • 34