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.ncco;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import com.vonage.client.voice.TextToSpeechLanguage;
021
022/**
023 * An NCCO talk action which allows for synthesized speech to be sent to a call.
024 */
025public class TalkAction extends JsonableBaseObject implements Action {
026    private static final String ACTION = "talk";
027
028    private String text;
029    private Boolean bargeIn;
030    private Integer loop, style;
031    private Float level;
032    private TextToSpeechLanguage language;
033    private Boolean premium;
034
035    TalkAction() {}
036
037    private TalkAction(Builder builder) {
038        this.text = builder.text;
039        this.bargeIn = builder.bargeIn;
040        this.loop = builder.loop;
041        this.level = builder.level;
042        this.style = builder.style;
043        this.language = builder.language;
044        this.premium = builder.premium;
045    }
046
047    @Override
048    public String getAction() {
049        return ACTION;
050    }
051
052    @JsonProperty("text")
053    public String getText() {
054        return text;
055    }
056
057    @JsonProperty("bargeIn")
058    public Boolean getBargeIn() {
059        return bargeIn;
060    }
061
062    @JsonProperty("loop")
063    public Integer getLoop() {
064        return loop;
065    }
066
067    @JsonProperty("level")
068    public Float getLevel() {
069        return level;
070    }
071
072    @JsonProperty("language")
073    public TextToSpeechLanguage getLanguage() {
074        return language;
075    }
076
077    @JsonProperty("style")
078    public Integer getStyle() {
079        return style;
080    }
081
082    @JsonProperty("premium")
083    public Boolean getPremium() {
084        return premium;
085    }
086
087    /**
088     * @param text A string of up to 1,500 characters (excluding SSML tags) containing the message to be
089     *             synthesized in the Call or Conversation. A single comma in text adds a short pause to the
090     *             synthesized speech. To add a longer pause a break tag needs to be used in SSML.
091     *             <p>
092     *             To use SSML tags, you must enclose the text in a speak element.
093     * @return A new {@linkplain Builder} with the text field initialised.
094     */
095    public static Builder builder(String text) {
096        return new Builder(text);
097    }
098
099    public static class Builder {
100        private String text;
101        private Boolean bargeIn, premium;
102        private Integer loop, style;
103        private Float level;
104        private TextToSpeechLanguage language;
105
106
107        Builder(String text) {
108            this.text = text;
109        }
110
111        /**
112         * @param text A string of up to 1,500 characters (excluding SSML tags) containing the message to be
113         *             synthesized in the Call or Conversation. A single comma in text adds a short pause to the
114         *             synthesized speech. To add a longer pause a break tag needs to be used in SSML.
115         *             <p>
116         *             To use SSML tags, you must enclose the text in a speak element.
117         *
118         * @return This builder.
119         */
120        public Builder text(String text) {
121            this.text = text;
122            return this;
123        }
124
125        /**
126         * @param bargeIn Set to true so this action is terminated when the user presses a button on the keypad.
127         *                Use this feature to enable users to choose an option without having to listen to the whole
128         *                message in your Interactive Voice Response (IVR). If you set bargeIn to true the next
129         *                action in the NCCO stack must be an input action. The default value is false.
130         *
131         * @return This builder.
132         */
133        public Builder bargeIn(Boolean bargeIn) {
134            this.bargeIn = bargeIn;
135            return this;
136        }
137
138        /**
139         * @param loop The number of times text is repeated before the Call is closed.
140         *             The default value is 1. Set to 0 to loop infinitely.
141         *
142         * @return This builder.
143         */
144        public Builder loop(Integer loop) {
145            this.loop = loop;
146            return this;
147        }
148
149        /**
150         * @param level The volume level that the speech is played. This can be any value between -1 to 1 with 0
151         *              being the default.
152         *
153         * @return This builder.
154         */
155        public Builder level(Float level) {
156            this.level = level;
157            return this;
158        }
159
160        /**
161         * @param language The Language to use when converting the text to speech.
162         *
163         * @return This builder.
164         */
165        public Builder language(TextToSpeechLanguage language) {
166            this.language = language;
167            return this;
168        }
169
170        /**
171         * @param style The vocal style to use.
172         *
173         * @return This builder.
174         */
175        public Builder style(Integer style) {
176            this.style = style;
177            return this;
178        }
179
180        /**
181         * @param premium Whether to use Premium text-to-speech. Set to {@code true} to use the premium version
182         *                of the specified style if available, otherwise the standard version will be used.
183         *
184         * @return This builder.
185         */
186        public Builder premium(Boolean premium) {
187            this.premium = premium;
188            return this;
189        }
190
191        /**
192         * @return A new {@link TalkAction} object from the stored builder options.
193         */
194        public TalkAction build() {
195            return new TalkAction(this);
196        }
197    }
198}