From fbbb5c842bca3e57763825565a9f47238c073baf Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 3 Mar 2022 02:13:27 +0300 Subject: [PATCH] Add `User::is_telegram` --- src/types/user.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/types/user.rs b/src/types/user.rs index 9498c799..60856fac 100644 --- a/src/types/user.rs +++ b/src/types/user.rs @@ -84,6 +84,26 @@ impl User { self.id == ANON_CHANNEL_ID } + + /// Returns `true` if this is special user used by telegram itself. + /// + /// It is sometimes also used as a fallback, for example when a channel post + /// is automatically forwarded to a group, bots in a group will get a + /// message where `from` is the Telegram user. + pub fn is_telegram(&self) -> bool { + const TELEGRAM_USER_ID: i64 = 777000; + + // Sanity check + debug_assert!( + (self.id != TELEGRAM_USER_ID) + || (!self.is_bot + && self.first_name == "Telegram" + && self.last_name.is_none() + && self.username.is_none()) + ); + + self.id == TELEGRAM_USER_ID + } } #[cfg(test)]