voice_call/
voice_call.rs

1//! Voice call TwiML examples
2//!
3//! Run this example with:
4//! ```bash
5//! cargo run --example voice_call
6//! ```
7
8use twiml_rust::{
9    voice::{Dial, DialNumber, Gather, Record, Say},
10    TwiML, VoiceResponse,
11};
12
13fn main() {
14    println!("=== TwiML Voice Call Examples ===\n");
15
16    // Example 1: Simple greeting
17    println!("1. Simple Greeting:");
18    simple_greeting();
19
20    // Example 2: Interactive voice response (IVR)
21    println!("\n2. Interactive Voice Response (IVR):");
22    ivr_menu();
23
24    // Example 3: Call forwarding
25    println!("\n3. Call Forwarding:");
26    call_forwarding();
27
28    // Example 4: Voicemail recording
29    println!("\n4. Voicemail Recording:");
30    voicemail();
31
32    // Example 5: Advanced SSML
33    println!("\n5. Advanced SSML:");
34    advanced_ssml();
35}
36
37/// Simple greeting with text-to-speech
38fn simple_greeting() {
39    let response = VoiceResponse::new()
40        .say("Hello! Welcome to TwiML Rust.")
41        .say("This is a simple voice response example.")
42        .hangup();
43
44    println!("{}", response.to_xml());
45}
46
47/// Interactive voice response menu
48fn ivr_menu() {
49    let gather = Gather::new()
50        .input(vec!["dtmf".to_string(), "speech".to_string()])
51        .action("https://example.com/handle-input")
52        .method("POST")
53        .timeout(10)
54        .num_digits(1)
55        .add_say(Say::new(
56            "Press 1 for sales, 2 for support, or 3 for billing. You can also say the department name.",
57        ));
58
59    let response = VoiceResponse::new()
60        .say("Welcome to our automated phone system.")
61        .gather(gather)
62        .say("We didn't receive any input.")
63        .redirect("https://example.com/main-menu");
64
65    println!("{}", response.to_xml());
66}
67
68/// Call forwarding to a phone number
69fn call_forwarding() {
70    let dial = Dial::new()
71        .timeout(30)
72        .add_number(DialNumber::new("+15559876543").send_digits("wwww1234"));
73
74    let response = VoiceResponse::new()
75        .say("Please wait while we connect your call.")
76        .dial_with(dial)
77        .say("The call could not be completed. Please try again later.")
78        .hangup();
79
80    println!("{}", response.to_xml());
81}
82
83/// Voicemail recording system
84fn voicemail() {
85    let record = Record::new()
86        .action("https://example.com/handle-recording")
87        .method("POST")
88        .max_length(120)
89        .finish_on_key("#")
90        .transcribe(true)
91        .transcribe_callback("https://example.com/transcription");
92
93    let response = VoiceResponse::new()
94        .say("Please leave a message after the beep. Press the pound key when finished.")
95        .record(record)
96        .say("Thank you for your message. Goodbye!")
97        .hangup();
98
99    println!("{}", response.to_xml());
100}
101
102/// Advanced SSML features
103fn advanced_ssml() {
104    let say = Say::new("Welcome to our service")
105        .voice("Polly.Joanna")
106        .language("en-US")
107        .add_break(Some("medium".to_string()), None)
108        .add_emphasis(Some("strong".to_string()), "Please listen carefully")
109        .add_break(None, Some("1s".to_string()))
110        .add_prosody(
111            Some("high".to_string()),
112            Some("slow".to_string()),
113            None,
114            "This is important information",
115        );
116
117    let response = VoiceResponse::new()
118        .say_with(say)
119        .play("https://example.com/music.mp3")
120        .hangup();
121
122    println!("{}", response.to_xml());
123}