Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Uncaught TypeError: Cannot read property 'intent' of undefined #24

Closed
alxferraz opened this issue Jan 25, 2019 · 6 comments
Closed

Uncaught TypeError: Cannot read property 'intent' of undefined #24

alxferraz opened this issue Jan 25, 2019 · 6 comments

Comments

@alxferraz
Copy link

I just tried to run the example as described in quickstart.md but i get this error:

Uncaught TypeError: Cannot read property 'intent' of undefined
at a.handleResult (microphone.min.js:2)
at a.result (microphone.min.js:2)
at a.fsm (microphone.min.js:2)
at WebSocket. (microphone.min.js:2)

can anybody help me out here?

@aadhi0319
Copy link

I am experiencing the same issue.

@7bamboo
Copy link

7bamboo commented May 6, 2019

@alxferraz @aadhi0319 Any luck with this? Getting the same issue

@aadhi0319
Copy link

@7bamboo I got it to work after a lot of suffering but I can't remember exactly what I changed to get it to work as I did this a while back. Do you start and stop the microphone? Also make sure the microphone doesn't automatically disconnect. If those suggestions don't work I'd be happy to compare your code to mine to see where the implementation is different. Good luck!

@aforaleka
Copy link
Contributor

latest release should fix it. this microphone repo is not being actively maintained however, as it is still using our legacy API. You might need to change the code a bit to support parsing the new API response.

@aadhi0319
Copy link

