DialogFlow and contexts with voximplant

401 Views Asked by At

I try to set Contexts in DialogFlow with Voximplant intergration described here: https://cogint.ai/voximplant-dialogflow-connector-2019/#settingcontexts

require(Modules.AI);
const languageCode = "en-US";
const agentId = 247;
let agent,
  call,
  conversation,
  endUserParticipant,
  isConversationCreated = false,
  isCallCreated = false,
  isCallConnected = false,
  isParticipantCreated = false;


VoxEngine.addEventListener(AppEvents.Started, 

function (ev) {
  agent = new CCAI.Agent(agentId);
  agent.addEventListener(CCAI.Events.Agent.Started, () => {
    conversation = new CCAI.Conversation({ agent: agent });
    conversation.addEventListener(CCAI.Events.Conversation.Created, () => {
      isConversationCreated = true;
      createParticipant();
    });
  });
});


VoxEngine.addEventListener(AppEvents.CallAlerting, 

function (ev) {
  isCallCreated = true;
  createParticipant();
  call = ev.call;
  call.answer();
  call.addEventListener(CallEvents.Connected, 
  
  function () {
    isCallConnected = true;
    //Script whith phone number to contexts must be added here somehow. Probably in setupMedia function.
    setupMedia();
  });
  
  call.addEventListener(CallEvents.Disconnected, 
  
  function () {
    conversation.stop();
    VoxEngine.terminate();
  });
});


function createParticipant() {
  if (!isConversationCreated || !isCallCreated) return;
  endUserParticipant = conversation.addParticipant({
    call: call,
    options: { role: "END_USER" },
    dialogflowSettings: {
      lang: languageCode,
      singleUtterance: true,
      replyAudioConfig: { audioEncoding: "OUTPUT_AUDIO_ENCODING_OGG_OPUS" },
    },
  });
  endUserParticipant.addEventListener(CCAI.Events.Participant.Created, () => {
    isParticipantCreated = true;
    setupMedia();
  });
}


function setupMedia() {
  if (!isParticipantCreated || !isCallConnected) return;
  endUserParticipant.analyzeContent({
    eventInput: { name: "WELCOME", languageCode: languageCode },
  });
  endUserParticipant.addEventListener(

//Script whith phone number to contexts must be added here somehow.
 phoneContext = {
        name: "phone",
        lifespanCount: 99,
        parameters: {
            caller_id: call.callerid(),
            called_number: call.number()
        }
    },
    //endUserParticipant.setQueryParameters({contexts: [phoneContext]})
//Script whith phone number to contexts must be added here somehow.
    CCAI.Events.Participant.PlaybackFinished,
    () => {
      
//Added by and call works, but hang up      
      VoxEngine.setQueryParameters({contexts: [phoneContext]});
//Added by and call works, but hang up

      VoxEngine.sendMediaBetween(call, endUserParticipant);
    }
  );
  VoxEngine.sendMediaBetween(call, endUserParticipant);
}

The Voximplant number is forwarded to Dialogflow but after 20 seconds the voicebot become silent, but call is not terminated. I remove contexts part and the call and voicebot works as it is intended to.

What is wrong?

2

There are 2 best solutions below

1
On BEST ANSWER

I recommend using Voximplant's Modules.AI integration instead of Modules.CCAI as I used in the cogint.ai article you mentioned. Modules.CCAIis used automatically through Dialogflow's One-click integrations, but it is not as well supported beyond that from what I have seen.

They have instructions for that here and a walkthrough video here. Unfortunately the API is very different that what you have with the CCAI module, but you will find many more references and examples to that (like what I have on cogint.ai).

Modules.AI only works with Dialogflow ES.

0
On

I ended up rewrite my code. I was able to pass on the caller_id / caller_number parameters to DialogFlow not as a Contexts by script. However, I added the two variables as Contexts in my Welcome intent.

function setupMedia() {
  if (!isParticipantCreated || !isCallConnected) return;
  endUserParticipant.analyzeContent({
    eventInput: { 
                  name: "WELCOME", 
                  languageCode: languageCode, 
                  parameters: {
                                      //phone: call.callerid(),
                                      caller_id: call.callerid(),
                                      called_number: call.number()}
              },
  });
  endUserParticipant.addEventListener(
    CCAI.Events.Participant.PlaybackFinished,
    () => {
      VoxEngine.sendMediaBetween(call, endUserParticipant);
    }
  );
  VoxEngine.sendMediaBetween(call, endUserParticipant);
}