【问题标题】:Chatbot answer/response time Code in jsjs中的聊天机器人回答/响应时间代码
【发布时间】:2016-08-30 23:00:50
【问题描述】:

对不起,我的英语不好,但我尽力了 :) 我有一些问题,希望我能在这里找到答案。 我想在 html/css/js 中创建一个离线聊天机器人并使用程序 Intel XDK。

  1. 我不知道,如何给聊天机器人一个特殊的命令?所以,他回答特殊的词/主题。 这是一个例子: “我:嘿” “机器人:嘿/嗨”

2.下一个问题是,在特殊单词/主题上创建响应时间。 一个例子:如果我说“嘿”,一定是聊天机器人的响应时间“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;
  }

【问题讨论】:

    标签: javascript css intel-xdk chatbot response-time


    【解决方案1】:

    实现这一点的最简单方法是设置一个包含消息、响应和超时的关联数组。喜欢:

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

    然后在fakeMessagelike 中获取您的消息

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

    作为对上述代码的改进,您可以在Fake 数组和.indexOf 上使用$.each 循环,以便对消息中的“Hi there”或“Hi Fabio”等消息做出良好响应

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 2021-08-16
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 2020-02-09
      相关资源
      最近更新 更多