tgbot/types/definitions/
dice.rs

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