Skip to content

Instantly share code, notes, and snippets.

@aylarov
Created January 9, 2017 09:27
Show Gist options
  • Save aylarov/4e96a4520cedf24dc04fcb71f42115a8 to your computer and use it in GitHub Desktop.
Save aylarov/4e96a4520cedf24dc04fcb71f42115a8 to your computer and use it in GitHub Desktop.
Voximplant ASR example #4: voice bot
// Enable ASR module
require(Modules.ASR);
var call, asr;
// Answer inbound call
VoxEngine.addEventListener(AppEvents.CallAlerting, function (e) {
call = e.call;
call.addEventListener(CallEvents.Connected, onCallConnected);
call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
call.answer();
});
function onCallConnected(callevent) {
// create ASR instance
asr = VoxEngine.createASR(ASRLanguage.ENGLISH_US);
// Recognition result
asr.addEventListener(ASREvents.Result, function (e) {
Net.httpRequestAsync("http://botservice.xyz/input?=" + encodeURIComponent(e.text))
.then(function (result) {
// assuming that webservice returns text that bot should say
if (result.code == 200) {
call.say(result.text, Language.US_ENGLISH_FEMALE);
call.addEventListener(CallEvents.PlaybackFinished, function (e) {
call.removeEventListener(CallEvents.PlaybackFinished);
call.sendMediaTo(asr);
});
}
})
.catch(function (err) {
Logger.write(err);
});
});
// Speech captured - stop sending data to ASR
asr.addEventListener(ASREvents.SpeechCaptured, function (e) {
call.stopMediaTo(asr);
});
call.say("Hi sir, how can I help you?", Language.US_ENGLISH_FEMALE);
call.addEventListener(CallEvents.PlaybackFinished, function (e) {
call.removeEventListener(CallEvents.PlaybackFinished);
call.sendMediaTo(asr);
});
}
@dyutibhaba
Copy link

In this scenario how a ASR event is aware of the call, meaning i don't see any link between call & asr. So how asr is aware that a call is going on and it should start doing the speech recognition.

@aylarov
Copy link
Author

aylarov commented Apr 16, 2020

Because of call.sendMediaTo(asr);

@dyutibhaba
Copy link

OK thanks for the reply. In that case is it possible to add any external ASR module. Can we do something like
call.sendMediaTo(externalAsr) where externalAsr can be any asr other than voximplant ASR ?

@aylarov
Copy link
Author

aylarov commented Apr 16, 2020

You can stream media via Websockets for external recognition, see https://voximplant.com/blog/websocket-introduction

@dyutibhaba
Copy link

That looks very interesting. Thank you for quick reply.

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