@aforaleka Thank you for the response. I did a git pull for the microphone repository and I still get the error. Are you saying that in order to fix this issue I must switch from using microphone commands to using the Wit HTTP API (https://wit.ai/docs/http/20170307)?

@aadhi0319
Copy link

@alxferraz @7bamboo I modified the microphone.min.js file to fix the issue. Replace microphone/js/microphone.min.js content with the following:

(function(){var a,b,c,d,e;navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia,window.AudioContext=window.AudioContext||window.webkitAudioContext||window.mozAudioContext||window.msAudioContext,function(){var a,b,c,d,e,f,g,h;for(e=window,h=["ms","moz","webkit","o"],f=0,g=h.length;f<g&&(d=h[f],!e.requestAnimationFrame);f++)e.requestAnimationFrame=e[d+"RequestAnimationFrame"],e.cancelAnimationFrame=e[d+"CancelAnimationFrame"]||e[d+"CancelRequestAnimationFrame"];if(e.requestAnimationFrame){if(e.cancelAnimationFrame)return;return a=e.requestAnimationFrame,b={},e.requestAnimationFrame=function(c){var d;return d=a(function(a){return d in b?delete b[d]:c(a)})},e.cancelAnimationFrame=function(a){return b[a]=!0}}c=0,e.requestAnimationFrame=function(a){var b;return c=Math.max(c+16,b=+new Date),e.setTimeout(function(){return a(+new Date)},c-b)},e.cancelAnimationFrame=function(a){return clearTimeout(a)}}(),d=("undefined"!=typeof localStorage&&null!==localStorage?localStorage.getItem:void 0)&&localStorage.getItem("wit_debug")?function(){return console.log.apply(console,arguments)}:function(){},c=function(a,b){return this.name="WitError",this.message=a||"",this.infos=b,this},c.prototype=Error.prototype,b=localStorage.getItem("wit_ws")||"wss://ws.wit.ai/speech_ws",a=function(a){var b,c;return this.conn=null,this.ctx=new AudioContext,this.state="disconnected",this.rec=!1,this.handleError=function(a){var b,c;if(_.isFunction(c=this.onerror))return b=_.isString(a)?a:_.isString(a.message)?a.message:"Something went wrong!",c.call(window,b,a)},this.handleResult=function(a){var b,c,d;if(_.isFunction(c=this.onresult))return c.call(window,a)},a&&(this.elem=a,a.innerHTML="<div class='mic mic-box icon-wit-mic'>\n</div>\n<svg class='mic-svg mic-box'>\n</svg>",a.className+=" wit-microphone",a.addEventListener("click",function(a){return function(b){return a.fsm("toggle_record")}}(this)),c=this.elem.children[1],b="http://www.w3.org/2000/svg",this.path=document.createElementNS(b,"path"),this.path.setAttribute("stroke","#eee"),this.path.setAttribute("stroke-width","5"),this.path.setAttribute("fill","none"),c.appendChild(this.path)),this.rmactive=function(){if(this.elem)return this.elem.classList.remove("active")},this.mkactive=function(){if(this.elem)return this.elem.classList.add("active")},this.mkthinking=function(){var a,b,d,e,f,g,h,i,j,k,l,m,n;if(this.thinking=!0,this.elem)return i=getComputedStyle(c),this.elem.classList.add("thinking"),l=parseInt(i.width,10),f=parseInt(i.height,10),b="border-box"===i.boxSizing?parseInt(i.borderTopWidth,10):0,g=l/2-b-5,a=1e3,d=l/2-b,e=f/2-b-g,m=0,j=1,h=(null!=(n=window.performance)?n.now():void 0)||new Date,k=function(c){return function(i){var n,o,p,q;return o=(i-h)%a/a*2*Math.PI-Math.PI/2,p=Math.cos(o)*g+l/2-b,q=Math.sin(o)*g+f/2-b,n=+(1.5*Math.PI>o&&o>Math.PI/2),c.path.setAttribute("d","M"+d+","+e+"A"+g+","+g+","+m+","+n+","+j+","+p+","+q),c.thinking?requestAnimationFrame(k):(c.elem.classList.remove("thinking"),c.path.setAttribute("d","M0,0"))}}(this),requestAnimationFrame(k)},this.rmthinking=function(){return this.thinking=!1},this},e={disconnected:{connect:function(a){var c,e;return a||this.handleError("No token provided"),c=new WebSocket(b),c.onopen=function(b){return function(b){var e;return d("connection opened",b),e={token:a,bps:16,encoding:"signed-integer"},c.send(JSON.stringify(["auth",e]))}}(),c.onclose=function(a){return function(b){return a.fsm("socket_closed")}}(this),c.onmessage=function(a){return function(b){var c,d,e;return e=JSON.parse(b.data),d=e[0],c=e[1],c?a.fsm.call(a,d,c):a.fsm.call(a,d)}}(this),this.conn=c,e=function(a){return function(b){var c,e,f;return c=a.ctx,f=c.createMediaStreamSource(b),e=(c.createScriptProcessor||c.createJavascriptNode).call(c,4096,1,1),e.onaudioprocess=function(b){var c,e,f,g,h,i,j,k;if(a.rec){for(c=b.inputBuffer,e=c.getChannelData(0),h=e.length,g=new Int16Array(h),f=k=0;0<=h?k<=h:k>=h;f=0<=h?++k:--k)i=e[f],j=i<0?32768*i:32767*i,g[f]=j;return d("[audiobuffer] rate="+c.sampleRate+", samples="+h+", bytes="+g.byteLength),a.conn.send(g)}},f.connect(e),e.connect(c.destination),a.stream=b,a.proc=e,a.src=f,a.fsm("got_stream")}}(this),navigator.getUserMedia({audio:!0},e,this.handleError),"connecting"}},connecting:{"auth-ok":function(){return"waiting_for_stream"},got_stream:function(){return"waiting_for_auth"},error:function(a){return this.handleError(a),"connecting"},socket_closed:function(){return"disconnected"}},waiting_for_auth:{"auth-ok":function(){return"ready"}},waiting_for_stream:{got_stream:function(){return"ready"}},ready:{socket_closed:function(){return"disconnected"},timeout:function(){return"ready"},start:function(){return this.fsm("toggle_record")},toggle_record:function(){return this.conn.send(JSON.stringify(["start",this.context||{}])),this.rec=!0,this.ctx||console.error("No context"),this.stream||console.error("No stream"),this.src||console.error("No source"),this.proc||console.error("No processor"),"audiostart"}},audiostart:{error:function(a){return this.rec=!1,this.handleError(new c("Error during recording",{code:"RECORD",data:a})),"ready"},socket_closed:function(){return this.rec=!1,"disconnected"},stop:function(){return this.fsm("toggle_record")},toggle_record:function(){return this.rec=!1,this.conn.send(JSON.stringify(["stop"])),this.timer=setTimeout(function(a){return function(){return a.fsm("timeout")}}(this),6e4),"audioend"}},audioend:{socket_closed:function(){return this.timer&&clearTimeout(this.timer),"disconnected"},timeout:function(){return this.handleError(new c("Wit timed out",{code:"TIMEOUT"})),"ready"},error:function(a){return this.timer&&clearTimeout(this.timer),this.handleError(new c("Wit did not recognize intent",{code:"RESULT",data:a})),"ready"},result:function(a){return this.timer&&clearTimeout(this.timer),this.handleResult(a),"ready"}}},a.prototype.fsm=function(a){var b,c,f,g;if(c=null!=(g=e[this.state])?g[a]:void 0,b=Array.prototype.slice.call(arguments,1),_.isFunction(c))switch(f=c.apply(this,b),d("fsm: "+this.state+" + "+a+" -> "+f,b),this.state=f,"audiostart"!==f&&"audioend"!==f&&"ready"!==f&&"connecting"!==f&&"disconnected"!==f||_.isFunction(c=this["on"+f])&&c.call(window),f){case"disconnected":case"ready":this.rmthinking(),this.rmactive();break;case"audiostart":this.mkactive();break;case"audioend":this.mkthinking(),this.rmactive()}else d("fsm error: "+this.state+" + "+a,b);return f},a.prototype.connect=function(a){return this.fsm("connect",a)},a.prototype.start=function(){return this.fsm("start")},a.prototype.stop=function(){return this.fsm("stop")},a.prototype.setContext=function(a){var b;this.context||(this.context={});for(b in a)a[b],this.context[b]=a[b];return d("context: ",this.context),null},window._||(window._={}),_.isFunction||(_.isFunction=function(a){return"function"==typeof a}),_.isString||(_.isString=function(a){return"[object String]"===toString.call(a)}),window.Wit||(window.Wit={}),Wit.Microphone=a}).call(this);

And use this to parse results:

this.listen = new Wit.Microphone();
this.listen.connect("CLIENT API KEY");
this.listen.onresult = function (resp) {
	console.log(resp);
}

Hope this helps.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants