001/*
002 *   Copyright 2023 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.ncco;
017
018import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
019import com.fasterxml.jackson.annotation.JsonInclude;
020import com.fasterxml.jackson.annotation.JsonProperty;
021import java.util.Arrays;
022import java.util.Collection;
023
024/**
025 * An NCCO input action which allows for the collection of digits and automatic speech recognition from a person.
026 */
027@JsonInclude(value = JsonInclude.Include.NON_NULL)
028@JsonIgnoreProperties(ignoreUnknown = true)
029public class InputAction implements Action {
030    private static final String ACTION = "input";
031
032    @JsonProperty(required = true)
033    private Collection<String> type;
034    private DtmfSettings dtmf;
035    private Collection<String> eventUrl;
036    private SpeechSettings speech;
037    private EventMethod eventMethod;
038
039    InputAction() {}
040
041    /**
042     * @param builder  builder to create InputAction object
043     */
044    private InputAction(Builder builder) {
045        type = builder.type;
046        dtmf = builder.dtmf;
047        eventUrl = builder.eventUrl;
048        eventMethod = builder.eventMethod;
049        speech = builder.speech;
050    }
051
052    @Override
053    public String getAction() {
054        return ACTION;
055    }
056
057    public Collection<String> getType(){
058        return type;
059    }
060
061    public DtmfSettings getDtmf() {
062        return dtmf;
063    }
064
065    public Collection<String> getEventUrl() {
066        return eventUrl;
067    }
068
069    public EventMethod getEventMethod() {
070        return eventMethod;
071    }
072
073    public SpeechSettings getSpeech() {
074        return speech;
075    }
076
077    public static Builder builder() {
078        return new Builder();
079    }
080
081    public static class Builder {
082        private DtmfSettings dtmf;
083        private Collection<String> eventUrl;
084        private EventMethod eventMethod;
085        private SpeechSettings speech;
086        private Collection<String> type;
087
088        Builder() {}
089
090        /**
091         * @param dtmf DTMF settings object to enable DTMF input.
092         * @return This builder to keep building the input action.
093         * @since 6.0.0
094         */
095        public Builder dtmf(DtmfSettings dtmf) {
096            this.dtmf = dtmf;
097            return this;
098        }
099
100        /**
101         * @param eventUrl Vonage sends the digits pressed by the callee to this URL after timeOut pause in activity or
102         *                 when # is pressed.
103         *
104         * @return This builder to keep building the input action.
105         */
106        public Builder eventUrl(Collection<String> eventUrl) {
107            this.eventUrl = eventUrl;
108            return this;
109        }
110
111        /**
112         * @param eventUrl Vonage sends the digits pressed by the callee to this URL after timeOut pause in activity or
113         *                 when # is pressed.
114         *
115         * @return This builder to keep building the input action.
116         */
117        public Builder eventUrl(String... eventUrl) {
118            return eventUrl(Arrays.asList(eventUrl));
119        }
120
121        /**
122         * @param eventMethod The HTTP method used to send event information to event_url The default value is POST.
123         *
124         * @return This builder to keep building the input action.
125         */
126        public Builder eventMethod(EventMethod eventMethod) {
127            this.eventMethod = eventMethod;
128            return this;
129        }
130
131        /**
132         * @param speech Automatic speech recognition settings object to enable speech input. Required if dtmf is not provided.
133         * @return This builder to keep building the input action.
134         * @since 6.0.0
135         */
136        public Builder speech(SpeechSettings speech){
137            this.speech = speech;
138            return this;
139        }
140
141        /**
142         * @param type Acceptable input type, can be set as [ "dtmf" ] for DTMF input only, [ "speech" ] for ASR only,
143         *            or [ "dtmf", "speech" ] for both.
144         * @return This builder to keep building the input action.
145         */
146        public Builder type(Collection<String> type){
147            this.type = type;
148            return this;
149        }
150
151        /**
152         * @return A new {@link InputAction} object from the stored builder options.
153         */
154        public InputAction build() {
155            return new InputAction(this);
156        }
157    }
158}