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

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