Skip to content

Instantly share code, notes, and snippets.

@alkateb
Created March 21, 2018 19:30
Show Gist options
  • Save alkateb/38c5f9895b2da713757d47fd21ea513c to your computer and use it in GitHub Desktop.
Save alkateb/38c5f9895b2da713757d47fd21ea513c to your computer and use it in GitHub Desktop.
Simply copy and paste the code and put your access token key, and you would have your bot ready
<html>
<head>
<title>Javascript-Dialogflow</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
var accessToken ="INSERT YOUR ACCESS TOKEN";
var baseUrl = "https://api.dialogflow.com/v1/";
$(document).ready(function() {
$("#input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
send();
this.value = '';
}
});
$("#rec").click(function(event) {
switchRecognition();
});
});
var recognition;
function startRecognition() {
recognition = new webkitSpeechRecognition();
recognition.onstart = function(event) {
updateRec();
};
recognition.onresult = function(event) {
var text = "";
for (var i = event.resultIndex; i < event.results.length; ++i) {
text += event.results[i][0].transcript;
}
setInput(text);
stopRecognition();
};
recognition.onend = function() {
stopRecognition();
};
recognition.lang = "en-US";
recognition.start();
}
function stopRecognition() {
if (recognition) {
recognition.stop();
recognition = null;
}
updateRec();
}
function switchRecognition() {
if (recognition) {
stopRecognition();
} else {
startRecognition();
}
}
function setInput(text) {
$("#input").val(text);
send();
}
function updateRec() {
$("#rec").text(recognition ? "Stop" : "Speak");
}
function send() {
var text = $("#input").val();
conversation.push("Me: " + text + '\r\n');
$.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) {
var respText = data.result.fulfillment.speech;
console.log("Respuesta: " + respText);
setResponse(respText);
},
error: function() {
setResponse("Internal Server Error");
}
});
}
function setResponse(val) {
//Edit "AI: " to change name
conversation.push("AI: " + val + '\r\n');
$("#response").text(conversation.join(""));
}
var conversation = [];
</script>
<style type="text/css">
body { background-color: #333333; width: 500px; margin: 0 auto; text-align: center; margin-top: 20px; }
div { position: absolute; }
input { color: #000000; background-color: #00BFFF; width: 500px; }
button { color: #FFD700; background-color: #228B22; width: 100px; }
textarea { background-color: #FFD700; width: 100%; }
</style>
</head>
<body>
<div>
<input id="input" type="text"> <button id="rec">Chat</button>
<p style="font-size:36px; color:#FF4500;">Response</p>
<textarea id="response" cols="40" rows="20"></textarea>
</div>
</body>
</html>
@ameyn21
Copy link

ameyn21 commented Oct 1, 2018

how to secure this bot in case if my customer wants a subscription based model ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment