sms_message/
sms_message.rs

1//! SMS/MMS messaging TwiML examples
2//!
3//! Run this example with:
4//! ```bash
5//! cargo run --example sms_message
6//! ```
7
8use twiml_rust::{
9    messaging::{Body, Media, Message, MessageAttributes, RedirectAttributes},
10    MessagingResponse, TwiML,
11};
12
13fn main() {
14    println!("=== TwiML Messaging Examples ===\n");
15
16    // Example 1: Simple SMS
17    println!("1. Simple SMS:");
18    simple_sms();
19
20    // Example 2: SMS with attributes
21    println!("\n2. SMS with Attributes:");
22    sms_with_attributes();
23
24    // Example 3: MMS with single media
25    println!("\n3. MMS with Single Media:");
26    mms_single_media();
27
28    // Example 4: MMS with multiple media
29    println!("\n4. MMS with Multiple Media:");
30    mms_multiple_media();
31
32    // Example 5: Message with redirect
33    println!("\n5. Message with Redirect:");
34    message_with_redirect();
35}
36
37/// Simple SMS message
38fn simple_sms() {
39    let response =
40        MessagingResponse::new().message("Thanks for your message! We'll get back to you soon.");
41
42    println!("{}", response.to_xml());
43}
44
45/// SMS with custom attributes
46fn sms_with_attributes() {
47    let response = MessagingResponse::new().message_with_attributes(
48        MessageAttributes::new()
49            .to("+15551234567")
50            .from("+15559876543")
51            .action("https://example.com/message-status")
52            .method("POST")
53            .status_callback("https://example.com/status"),
54        "Your order #12345 has been shipped!",
55    );
56
57    println!("{}", response.to_xml());
58}
59
60/// MMS with a single media attachment
61fn mms_single_media() {
62    let message = Message::with_nouns(MessageAttributes::new())
63        .body(Body::new("Check out this image!"))
64        .add_media(Media::new("https://example.com/image.jpg"));
65
66    let response = MessagingResponse::new().message_with_nouns(message);
67
68    println!("{}", response.to_xml());
69}
70
71/// MMS with multiple media attachments
72fn mms_multiple_media() {
73    let message = Message::with_nouns(
74        MessageAttributes::new()
75            .to("+15551234567")
76            .from("+15559876543"),
77    )
78    .body(Body::new("Here are the photos from today's event!"))
79    .add_media(Media::new("https://example.com/photo1.jpg"))
80    .add_media(Media::new("https://example.com/photo2.jpg"))
81    .add_media(Media::new("https://example.com/photo3.jpg"));
82
83    let response = MessagingResponse::new().message_with_nouns(message);
84
85    println!("{}", response.to_xml());
86}
87
88/// Message followed by a redirect
89fn message_with_redirect() {
90    let response = MessagingResponse::new()
91        .message("Processing your request...")
92        .redirect_with_attributes(
93            RedirectAttributes::new().method("POST"),
94            "https://example.com/next-step",
95        );
96
97    println!("{}", response.to_xml());
98}