tgbot/types/dice/
mod.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    api::{Method, Payload},
7    types::{ChatId, Integer, Message, ReplyMarkup, ReplyParameters},
8};
9
10#[cfg(test)]
11mod tests;
12
13/// Represents a dice with a random value.
14#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize)]
15pub struct Dice {
16    #[serde(rename = "emoji")]
17    dice_type: DiceType,
18    value: Integer,
19}
20
21impl Dice {
22    /// Creates a new `Dice`.
23    ///
24    /// # Arguments
25    ///
26    /// * `dice_type` - Type of the dice.
27    /// * `value` - Value rolled on the dice.
28    pub fn new(dice_type: DiceType, value: Integer) -> Self {
29        Self { dice_type, value }
30    }
31
32    /// Returns the type of the dice.
33    pub fn dice_type(&self) -> DiceType {
34        self.dice_type
35    }
36
37    /// Returns the value rolled on the dice.
38    pub fn value(&self) -> Integer {
39        self.value
40    }
41}
42
43/// Represents a type of a dice.
44#[derive(Debug, Copy, Clone, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
45#[non_exhaustive]
46pub enum DiceType {
47    /// Basketball; range: 1-5.
48    #[serde(rename = "🏀")]
49    Basketball,
50    /// Bones; range: 1-6.
51    #[serde(rename = "🎲")]
52    Bones,
53    /// Bowling; range: 1-6.
54    #[serde(rename = "🎳")]
55    Bowling,
56    /// Darts; range: 1-6.
57    #[serde(rename = "🎯")]
58    Darts,
59    /// Football; range: 1-5.
60    #[serde(rename = "⚽")]
61    Football,
62    /// Slot machine; range: 1-64.
63    #[serde(rename = "🎰")]
64    SlotMachine,
65}
66
67impl DiceType {
68    fn as_char(self) -> char {
69        use super::DiceType::*;
70        match self {
71            Basketball => '🏀',
72            Bones => '🎲',
73            Bowling => '🎳',
74            Darts => '🎯',
75            Football => '⚽',
76            SlotMachine => '🎰',
77        }
78    }
79}
80
81impl From<DiceType> for char {
82    fn from(value: DiceType) -> Self {
83        value.as_char()
84    }
85}
86
87impl fmt::Display for DiceType {
88    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89        fmt::Display::fmt(&self.as_char(), f)
90    }
91}
92
93/// Sends a dice.
94#[serde_with::skip_serializing_none]
95#[derive(Clone, Debug, Serialize)]
96pub struct SendDice {
97    chat_id: ChatId,
98    emoji: DiceType,
99    allow_paid_broadcast: Option<bool>,
100    business_connection_id: Option<String>,
101    disable_notification: Option<bool>,
102    message_effect_id: Option<String>,
103    message_thread_id: Option<Integer>,
104    protect_content: Option<bool>,
105    reply_markup: Option<ReplyMarkup>,
106    reply_parameters: Option<ReplyParameters>,
107}
108
109impl SendDice {
110    /// Creates a new `SendDice`.
111    ///
112    /// # Arguments
113    ///
114    /// * `chat_id` - Unique identifier of the target chat.
115    /// * `dice_type` - Type of a dice.
116    pub fn new<T>(chat_id: T, dice_type: DiceType) -> Self
117    where
118        T: Into<ChatId>,
119    {
120        Self {
121            chat_id: chat_id.into(),
122            emoji: dice_type,
123            allow_paid_broadcast: None,
124            business_connection_id: None,
125            disable_notification: None,
126            message_effect_id: None,
127            message_thread_id: None,
128            protect_content: None,
129            reply_markup: None,
130            reply_parameters: None,
131        }
132    }
133
134    /// Sets a new value for the `allow_paid_broadcast` flag.
135    ///
136    /// # Arguments
137    ///
138    /// * `value` - Whether to allow up to 1000 messages per second, ignoring broadcasting limits
139    ///   for a fee of 0.1 Telegram Stars per message.
140    ///   The relevant Stars will be withdrawn from the bot's balance.
141    pub fn with_allow_paid_broadcast(mut self, value: bool) -> Self {
142        self.allow_paid_broadcast = Some(value);
143        self
144    }
145
146    /// Sets a new business connection ID.
147    ///
148    /// # Arguments
149    ///
150    /// * `value` - Unique identifier of the business connection.
151    pub fn with_business_connection_id<T>(mut self, value: T) -> Self
152    where
153        T: Into<String>,
154    {
155        self.business_connection_id = Some(value.into());
156        self
157    }
158
159    /// Sets a new value for the `disable_notification` flag.
160    ///
161    /// # Arguments
162    ///
163    /// * `value` - Indicates whether to send the message silently or not;
164    ///   a user will receive a notification without sound.
165    pub fn with_disable_notification(mut self, value: bool) -> Self {
166        self.disable_notification = Some(value);
167        self
168    }
169
170    /// Sets a new message effect ID.
171    ///
172    /// # Arguments
173    ///
174    /// * `value` - Unique identifier of the message effect to be added to the message; for private chats only.
175    pub fn with_message_effect_id<T>(mut self, value: T) -> Self
176    where
177        T: Into<String>,
178    {
179        self.message_effect_id = Some(value.into());
180        self
181    }
182
183    /// Sets a new message thread ID.
184    ///
185    /// # Arguments
186    ///
187    /// * `value` - Unique identifier of the target message thread;
188    ///   supergroups only.
189    pub fn with_message_thread_id(mut self, value: Integer) -> Self {
190        self.message_thread_id = Some(value);
191        self
192    }
193
194    /// Sets a new value for the `protect_content` flag.
195    ///
196    /// # Arguments
197    ///
198    /// * `value` - Indicates whether to protect the contents
199    ///   of the sent message from forwarding and saving.
200    pub fn with_protect_content(mut self, value: bool) -> Self {
201        self.protect_content = Some(value);
202        self
203    }
204
205    /// Sets a new reply markup.
206    ///
207    /// # Arguments
208    ///
209    /// * `value` - Reply markup.
210    pub fn with_reply_markup<T>(mut self, value: T) -> Self
211    where
212        T: Into<ReplyMarkup>,
213    {
214        self.reply_markup = Some(value.into());
215        self
216    }
217
218    /// Sets new reply parameters.
219    ///
220    /// # Arguments
221    ///
222    /// * `value` - Description of the message to reply to.
223    pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Self {
224        self.reply_parameters = Some(value);
225        self
226    }
227}
228
229impl Method for SendDice {
230    type Response = Message;
231
232    fn into_payload(self) -> Payload {
233        Payload::json("sendDice", self)
234    }
235}