001package com.plivo.api.models.message; 002 003import com.fasterxml.jackson.annotation.JsonProperty; 004import com.fasterxml.jackson.databind.annotation.JsonSerialize; 005import com.fasterxml.jackson.databind.ObjectMapper; 006import com.plivo.api.models.base.Creator; 007import com.plivo.api.serializers.DelimitedListSerializer; 008import com.plivo.api.util.Utils; 009import java.net.URL; 010import java.util.Collections; 011import java.util.List; 012import retrofit2.Call; 013 014/** 015 * Represents an instance of a message created on PlivoClient. 016 */ 017public class MessageCreator extends Creator < MessageCreateResponse > { 018 019 @JsonProperty("src") 020 private String source; 021 @JsonSerialize(using = DelimitedListSerializer.class) 022 @JsonProperty("dst") 023 private final List < String > destination; 024 private String text; 025 @JsonProperty("powerpack_uuid") 026 private String powerpackUUID; 027 private MessageType type = null; 028 private URL url = null; 029 private String method = "POST"; 030 private Boolean log = null; 031 private Boolean trackable = null; 032 private String[] media_urls = null; 033 private String[] media_ids = null; 034 private Long message_expiry; 035 private String dlt_entity_id; 036 private String dlt_template_id; 037 private String dlt_template_category; 038 @JsonProperty("template") 039 private Template template; 040 041 /** 042 * @param source The phone number that will be shown as the sender ID. 043 * @param destination The numbers to which the message will be sent in string format. 044 * @param text The text message that will be sent. 045 */ 046 047 MessageCreator(String source, String destination, String text) { 048 if (!Utils.allNotNull(source, destination)) { 049 throw new IllegalArgumentException("source, destination must not be null"); 050 } 051 if (destination.equals(source)) { 052 throw new IllegalArgumentException("destination cannot include source"); 053 } 054 if (source.length()<= 14) { 055 this.source = source; 056 this.destination = Collections.singletonList(destination); 057 this.text = text; 058 } else { 059 this.powerpackUUID = source; 060 this.destination = Collections.singletonList(destination); 061 this.text = text; 062 } 063 064 } 065 066 /** 067 * @param source The phone number that will be shown as the sender. 068 * @param destination The numbers to which the message will be sent. 069 */ 070 MessageCreator(String source, String destination) { 071 if (!Utils.allNotNull(source, destination)) { 072 throw new IllegalArgumentException("source, destination must not be null"); 073 } 074 if (destination.equals(source)) { 075 throw new IllegalArgumentException("destination cannot include source"); 076 } 077 this.source = source; 078 this.destination = Collections.singletonList(destination); 079 } 080 081 /** 082 * @param source The phone number that will be shown as the sender ID. 083 * @param destination The numbers to which the message will be sent. 084 * @param text The text message that will be sent. 085 */ 086 MessageCreator(String source, List < String > destination, String text) { 087 if (!Utils.allNotNull(source, destination)) { 088 throw new IllegalArgumentException("source, destination must not be null"); 089 } 090 091 if (destination.contains(source)) { 092 throw new IllegalArgumentException("destination cannot include source"); 093 } 094 095 this.source = source; 096 this.destination = destination; 097 this.text = text; 098 } 099 100 /** 101 * @param destination The numbers to which the message will be sent. 102 * @param text The text message that will be sent. 103 * @param powerpackUUID The powerpack UUID to be used. 104 */ 105 MessageCreator(List < String > destination, String text, String powerpackUUID) { 106 if (!Utils.allNotNull(powerpackUUID, destination)) { 107 throw new IllegalArgumentException("powerpack uuid, destination and text must not be null"); 108 } 109 this.destination = destination; 110 this.text = text; 111 this.powerpackUUID = powerpackUUID; 112 } 113 114 public String source() { 115 return this.source; 116 } 117 118 public List < String > destination() { 119 return this.destination; 120 } 121 122 public String text() { 123 return this.text; 124 } 125 126 public MessageType type() { 127 return this.type; 128 } 129 130 public URL url() { 131 return this.url; 132 } 133 134 public String method() { 135 return this.method; 136 } 137 138 public Boolean log() { 139 return this.log; 140 } 141 142 public String[] media_urls() { 143 return this.media_urls; 144 } 145 146 public String[] media_ids() { 147 return this.media_ids; 148 } 149 150 public Long message_expiry() { 151 return this.message_expiry; 152 } 153 154 public String dlt_entity_id() { 155 return this.dlt_entity_id; 156 } 157 158 public String dlt_template_id() { 159 return this.dlt_template_id; 160 } 161 162 public String dlt_template_category() { 163 return this.dlt_template_category; 164 } 165 166 /** 167 * @param type Must be {@link MessageType#SMS} 168 */ 169 public MessageCreator type(final MessageType type) { 170 this.type = type; 171 return this; 172 } 173 174 /** 175 * @param url The URL to which with the status of the message is sent. 176 */ 177 public MessageCreator url(final URL url) { 178 this.url = url; 179 return this; 180 } 181 182 /** 183 * @param method The method used to call the url. Defaults to POST. 184 */ 185 public MessageCreator method(final String method) { 186 this.method = method; 187 return this; 188 } 189 190 /** 191 * @param log If set to false, the content of this message will not be logged on the Plivo 192 * infrastructure and the dst value will be masked 193 */ 194 public MessageCreator log(final Boolean log) { 195 this.log = log; 196 return this; 197 } 198 199 /** 200 * @param trackable 201 */ 202 public MessageCreator trackable(final Boolean trackable) { 203 this.trackable = trackable; 204 return this; 205 } 206 /** 207 + * @param media_url The media url is used to send media for MMS. 208 + */ 209 public MessageCreator media_urls(final String[] media_urls) { 210 this.media_urls = media_urls; 211 return this; 212 } 213 214 /** 215 + * @param media_ids The media ids is used to send media for MMS. 216 + */ 217 public MessageCreator media_ids(final String[] media_ids) { 218 this.media_ids = media_ids; 219 return this; 220 } 221 222 //@param message_expiry used to set message expiry. 223 public MessageCreator message_expiry(final Long message_expiry) { 224 this.message_expiry = message_expiry; 225 return this; 226 } 227 228 /** 229 * @param dlt_entity_id This is the DLT entity id passed in the message request. 230 */ 231 public MessageCreator dlt_entity_id(final String dlt_entity_id) { 232 this.dlt_entity_id = dlt_entity_id; 233 return this; 234 } 235 236 /** 237 * @param dlt_template_id This is the DLT template id passed in the message request. 238 */ 239 public MessageCreator dlt_template_id(final String dlt_template_id) { 240 this.dlt_template_id = dlt_template_id; 241 return this; 242 } 243 244 /** 245 * @param dlt_template_category This is the DLT template category passed in the message request. 246 */ 247 public MessageCreator dlt_template_category(final String dlt_template_category) { 248 this.dlt_template_category = dlt_template_category; 249 return this; 250 } 251 252 /** 253 * @param template_json_string This is the template passed as a json string in the whatsapp message request. 254 */ 255 public MessageCreator template_json_string(final String template_json_string) { 256 if (this.type == null) { 257 this.type = MessageType.WHATSAPP; 258 } else { 259 if (this.type.equals(MessageType.SMS) || (this.type.equals(MessageType.MMS))) 260 throw new IllegalArgumentException("type parameter should be whatsapp"); 261 } 262 if (Utils.allNotNull(this.template)) { 263 throw new IllegalArgumentException("template parameter is already set"); 264 } 265 try { 266 ObjectMapper objectMapper = new ObjectMapper(); 267 Template temp = objectMapper.readValue(template_json_string, Template.class); 268 if (temp.getName() == null || temp.getName().isEmpty()) { 269 throw new IllegalArgumentException("template name must not be null or empty"); 270 } 271 if (temp.getLanguage() == null || temp.getLanguage().isEmpty()) { 272 throw new IllegalArgumentException("template language must not be null or empty"); 273 } 274 this.template = temp; 275 } catch (Exception e) { 276 e.printStackTrace(); 277 throw new IllegalArgumentException("failed to read template"); 278 } 279 return this; 280 } 281 282 /** 283 * @param temp This is the template passed as a template object in the whatsapp message request. 284 */ 285 public MessageCreator template(final Template temp) { 286 if (type == null) { 287 this.type = MessageType.WHATSAPP; 288 } else { 289 if (type.equals(MessageType.SMS) || (type.equals(MessageType.MMS))) 290 throw new IllegalArgumentException("type parameter should be whatsapp"); 291 } 292 if (Utils.allNotNull(this.template)) { 293 throw new IllegalArgumentException("template parameter is already set"); 294 } 295 if (temp.getName() == null || temp.getName().isEmpty()) { 296 throw new IllegalArgumentException("template name must not be null or empty"); 297 } 298 if (temp.getLanguage() == null || temp.getLanguage().isEmpty()) { 299 throw new IllegalArgumentException("template language must not be null or empty"); 300 } 301 this.template = temp; 302 303 return this; 304 } 305 306 @Override 307 protected Call < MessageCreateResponse > obtainCall() { 308 return client().getApiService().messageSend(client().getAuthId(), this); 309 } 310}