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