var local = {}; local.avatar = "https://image.flaticon.com/icons/png/128/149/149071.png"; var remote = {}; remote.avatar = "https://developers.viber.com/images/apps/apiai-icon.png"; var accessToken = "47c6ee997d5d4d45bda02ce9432ea001"; var baseUrl = "https://api.api.ai/v1/"; function formatTime(date) { var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'PM' : 'AM'; hours = hours % 12; hours = hours ? hours : 12; // the hour '0' should be '12' minutes = minutes < 10 ? '0'+minutes : minutes; var strTime = hours + ':' + minutes + ' ' + ampm; return strTime; } function insertChat(who, text){ var control = ""; var date = formatTime(new Date()); if (who == "local"){ control = '
  • ' + '
    ' + '
    ' + '

    '+text+'

    ' + '

    '+date+'

    ' + '
    ' + '
    ' + '
  • '; }else{ control = '
  • ' + '
    ' + '
    ' + '
    ' + '

    '+ text +'

    ' + '

    '+date+'

    ' + '
    ' + '
    ' + '
  • '; } $("#messages").append(control); var objDiv = document.getElementById("messages"); objDiv.scrollTop = objDiv.scrollHeight; } $("#chat-panel").on('click',function(){ $(".innerframe").toggle(); }); function resetChat(){ $("#messages").empty(); } $(".mytext").on("keyup", function(e){ if (e.which == 13){ var text = $(this).val(); if (text !== ""){ insertChat("local", text); $(this).val(''); queryBot(text) } } }); resetChat(); function queryBot(text) { $.ajax({ type: "POST", url: baseUrl + "query?v=20150910", contentType: "application/json; charset=utf-8", dataType: "json", headers: { "Authorization": "Bearer " + accessToken }, data: JSON.stringify({ query: text, lang: "en", sessionId: "somerandomthing" }), success: function(data) { insertChat("remote",data.result.fulfillment.speech); }, error: function() { insertChat("remote","Internal Server Error"); } }); }