001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.messages.viber;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.messages.MessageRequest;
020import com.vonage.client.messages.Channel;
021import com.vonage.client.messages.MessageType;
022import com.vonage.client.common.E164;
023
024public abstract class ViberRequest extends MessageRequest {
025        protected final ViberService viberService;
026
027        protected ViberRequest(Builder<?, ?> builder, MessageType messageType) {
028                super(builder, Channel.VIBER, messageType);
029                viberService = ViberService.construct(
030                                builder.category, builder.ttl, builder.viberType,
031                                Action.construct(builder.actionUrl, builder.actionText),
032                                builder.duration, builder.fileSize
033                );
034        }
035
036        @Override
037        protected void validateSenderAndRecipient(String from, String to) throws IllegalArgumentException {
038                if (from == null || from.isEmpty()) {
039                        throw new IllegalArgumentException("Sender ID cannot be empty");
040                }
041                if (from.length() > 50) {
042                        throw new IllegalArgumentException("Sender ID cannot be longer than 50 characters");
043                }
044                this.to = new E164(to).toString();
045        }
046
047        @JsonProperty("viber_service")
048        public ViberService getViberService() {
049                return viberService;
050        }
051
052        @SuppressWarnings("unchecked")
053        protected abstract static class Builder<M extends ViberRequest, B extends Builder<? extends M, ? extends B>> extends MessageRequest.Builder<M, B> {
054                protected Category category;
055                protected Integer duration, fileSize, ttl;
056                protected String viberType, actionUrl, actionText;
057
058                /**
059                 * (OPTIONAL)
060                 * Sets the category tag of the message.
061                 *
062                 * @param category The Viber message category.
063                 * @return This builder.
064                 */
065                public B category(Category category) {
066                        this.category = category;
067                        return (B) this;
068                }
069
070                /**
071                 * (OPTIONAL)
072                 * Sets the time-to-live of message to be delivered in seconds. If the message is not
073                 * delivered within this time, it will be deleted. The TTL must be between 30 and
074                 * 259200 seconds (i.e. 3 days), inclusive.
075                 *
076                 * @param ttl The number of seconds the message can live undelivered before being discarded.
077                 * @return This builder.
078                 */
079                @Override
080                public B ttl(int ttl) {
081                        this.ttl = ttl;
082                        return (B) this;
083                }
084
085                /**
086                 * (OPTIONAL)
087                 * Viber-specific type definition. To use "template", please contact your Vonage Account Manager
088                 * to set up your templates. To find out more please visit the
089                 * <a href=https://www.vonage.com/communications-apis/messages/>product page</a>.
090                 *
091                 * @param type The Viber type.
092                 * @return This builder.
093                 */
094                public B viberType(String type) {
095                        this.viberType = type;
096                        return (B) this;
097                }
098
099                /**
100                 * (OPTIONAL)
101                 * A URL which is requested when the action button is clicked.
102                 *
103                 * @param actionUrl The URL as a string.
104                 * @return This builder.
105                 * @since 7.2.0
106                 */
107                protected B actionUrl(String actionUrl) {
108                        this.actionUrl = actionUrl;
109                        return (B) this;
110                }
111
112                /**
113                 * (OPTIONAL)
114                 * Text which is rendered on the action button.
115                 *
116                 * @param actionText The action button description.
117                 * @return This builder.
118                 * @since 7.2.0
119                 */
120                protected B actionText(String actionText) {
121                        this.actionText = actionText;
122                        return (B) this;
123                }
124        }
125}