001package com.plivo.api.models.call;
002
003import com.fasterxml.jackson.databind.annotation.JsonSerialize;
004import com.plivo.api.models.base.Creator;
005import com.plivo.api.serializers.MapToCommaListSerializer;
006import com.plivo.api.util.Utils;
007import java.util.List;
008import java.util.Map;
009import retrofit2.Call;
010
011public class CallCreator extends Creator<CallCreateResponse> {
012
013  private String from;
014  private List<String> to;
015  private String answerUrl;
016  private String answerMethod;
017  private String ringUrl;
018  private String ringMethod;
019  private String hangupUrl;
020  private String hangupMethod;
021  private String fallbackUrl;
022  private String fallbackMethod;
023  private String callbackUrl;
024  private String callbackMethod;
025  private String callerName;
026  private String sendDigits;
027  private Boolean sendOnPreanswer;
028  private Long timeLimit;
029  private Long hangupOnRing;
030  private String machineDetection;
031  private Long machineDetectionTime;
032  private String machineDetectionUrl;
033  private String machineDetectionMethod;
034  @JsonSerialize(using = MapToCommaListSerializer.class)
035  private Map<String, String> sipHeaders;
036  private Long ringTimeout;
037  private String parentCallUuid;
038  private Boolean errorIfParentNotFound;
039
040  CallCreator(String from, List<String> to, String answerUrl) {
041    if (!Utils.allNotNull(from, to, answerUrl)) {
042      throw new IllegalArgumentException("from, to and answerUrl must not be null");
043    }
044
045    if (to.isEmpty()) {
046      throw new IllegalArgumentException("to cannot be empty");
047    }
048
049    this.from = from;
050    this.to = to;
051    this.answerUrl = answerUrl;
052  }
053
054  /**
055   * @return The phone number to be used as the caller id (with the country code).
056   */
057  public String from() {
058    return from;
059  }
060
061  /**
062   * @return The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with
063   * country code but without the + sign).
064   */
065  public List<String> to() {
066    return to;
067  }
068
069  /**
070   * @return The URL invoked by Plivo when the outbound call is answered.
071   */
072  public String answerUrl() {
073    return answerUrl;
074  }
075
076  /**
077   * @return The method used to call the answer_url. Defaults to POST.
078   */
079  public String answerMethod() {
080    return this.answerMethod;
081  }
082
083  /**
084   * @return The URL that is notified by Plivo when the call is ringing. Defaults not set.
085   */
086  public String ringUrl() {
087    return this.ringUrl;
088  }
089
090  /**
091   * @return The method used to call the ring_url. Defaults to POST.
092   */
093  public String ringMethod() {
094    return this.ringMethod;
095  }
096
097  /**
098   * @return The URL that is notified by Plivo when the call hangs up. Defaults to answer_url.
099   */
100  public String hangupUrl() {
101    return this.hangupUrl;
102  }
103
104  /**
105   * @return The method used to call the hangup_url. Defaults to POST.
106   */
107  public String hangupMethod() {
108    return this.hangupMethod;
109  }
110
111  /**
112   * @return Invoked by Plivo only if answer_url is unavailable or the XML response is invalid.
113   * Should contain a XML response.
114   */
115  public String fallbackUrl() {
116    return this.fallbackUrl;
117  }
118
119  /**
120   * @return If added, asynchronous request made to this callback_url
121   */
122  public String callbackUrl() {
123    return this.callbackUrl;
124  }
125
126  /**
127   * @return The method used to call the fallback_url. Defaults to POST.
128   */
129  public String fallbackMethod() {
130    return this.fallbackMethod;
131  }
132
133  /**
134   * @return The method used to call the callback_url. Defaults to POST.
135   */
136  public String callbackMethod() {
137    return this.callbackMethod;
138  }
139
140  /**
141   * @return Caller name to use with the call.
142   */
143  public String callerName() {
144    return this.callerName;
145  }
146
147  /**
148   * Plivo plays DTMF tones when the call is answered. This is useful when dialing a phone number
149   * and an extension. Plivo will dial the number, and when the automated system picks up, sends the
150   * DTMF tones to connect to the extension. E.g. If you want to dial the 2410 extension after the
151   * call is connected, and you want to wait for a few seconds before sending the extension, add a
152   * few leading 'w' characters. Each 'w' character waits 0.5 second before sending a digit. Each
153   * 'W' character waits 1 second before sending a digit. You can also add the tone duration in ms
154   * by appending @duration after the string (default duration is 2000 ms). For example, 1w2w3@1000
155   * See the DTMF API for additional information.
156   *
157   * @return DTMF tones to play
158   */
159  public String sendDigits() {
160    return this.sendDigits;
161  }
162
163  /**
164   * @return If set to true and send_digits is also set, digits are sent when the call is in
165   * preanswer state. Defaults to false.
166   */
167  public Boolean sendOnPreanswer() {
168    return this.sendOnPreanswer;
169  }
170
171  /**
172   * @return Schedules the call for hangup at a specified time after the call is answered.
173   */
174  public Long timeLimit() {
175    return this.timeLimit;
176  }
177
178  /**
179   * @return Schedules the call for hangup at a specified time after the call starts ringing.
180   */
181  public Long hangupOnRing() {
182    return this.hangupOnRing;
183  }
184
185  /**
186   * Used to detect if the call has been answered by a machine. The valid values are true and
187   * hangup. Default time to analyze is 5000 milliseconds (or 5 seconds). You can change it with the
188   * machine_detection_time parameter. Note that no XML is processed during the analysis phase. If a
189   * machine is detected during the call and machine_detection is set to true, the Machine parameter
190   * will be set to true and will be sent to the answer_url, hangup_url, or any other URL that is
191   * invoked by the call. If a machine is detected during the call and machine_detection is set to
192   * hangup, the call hangs up immediately and a request is made to the hangup_url with the Machine
193   * parameter set to true
194   */
195  public String machineDetection() {
196    return this.machineDetection;
197  }
198
199  /**
200   * @return Time allotted to analyze if the call has been answered by a machine.
201   */
202  public Long machineDetectionTime() {
203    return this.machineDetectionTime;
204  }
205
206  /**
207   * @return A URL where machine detection parameters will be sent by Plivo.
208   */
209  public String machineDetectionUrl() {
210    return this.machineDetectionUrl;
211  }
212
213  /**
214   * @return The HTTP method which will be used by Plivo to request the machine_detection_url.
215   * Defaults to POST.
216   */
217  public String machineDetectionMethod() {
218    return this.machineDetectionMethod;
219  }
220
221  /**
222   * @return List of SIP headers in the form of 'key=value' pairs, separated by commas.
223   */
224  public Map<String, String> sipHeaders() {
225    return this.sipHeaders;
226  }
227
228  /**
229   * @return Determines the time in seconds the call should ring.
230   */
231  public Long ringTimeout() {
232    return this.ringTimeout;
233  }
234
235  /**
236   * @return The call_uuid of the first leg in an ongoing conference call.
237   */
238  public String parentCallUuid() {
239    return this.parentCallUuid;
240  }
241
242  /**
243   * @return If set to true and the parent_call_uuid cannot be found, the API request would return
244   * an error.
245   */
246  public Boolean errorIfParentNotFound() {
247    return this.errorIfParentNotFound;
248  }
249
250  public CallCreator answerMethod(final String answerMethod) {
251    this.answerMethod = answerMethod;
252    return this;
253  }
254
255  public CallCreator ringUrl(final String ringUrl) {
256    this.ringUrl = ringUrl;
257    return this;
258  }
259
260  public CallCreator ringMethod(final String ringMethod) {
261    this.ringMethod = ringMethod;
262    return this;
263  }
264
265  public CallCreator hangupUrl(final String hangupUrl) {
266    this.hangupUrl = hangupUrl;
267    return this;
268  }
269
270  public CallCreator hangupMethod(final String hangupMethod) {
271    this.hangupMethod = hangupMethod;
272    return this;
273  }
274
275  public CallCreator fallbackUrl(final String fallbackUrl) {
276    this.fallbackUrl = fallbackUrl;
277    return this;
278  }
279
280  public CallCreator fallbackMethod(final String fallbackMethod) {
281    this.fallbackMethod = fallbackMethod;
282    return this;
283  }
284
285  public CallCreator callbackUrl(final String callbackUrl) {
286    this.callbackUrl = callbackUrl;
287    return this;
288  }
289
290  public CallCreator callbackMethod(final String callbackMethod) {
291    this.callbackMethod = callbackMethod;
292    return this;
293  }
294
295
296  public CallCreator callerName(final String callerName) {
297    this.callerName = callerName;
298    return this;
299  }
300
301  public CallCreator sendDigits(final String sendDigits) {
302    this.sendDigits = sendDigits;
303    return this;
304  }
305
306  public CallCreator sendOnPreanswer(final Boolean sendOnPreanswer) {
307    this.sendOnPreanswer = sendOnPreanswer;
308    return this;
309  }
310
311  public CallCreator timeLimit(final Long timeLimit) {
312    this.timeLimit = timeLimit;
313    return this;
314  }
315
316  public CallCreator hangupOnRing(final Long hangupOnRing) {
317    this.hangupOnRing = hangupOnRing;
318    return this;
319  }
320
321  public CallCreator machineDetection(final String machineDetection) {
322    this.machineDetection = machineDetection;
323    return this;
324  }
325
326  public CallCreator machineDetectionTime(final Long machineDetectionTime) {
327    this.machineDetectionTime = machineDetectionTime;
328    return this;
329  }
330
331  public CallCreator machineDetectionUrl(final String machineDetectionUrl) {
332    this.machineDetectionUrl = machineDetectionUrl;
333    return this;
334  }
335
336  public CallCreator machineDetectionMethod(final String machineDetectionMethod) {
337    this.machineDetectionMethod = machineDetectionMethod;
338    return this;
339  }
340
341  public CallCreator sipHeaders(final Map<String, String> sipHeaders) {
342    this.sipHeaders = sipHeaders;
343    return this;
344  }
345
346  public CallCreator ringTimeout(final Long ringTimeout) {
347    this.ringTimeout = ringTimeout;
348    return this;
349  }
350
351  public CallCreator parentCallUuid(final String parentCallUuid) {
352    this.parentCallUuid = parentCallUuid;
353    return this;
354  }
355
356  public CallCreator errorIfParentNotFound(final Boolean errorIfParentNotFound) {
357    this.errorIfParentNotFound = errorIfParentNotFound;
358    return this;
359  }
360
361
362  @Override
363  protected Call<CallCreateResponse> obtainCall() {
364    return client().getApiService().callCreate(client().getAuthId(), this);
365  }
366}