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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use serde::{Deserialize, Serialize};

use crate::chat::ChatMemberUpdated;
use crate::message::{Message, Poll, PollAnswer};
use crate::payment::{PreCheckoutQuery, ShippingQuery};
use crate::query::{CallbackQuery, ChosenInlineResult, InlineQuery};
use crate::{JsonMethod, TelegramMethod};

/// This object represents an incoming update.
/// At most **one** of the optional parameters can be present in any given update.
#[derive(Deserialize)]
pub struct Update {
    /// The update's unique identifier.
    /// Update identifiers start from a certain positive number and increase sequentially.
    /// This ID becomes especially handy if you're using Webhooks,
    /// since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
    /// If there are no new updates for at least a week,
    /// then identifier of the next update will be chosen randomly instead of sequentially.
    pub update_id: u32,
    #[serde(flatten)]
    /// Update type
    pub kind: UpdateKind,
}

/// Update type
#[derive(Deserialize)]
#[serde(untagged)]
pub enum UpdateKind {
    /// New incoming message of any kind — text, photo, sticker, etc.
    Message { message: Message },
    /// New version of a message that is known to the bot and was edited
    EditedMessage { edited_message: Message },
    /// New incoming channel post of any kind — text, photo, sticker, etc.
    ChannelPost { channel_post: Message },
    /// New version of a channel post that is known to the bot and was edited
    EditedChannelPost { edited_channel_post: Message },
    /// New incoming [inline](https://core.telegram.org/bots/api#inline-mode) query
    InlineQuery { inline_query: InlineQuery },
    /// The result of an [inline](https://core.telegram.org/bots/api#inline-mode)
    /// query that was chosen by a user and sent to their chat partner.
    /// Please see Telegram's documentation on the [feedback collecting](https://core.telegram.org/bots/inline#collecting-feedback) for details
    /// on how to enable these updates for your bot.
    ChosenInlineResult {
        chosen_inline_result: ChosenInlineResult,
    },
    /// New incoming callback query
    CallbackQuery { callback_query: CallbackQuery },
    /// New incoming shipping query. Only for invoices with flexible price
    ShippingQuery { shipping_query: ShippingQuery },
    /// New incoming pre-checkout query. Contains full information about checkout
    PreCheckoutQuery {
        pre_checkout_query: PreCheckoutQuery,
    },
    /// New poll state.
    /// Bots receive only updates about stopped polls and polls, which are sent by the bot
    Poll { poll: Poll },
    /// A user changed their answer in a non-anonymous poll.
    /// Bots receive new votes only in polls that were sent by the bot itself.
    PollAnswer { poll_answer: PollAnswer },
    /// The bot's chat member status was updated in a chat. For private chats,
    /// this update is received only when the bot is blocked or unblocked by the user.
    MyChatMemberUpdated { my_chat_member: ChatMemberUpdated },
    /// A chat member's status was updated in a chat.
    /// The bot must be an administrator in the chat and must explicitly specify “chat_member”
    /// in the list of *allowed_updates* to receive these updates.
    ChatMemberUpdated { my_chat_member: ChatMemberUpdated },
}

/// Use this method to receive incoming updates using long polling ([wiki](https://en.wikipedia.org/wiki/Push_technology#Long_polling)).
#[derive(Serialize)]
pub struct GetUpdates {
    /// Identifier of the first update to be returned.
    /// Must be greater by one than the highest among the identifiers of previously received updates.
    /// By default, updates starting with the earliest unconfirmed update are returned.
    /// An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
    /// The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue.
    /// All previous updates will forgotten.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<i32>,
    /// Limits the number of updates to be retrieved.
    /// Values between 1-100 are accepted. Defaults to 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// Timeout in seconds for long polling.
    /// Defaults to 0, i.e. usual short polling.
    /// Should be positive, short polling should be used for testing purposes only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout: Option<u32>,
    /// A JSON-serialized list of the update types you want your bot to receive.
    /// For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types.
    /// See [Update](https://core.telegram.org/bots/api#update) for a complete list of available update types.
    /// Specify an empty list to receive all update types except chat_member (default).
    /// If not specified, the previous setting will be used.
    ///
    /// Please note that this parameter doesn't affect updates created before the call to the getUpdates,
    /// so unwanted updates may be received for a short period of time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_updates: Option<Vec<String>>,
}

impl GetUpdates {
    /// Create a new getUpdates request
    pub fn new() -> Self {
        Self {
            offset: None,
            limit: None,
            timeout: None,
            allowed_updates: None,
        }
    }
    /// Set offset
    pub fn with_offset(self, offset: i32) -> Self {
        Self {
            offset: Some(offset),
            ..self
        }
    }
    /// Set limit
    pub fn with_limit(self, limit: u32) -> Self {
        Self {
            limit: Some(limit),
            ..self
        }
    }
    /// Set timeout
    pub fn with_timeout(self, timeout: u32) -> Self {
        Self {
            timeout: Some(timeout),
            ..self
        }
    }
    /// Set allowed updates
    pub fn with_allowed_updates(self, updates: Vec<String>) -> Self {
        Self {
            allowed_updates: Some(updates),
            ..self
        }
    }
    /// Add one allowed update
    pub fn with_allowed_update(mut self, update: impl Into<String>) -> Self {
        let updates = self.allowed_updates.get_or_insert_with(Default::default);
        updates.push(update.into());
        Self {
            allowed_updates: self.allowed_updates,
            ..self
        }
    }
}

impl TelegramMethod for GetUpdates {
    type Response = Vec<Update>;

    fn name() -> &'static str {
        "getUpdates"
    }
}

impl JsonMethod for GetUpdates {}