001package com.plivo.examples.multipartycall;
002
003import com.plivo.api.Plivo;
004import com.plivo.api.exceptions.PlivoRestException;
005import com.plivo.api.exceptions.PlivoValidationException;
006import com.plivo.api.models.call.Call;
007import com.plivo.api.models.call.CallCreateResponse;
008import com.plivo.api.models.multipartycall.MultiPartyCall;
009import com.plivo.api.models.multipartycall.MultiPartyCallParticipantAddResponse;
010import com.plivo.api.models.multipartycall.MultiPartyCallUtils;
011
012import java.io.IOException;
013import java.util.Arrays;
014import java.util.Collections;
015import java.util.concurrent.TimeUnit;
016
017public class AddParticipant {
018
019  public static void main(String[] args) throws IOException, PlivoRestException, InterruptedException, PlivoValidationException {
020    Plivo.init("<YOUR-AUTH-ID>", "<YOUR-AUTH-TOKEN>");
021    startNewMPCWithCustomer("<fromNumber>", "<toNumber>");
022    startNewMPCBulkDialToAgents("<fromNumber>", "<toNumber1>", "<toNumber2>", "<toNumber3>");
023    transferCallToMPC();
024  }
025
026  private static void startNewMPCWithCustomer(String fromNumber, String toNumber) throws IOException, PlivoRestException, PlivoValidationException {
027
028    MultiPartyCallParticipantAddResponse resp = MultiPartyCall.addParticipant(MultiPartyCallUtils.friendlyName("myMPC"),
029      MultiPartyCallUtils.customer, fromNumber, Collections.singletonList(toNumber)).
030      startMpcOnEnter(false).endMpcOnExit(true).maxDuration(1500).maxParticipants(7).record(true).recordMinMemberCount(1).update();
031
032    System.out.println(resp.getMessage());
033    System.out.printf("from number matches: %s", resp.getCalls().get(0).getFrom().equals(fromNumber));
034  }
035
036  private static void startNewMPCBulkDialToAgents(String fromNumber, String... toNumber) throws IOException, PlivoRestException, PlivoValidationException {
037
038    MultiPartyCallParticipantAddResponse resp = MultiPartyCall.addParticipant(MultiPartyCallUtils.friendlyName("myBulkMPC"),
039      MultiPartyCallUtils.agent, fromNumber, Arrays.asList(toNumber)).startMpcOnEnter(false).
040      endMpcOnExit(true).maxDuration(3840).maxParticipants(5).record(false).update();
041
042    System.out.println(resp.getMessage());
043    System.out.printf("1st destination number matches: %s", resp.getCalls().get(1).getFrom().equals(fromNumber));
044    System.out.printf("2nd destination number matches: %s", resp.getCalls().get(0).getFrom().equals(fromNumber));
045  }
046
047  private static void transferCallToMPC() throws IOException, PlivoRestException, InterruptedException, PlivoValidationException {
048
049    // First let's make a call
050    CallCreateResponse resp = Call.creator("<from-number>", Collections.singletonList("<to-number>"), "<YOUR-ANSWER-URL>").create();
051
052    // Mimicking callback to occur
053    TimeUnit.SECONDS.sleep(10);
054
055    // Now transfer this to an MPC
056    MultiPartyCallParticipantAddResponse transferResp = MultiPartyCall.addParticipant(MultiPartyCallUtils.friendlyName("myTransferMPC"), MultiPartyCallUtils.supervisor, resp.getRequestUuid()).update();
057    System.out.printf("Transferred call from number matches original call: %s", transferResp.getCalls().get(0).getFrom().equalsIgnoreCase("<from-number>"));
058  }
059}