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.messenger;
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;
022
023public abstract class MessengerRequest extends MessageRequest {
024        protected final Messenger messenger;
025
026        protected MessengerRequest(Builder<?, ?> builder, MessageType messageType) {
027                super(builder, Channel.MESSENGER, messageType);
028                messenger = Messenger.construct(builder.category, builder.tag);
029        }
030
031        @Override
032        protected void validateSenderAndRecipient(String from, String to) {
033                for (String id : new String[]{from, to}) {
034                        if (id == null || id.isEmpty()) {
035                                throw new IllegalArgumentException("ID cannot be empty");
036                        }
037                        if (id.length() > 50) {
038                                throw new IllegalArgumentException("ID cannot be longer than 50 characters");
039                        }
040                }
041        }
042
043        @JsonProperty("messenger")
044        public Messenger getMessenger() {
045                return messenger;
046        }
047
048        @SuppressWarnings("unchecked")
049        protected abstract static class Builder<M extends MessengerRequest, B extends Builder<? extends M, ? extends B>> extends MessageRequest.Builder<M, B> {
050                protected Tag tag;
051                protected Category category;
052
053                /**
054                 * (OPTIONAL, but REQUIRED if Category is {@link Category#MESSAGE_TAG})
055                 * A tag describing the type and relevance of the 1:1 communication between your app and the end user.
056                 *
057                 * @param tag The tag.
058                 * @return This builder.
059                 */
060                public B tag(Tag tag) {
061                        this.tag = tag;
062                        return (B) this;
063                }
064
065                /**
066                 * (OPTIONAL)
067                 * The use of different category tags enables the business to send messages for different use cases.
068                 * For Facebook Messenger they need to comply with their Messaging Types policy. Vonage maps our category to
069                 * their messaging_type. If message_tag is used, then an additional tag for that type is mandatory. By
070                 * default, Vonage sends the response category to Facebook Messenger.
071                 *
072                 * @param category The category.
073                 * @return This builder.
074                 */
075                public B category(Category category) {
076                        this.category = category;
077                        return (B) this;
078                }
079        }
080}