rustigram_types/poll.rs
1use crate::user::User;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5/// A native Telegram poll or quiz.
6///
7/// Polls are either `regular` (users vote freely) or `quiz` (one correct
8/// answer). Returned inside [`Message`](crate::message::Message) when a
9/// poll is sent or forwarded.
10pub struct Poll {
11 /// Unique poll identifier.
12 pub id: String,
13 /// Poll question (1–300 characters).
14 pub question: String,
15 /// Special entities in the question.
16 pub question_entities: Option<Vec<crate::message::MessageEntity>>,
17 /// List of answer options.
18 pub options: Vec<PollOption>,
19 /// Total number of users who voted.
20 pub total_voter_count: u32,
21 /// `true` if the poll is closed.
22 pub is_closed: bool,
23 /// `true` if the poll is anonymous.
24 pub is_anonymous: bool,
25 /// Poll type.
26 #[serde(rename = "type")]
27 pub kind: PollType,
28 /// `true` if the poll allows multiple answers.
29 pub allows_multiple_answers: bool,
30 /// `true` if voters can change their vote.
31 pub allows_revoting: Option<bool>,
32 /// Zero-based index of the correct answer (quiz polls only).
33 pub correct_option_id: Option<u8>,
34 /// Explanation shown after the quiz is answered.
35 pub explanation: Option<String>,
36 /// Special entities in the explanation.
37 pub explanation_entities: Option<Vec<crate::message::MessageEntity>>,
38 /// Duration in seconds for which the poll will be active after creation.
39 pub open_period: Option<u32>,
40 /// Unix timestamp when the poll closes automatically.
41 pub close_date: Option<i64>,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45/// One option in a [`Poll`].
46pub struct PollOption {
47 /// Option text (1–100 characters).
48 pub text: String,
49 /// Special entities in the option text.
50 pub text_entities: Option<Vec<crate::message::MessageEntity>>,
51 /// Number of users who voted for this option.
52 pub voter_count: u32,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56/// An option to include when creating a poll with `sendPoll`.
57pub struct InputPollOption {
58 /// Option text (1–100 characters).
59 pub text: String,
60 /// Parse mode for the option text.
61 pub text_parse_mode: Option<crate::message::ParseMode>,
62 /// Special entities in the option text.
63 pub text_entities: Option<Vec<crate::message::MessageEntity>>,
64}
65
66impl InputPollOption {
67 /// Creates a new `InputPollOption` with the given text.
68 pub fn new(text: impl Into<String>) -> Self {
69 Self {
70 text: text.into(),
71 text_parse_mode: None,
72 text_entities: None,
73 }
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "snake_case")]
79/// The type of a poll.
80pub enum PollType {
81 /// A regular poll where users can vote freely.
82 Regular,
83 /// A quiz with one correct answer.
84 Quiz,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88/// A portion of a price — label and amount in the smallest currency unit.
89///
90/// For Telegram Stars (`XTR`) the amount is in whole Stars.
91/// For fiat currencies (e.g. `USD`) the amount is in cents.
92pub struct PollAnswer {
93 /// Unique poll identifier.
94 pub poll_id: String,
95 /// The chat that changed the answer (for anonymous polls in groups).
96 pub voter_chat: Option<crate::chat::Chat>,
97 /// The user who changed the answer (for non-anonymous polls).
98 pub user: Option<User>,
99 /// Indices of the chosen options (empty if the vote was retracted).
100 pub option_ids: Vec<u8>,
101}