
let accessToken = "80ee6e4b839545e48d372258def2f65f",
    baseUrl = "https://api.api.ai/v1/",
    $speechInput,
    $recBtn,
    recognition,
    messageRecording = "Recording...",
    messageCouldntHear = "I couldn't hear you, could you say that again?",
    messageInternalError = "Oh no, there has been an internal server error",
    messageSorry = "I'm sorry, I don't have the answer to that yet.";

$(document).ready(function() {
  $speechInput = $("#speech");
  $recBtn = $("#rec");

  $speechInput.keypress(function(event) {
    if (event.which == 13) {
      event.preventDefault();
      send();
    }
  });

  $recBtn.on("click", function(event) {
    switchRecognition();
  });

  $(".debug__btn").on("click", function() {
    console.log('hello');
    $(this).next().toggleClass("is-active");
    return false;
  });
});

function switchRecognition() {
  if (recognition) { stopRecognition(); }
  else { startRecognition(); }
}

function startRecognition() {
  recognition = new webkitSpeechRecognition();
  recognition.continuous = false;
      recognition.interimResults = false;

  recognition.onstart = function(event) {
    respond(messageRecording);
    updateRec();
  };

  recognition.onresult = function(event) {
    recognition.onend = null;

    var text = "";
      for (var i = event.resultIndex; i < event.results.length; ++i) {
        text += event.results[i][0].transcript;
      }
      setInput(text);
    stopRecognition();
  };

  recognition.onend = function() {
    respond(messageCouldntHear);
    stopRecognition();
  };

  recognition.lang = "en-US";
  recognition.start();
}

function stopRecognition() {
  if (recognition) {
    recognition.stop();
    recognition = null;
  }
  updateRec();
}


function setInput(text) {
  $speechInput.val(text);
  send();
}

function updateRec() {
  $recBtn.text(recognition ? "Stop" : "Speak");
}

function send() {
  var text = $speechInput.val();
  $.ajax({
    type: "POST",
    url: baseUrl + "query/",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    headers: {
      "Authorization": "Bearer " + accessToken
    },
    data: JSON.stringify({sessionId: "1234", q: text, lang: "en"}),

    success: function(data) {
      prepareResponse(data);
    },
    error: function() {
      respond(messageInternalError);
    }
  });
}

function prepareResponse(val) {
  var debugJSON = JSON.stringify(val, undefined, 2),
    spokenResponse = val.result.speech;

  respond(spokenResponse);
  debugRespond(debugJSON);
}

function debugRespond(val) {
  $("#response").text(val);
}

function respond(val) {
  if (val == "") {
    val = messageSorry;
  }

  if (val !== messageRecording) {
    var msg = new SpeechSynthesisUtterance();
    msg.voiceURI = "native";
    msg.text = val;
    msg.lang = "en-US";
    window.speechSynthesis.speak(msg);
  }

  $("#spokenResponse").addClass("is-active").find(".spoken-response__text").html(val);
}

@@ -17,40 +17,100 @@
  = %22
-80ee6e4b839545e48d372258def2f65f
+c03ebe1ad04942d9952ffa04dc7f2b7e%22,%0A    accessTokenWeather= %224906bdaca138277e3ca24c69af9a2957
 %22,%0A 
@@ -888,16 +888,17 @@
 );%0A%7D);%0A%0A
+%0A
 function
@@ -1926,24 +1926,82 @@
 Speak%22);%0A%7D%0A%0A
+function clearInput() %7B%0A  return $speechInput.val('');%0A%7D%0A%0A
 function sen
@@ -2448,16 +2448,32 @@
 %7D%0A  %7D);%0A
+  clearInput();%0A
 %7D%0A%0Afunct
@@ -2552,16 +2552,18 @@
 ed, 2),%0A
+  
     spok
@@ -2594,16 +2594,16 @@
 speech;%0A
-
 %0A  respo
@@ -2981,16 +2981,16 @@
 );%0A  %7D%0A%0A
-
   $(%22#sp
@@ -3062,16 +3062,518 @@
 %22).html(val);%0A%7D%0A
+%0Afunction getWeather() %7B%0A  let text = $speechInput.val();%0A  let result =%0A%0A  $.ajax(%7B%0A    type: %22GET%22,%0A    url: %22http://api.openweathermap.org/data/2.5/%22 + %22weather/%22 + %22?q=%22 + text + %22&appid=%22 + accessTokenWeather,%0A    dataType: %22json%22,%0A    data: JSON.stringify(%5B'weather', 'description', 'main', 'temp'%5D),%0A%0A    success: function(data) %7B%0A      console.log(data);%0A      // prepareResponse(data);%0A      let response = $()%0A    %7D,%0A    error: function() %7B%0A      respond(messageInternalError);%0A    %7D%0A  %7D);%0A%7D%0A

Add weather webhook api