I am making a chatbot with apiai package from npmjs.
I have a javascript file called 'brain.js' which I have browserified so that the html browser can know what require keyword is. But the problem that I am facing is that when I want to bind that function(in js file) with dom element for example with onclick() function, my function seems not to be referenced properly. But when I am not binding it with onclick event, everything seems to work fine. I would be glad to have some suggestion from the community on how can i make my chatbot to work from html.
this is how brain.js looks:
function fun(){
var c;
var apiai=require('apiai');
const getJSON = require('simple-get-json');
var app = apiai("3dd3ae56a8094061a8fe8745dcad45a0");
var options = {
sessionId: 'UNIQE SESSION ID'
};
var request = app.textRequest("book me a good hotel",options);
request.on('response', function(response) {
if(response.result.action.match('hotel.book')){
var url='http://api.openweathermap.org/data/2.5/weather?q=';
var location='delhi';
var end='&APPID=761ddf2847a0e1dc0ff709d8e9745d72';
getJSON((url+location+end),function(data){
// console.log('the temperature in '+location+' is : '+data.main.temp);
document.addEventListener("onload",function(){document.getElementById('text').innerHTML= 'data.main.temp';})
});
}
});
request.on('error', function(error) {
// document.getElementById('demo').innerHTML='shit it is error!!!';
});
request.end();
return c;
}
I am not attaching the browserified code snippet to avoid the clumsy look that this page would have.
This is the html file that i want to run:
<!DOCTYPE html>
<html>
<head >This is starting.
<script src='bundles.js'>
</script>
</head>
<body>
<h1 id='text' >Click on this text!</h1>
<h1 id='test'>Hello here</h1>
</body>
</html>
Please let me know if something is still left unclear. I have been trying to find an alternative for so long. So any suggestion or a clean way to do this would be appreciated. Thanks in advance.