mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
add Update::user(), Update::chat() functions
This commit is contained in:
parent
2028ce70c7
commit
1cec80bf9d
1 changed files with 57 additions and 4 deletions
|
@ -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::{
|
||||
|
|
Loading…
Reference in a new issue