diff --git a/src/types/update.rs b/src/types/update.rs index b740afe1..02f70977 100644 --- a/src/types/update.rs +++ b/src/types/update.rs @@ -2,10 +2,7 @@ use serde::{Deserialize, Serialize}; -use crate::types::{ - CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll, - PreCheckoutQuery, ShippingQuery, -}; +use crate::types::{CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll, PreCheckoutQuery, ShippingQuery, User, Sender, Chat}; /// This [object] represents an incoming update. /// @@ -73,6 +70,62 @@ pub enum UpdateKind { Poll(Poll), } +impl Update { + pub fn user(&self) -> Option<&User> { + match &self.kind { + UpdateKind::Message(m) => { + match m.from() { + Some(Sender::User(user)) => Some(user), + _ => None, + } + } + UpdateKind::EditedMessage(m) => { + match m.from() { + Some(Sender::User(user)) => Some(user), + _ => None, + } + } + UpdateKind::CallbackQuery(query) => { + Some(&query.from) + } + UpdateKind::ChosenInlineResult(chosen) => { + Some(&chosen.from) + } + UpdateKind::InlineQuery(query) => { + Some(&query.from) + } + UpdateKind::ShippingQuery(query) => { + Some(&query.from) + } + UpdateKind::PreCheckoutQuery(query) => { + Some(&query.from) + } + _ => None + } + } + + pub fn chat(&self) -> Option<&Chat> { + match &self.kind { + UpdateKind::Message(m) => { + Some(&m.chat) + } + UpdateKind::EditedMessage(m) => { + Some(&m.chat) + } + UpdateKind::ChannelPost(p) => { + Some(&p.chat) + } + UpdateKind::EditedChannelPost(p) => { + Some(&p.chat) + } + UpdateKind::CallbackQuery(q) => { + Some(&q.message?.chat) + } + _ => None + } + } +} + #[cfg(test)] mod test { use crate::types::{