1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use super::base::{Request, TelegramMethod};

use crate::{client::Bot, types::ShippingOption};

use serde::Serialize;
use serde_with::skip_serializing_none;

/// If you sent an invoice requesting a shipping address and the parameter `is_flexible` was specified, the Bot API will send an [`Update`](crate::types::Update) with a `shipping_query` field to the bot. Use this method to reply to shipping queries.
/// # Documentation
/// <https://core.telegram.org/bots/api#answershippingquery>
/// # Returns
/// On success, `true` is returned
#[skip_serializing_none]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize)]
pub struct AnswerShippingQuery {
    /// Unique identifier for the query to be answered
    pub shipping_query_id: String,
    /// Pass `true` if delivery to the specified address is possible and `false` if there are any problems (for example, if delivery to the specified address is not possible)
    pub ok: bool,
    /// Required if `ok` is `true`. A JSON-serialized array of available shipping options.
    pub shipping_options: Option<Vec<ShippingOption>>,
    /// Required if `ok` is `false`. Error message in human readable form that explains why it is impossible to complete the order (e.g. "Sorry, delivery to your desired address is unavailable'). Telegram will display this message to the user.
    pub error_message: Option<String>,
}

impl AnswerShippingQuery {
    #[must_use]
    pub fn new(shipping_query_id: impl Into<String>, ok: bool) -> Self {
        Self {
            shipping_query_id: shipping_query_id.into(),
            ok,
            shipping_options: None,
            error_message: None,
        }
    }

    #[must_use]
    pub fn shipping_query_id(self, val: impl Into<String>) -> Self {
        Self {
            shipping_query_id: val.into(),
            ..self
        }
    }

    #[must_use]
    pub fn ok(self, val: bool) -> Self {
        Self { ok: val, ..self }
    }

    #[must_use]
    pub fn shipping_option(self, val: ShippingOption) -> Self {
        Self {
            shipping_options: Some(
                self.shipping_options
                    .unwrap_or_default()
                    .into_iter()
                    .chain(Some(val))
                    .collect(),
            ),
            ..self
        }
    }

    #[must_use]
    pub fn shipping_options(self, val: impl IntoIterator<Item = ShippingOption>) -> Self {
        Self {
            shipping_options: Some(
                self.shipping_options
                    .unwrap_or_default()
                    .into_iter()
                    .chain(val)
                    .collect(),
            ),
            ..self
        }
    }

    #[must_use]
    pub fn error_message(self, val: impl Into<String>) -> Self {
        Self {
            error_message: Some(val.into()),
            ..self
        }
    }
}

impl AnswerShippingQuery {
    #[must_use]
    pub fn shipping_options_option(
        self,
        val: Option<impl IntoIterator<Item = ShippingOption>>,
    ) -> Self {
        Self {
            shipping_options: val.map(|val| {
                self.shipping_options
                    .unwrap_or_default()
                    .into_iter()
                    .chain(val)
                    .collect()
            }),
            ..self
        }
    }

    #[must_use]
    pub fn error_message_option(self, val: Option<impl Into<String>>) -> Self {
        Self {
            error_message: val.map(Into::into),
            ..self
        }
    }
}

impl TelegramMethod for AnswerShippingQuery {
    type Method = Self;
    type Return = bool;

    fn build_request<Client>(&self, _bot: &Bot<Client>) -> Request<Self::Method> {
        Request::new("answerShippingQuery", self, None)
    }
}

impl AsRef<AnswerShippingQuery> for AnswerShippingQuery {
    fn as_ref(&self) -> &Self {
        self
    }
}