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.whatsapp;
017
018import com.vonage.client.messages.MessageType;
019import java.util.*;
020
021/**
022 * See <a href=https://developer.vonage.com/en/messages/concepts/whatsapp-product-messages>
023 * WhatsApp Product Messages documentation for details.</a>
024 *
025 * @since 7.2.0
026 */
027public final class WhatsappSingleProductRequest extends WhatsappRequest {
028
029        WhatsappSingleProductRequest(Builder builder) {
030                super(builder, MessageType.CUSTOM);
031                Map<String, Object> action = new LinkedHashMap<>(4);
032                action.put("catalog_id", Objects.requireNonNull(
033                                builder.catalogId, "Catalog ID is required."
034                ));
035                action.put("product_retailer_id", Objects.requireNonNull(
036                                builder.productRetailerId, "Product retailer ID is required."
037                ));
038                Map<String, Object> interactive = new LinkedHashMap<>(8);
039                interactive.put("type", "product");
040                if (builder.bodyText != null) {
041                        interactive.put("body", Collections.singletonMap("text", builder.bodyText));
042                }
043                if (builder.footerText != null) {
044                        interactive.put("footer", Collections.singletonMap("text", builder.footerText));
045                }
046                interactive.put("action", action);
047                custom.put("type", "interactive");
048                custom.put("interactive", interactive);
049        }
050
051        @Override
052        public Map<String, ?> getCustom() {
053                return super.getCustom();
054        }
055
056        public static Builder builder() {
057                return new Builder();
058        }
059
060        public static final class Builder extends WhatsappRequest.Builder<WhatsappSingleProductRequest, Builder> {
061                String bodyText, footerText, catalogId, productRetailerId;
062
063                Builder() {}
064
065                /**
066                 * (REQUIRED)
067                 * ID for the catalog you want to use for this message. Retrieve this ID via Commerce Manager.
068                 *
069                 * @param catalogId The catalog ID.
070                 * @return This builder.
071                 */
072                public Builder catalogId(String catalogId) {
073                        this.catalogId = catalogId;
074                        return this;
075                }
076
077                /**
078                 * (REQUIRED)
079                 * A product’s unique identifier.
080                 *
081                 * @param productRetailerId The product ID.
082                 * @return This builder.
083                 */
084                public Builder productRetailerId(String productRetailerId) {
085                        this.productRetailerId = productRetailerId;
086                        return this;
087                }
088
089                /**
090                 * (OPTIONAL)
091                 * The main message text.
092                 *
093                 * @param bodyText The body text.
094                 * @return This builder.
095                 */
096                public Builder bodyText(String bodyText) {
097                        this.bodyText = bodyText;
098                        return this;
099                }
100
101                /**
102                 * (OPTIONAL)
103                 * The text which appears at the end of the message.
104                 *
105                 * @param footerText The footer text.
106                 * @return This builder.
107                 */
108                public Builder footerText(String footerText) {
109                        this.footerText = footerText;
110                        return this;
111                }
112
113                @Override
114                public WhatsappSingleProductRequest build() {
115                        return new WhatsappSingleProductRequest(this);
116                }
117        }
118}