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.voice;
017
018import com.fasterxml.jackson.annotation.*;
019import com.vonage.client.JsonableBaseObject;
020
021/**
022 * Configure the behavior of Vonage's advanced machine detection. See
023 * <a href=https://developer.vonage.com/en/voice/voice-api/concepts/advanced-machine-detection>
024 * the documentation</a> for details.
025 *
026 * @since 7.4.0
027 */
028public class AdvancedMachineDetection extends JsonableBaseObject {
029
030        /**
031         * Represents the beep detection mode.
032         */
033        public enum Mode {
034                /**
035                 * Detect machine and send a status human / machine webhook.
036                 */
037                DETECT,
038
039                /**
040                 * Detect machine and send back a status human / machine webhook, but also when machine is detected, attempt
041                 * to detect voice mail beep and send back another status machine webhook with {@code sub_state: beep_start}.
042                 */
043                DETECT_BEEP,
044
045                /**
046                 * Asynchronously start processing NCCO actions during the detection phase.
047                 *
048                 * @since 8.2.0
049                 */
050                DEFAULT;
051
052                @JsonCreator
053                public static Mode fromString(String value) {
054                        try {
055                                return Mode.valueOf(value.toUpperCase());
056                        }
057                        catch (NullPointerException | IllegalArgumentException ex) {
058                                return null;
059                        }
060                }
061
062                @JsonValue
063                @Override
064                public String toString() {
065                        return name().toLowerCase();
066                }
067        }
068
069        private MachineDetection behavior;
070        private Mode mode;
071        private Integer beepTimeout;
072
073        AdvancedMachineDetection() {}
074
075        AdvancedMachineDetection(Builder builder) {
076                behavior = builder.behavior;
077                mode = builder.mode;
078                if ((beepTimeout = builder.beepTimeout) != null && (beepTimeout < 45 || beepTimeout > 120)) {
079                        throw new IllegalArgumentException("Beep timeout must be between 45 and 120 seconds.");
080                }
081        }
082
083        /**
084         * Defines how the system responds when a machine is detected.
085         * When {@link MachineDetection#HANGUP} is used, the call will be terminated if a machine is detected.
086         * When {@link MachineDetection#CONTINUE} is used, the call will continue even if a machine is detected.
087         *
088         * @return The machine detection behaviour as an enum.
089         */
090        @JsonProperty("behavior")
091        public MachineDetection getBehavior() {
092                return behavior;
093        }
094
095        /**
096         * Detect if machine answered and sends a human or machine status in the webhook payload. When set to
097         * {@link Mode#DETECT_BEEP}, the system also attempts to detect voice mail beep and sends an additional
098         * parameter {@code sub_state} in the webhook with the value {@code beep_start}.
099         *
100         * @return The machine detection mode.
101         */
102        @JsonProperty("mode")
103        public Mode getMode() {
104                return mode;
105        }
106
107        /**
108         * Maximum time in seconds Vonage should wait for a machine beep to be detected. A machine event with
109         * {@code sub_state} set to {@code beep_timeout} will be sent if the timeout is exceeded.
110         *
111         * @return The maximum wait time in seconds for machine detection, or {@code null} if unset.
112         */
113        @JsonProperty("beep_timeout")
114        public Integer getBeepTimeout() {
115                return beepTimeout;
116        }
117
118        /**
119         * Entry point for constructing an instance of this class.
120         *
121         * @return A new builder.
122         */
123        public static Builder builder() {
124                return new Builder();
125        }
126
127        /**
128         * Builder for specifying the Advanced Machine Detection properties.
129         */
130        public static class Builder {
131                private MachineDetection behavior;
132                private Mode mode;
133                private Integer beepTimeout;
134
135                Builder() {}
136
137                /**
138                 * Define how the system responds when a machine is detected.
139                 * When {@link MachineDetection#HANGUP} is used, the call will be terminated if a machine is detected.
140                 * When {@link MachineDetection#CONTINUE} is used, the call will continue even if a machine is detected.
141                 *
142                 * @param behavior The machine detection behaviour as an enum.
143                 *
144                 * @return This builder.
145                 */
146                public Builder behavior(MachineDetection behavior) {
147                        this.behavior = behavior;
148                        return this;
149                }
150
151                /**
152                 * Detect if machine answered and sends a human or machine status in the webhook payload. When set to
153                 * {@link Mode#DETECT_BEEP}, the system also attempts to detect voice mail beep and sends an additional
154                 * parameter {@code sub_state} in the webhook with the value {@code beep_start}.
155                 *
156                 * @param mode The machine detection mode enum.
157                 *
158                 * @return This builder.
159                 */
160                public Builder mode(Mode mode) {
161                        this.mode = mode;
162                        return this;
163                }
164
165                /**
166                 * Maximum time in seconds Vonage should wait for a machine beep to be detected. A machine event with
167                 * {@code sub_state} set to {@code beep_timeout} will be sent if the timeout is exceeded.
168                 *
169                 * @param beepTimeout The beep timeout in seconds as an integer.
170                 *
171                 * @return This builder.
172                 */
173                public Builder beepTimeout(int beepTimeout) {
174                        this.beepTimeout = beepTimeout;
175                        return this;
176                }
177
178                /**
179                 * Constructs the AdvancedMachineDetection object.
180                 *
181                 * @return A new AdvancedMachineDetection instance with this builder's properties.
182                 */
183                public AdvancedMachineDetection build() {
184                        return new AdvancedMachineDetection(this);
185                }
186        }
187}