mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-24 23:57:38 +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 serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::types::{
|
use crate::types::{CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll, PreCheckoutQuery, ShippingQuery, User, Sender, Chat};
|
||||||
CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll,
|
|
||||||
PreCheckoutQuery, ShippingQuery,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// This [object] represents an incoming update.
|
/// This [object] represents an incoming update.
|
||||||
///
|
///
|
||||||
|
@ -73,6 +70,62 @@ pub enum UpdateKind {
|
||||||
Poll(Poll),
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
|
|
Loading…
Add table
Reference in a new issue