Created
March 10, 2020 02:04
-
-
Save chadwallacehart/d88e3fd94b50d3afd86f18f990dfa63d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(Modules.AI) | |
require(Modules.Player) | |
require(Modules.Conference) | |
let dialogflow, call, hangup, conference | |
// Globals for noise | |
let ambientPlayer = VoxEngine.createURLPlayer("https://actions.google.com/sounds/v1/ambiences/coffee_shop.ogg", { "progressivePlayback": true, "loop": true }) | |
// Inbound call processing | |
VoxEngine.addEventListener(AppEvents.CallAlerting, (e) => { | |
call = e.call | |
call.addEventListener(CallEvents.Connected, onCallConnected) | |
call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate) | |
call.answer() | |
call.record({stereo: true}) | |
}) | |
function onCallConnected(e) { | |
// Start ambient background noise with a conference | |
conference = VoxEngine.createConference() | |
ambientPlayer.sendMediaTo(conference); | |
conference.sendMediaTo(call) | |
// Create Dialogflow object | |
dialogflow = AI.createDialogflow({ | |
lang: DialogflowLanguage.ENGLISH_US | |
}) | |
dialogflow.addEventListener(AI.Events.DialogflowResponse, onDialogflowResponse) | |
// Sending WELCOME event to let the agent says a welcome message | |
dialogflow.sendQuery({ event: { name: "WELCOME", language_code: "en" } }) | |
// Playback marker used for better user experience | |
dialogflow.addMarker(-300) | |
// Start sending media from Dialogflow to the call | |
dialogflow.sendMediaTo(conference) | |
dialogflow.addEventListener(AI.Events.DialogflowPlaybackFinished, (e) => { | |
ambientPlayer.sendMediaTo(conference) | |
// Dialogflow TTS playback finished. Hangup the call if hangup flag was set to true | |
if (hangup) { | |
call.hangup() | |
VoxEngine.destroyConference(conference) | |
} | |
}) | |
dialogflow.addEventListener(AI.Events.DialogflowPlaybackMarkerReached, (e) => { | |
Logger.write("DEBUG: DialogflowPlaybackMarkerReached - sending media to Dialogflow") | |
call.sendMediaTo(dialogflow) | |
}) | |
} | |
// Handle Dialogflow responses | |
function onDialogflowResponse(e) { | |
// If DialogflowResponse with queryResult received - the call stops sending media to Dialogflow | |
// in case of response with queryResult but without responseId we can continue sending media to dialogflow | |
if (e.response.queryResult !== undefined && e.response.responseId === undefined) { | |
call.sendMediaTo(dialogflow) | |
} else if (e.response.queryResult !== undefined && e.response.responseId !== undefined) { | |
// Do whatever required with e.response.queryResult or e.response.webhookStatus | |
// If we need to hangup because end of conversation has been reached | |
if (e.response.queryResult.diagnosticInfo !== undefined && | |
e.response.queryResult.diagnosticInfo.end_conversation == true) { | |
hangup = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment