mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-10 20:12:25 +01:00
add MessageTextFilter
This commit is contained in:
parent
61d9dc5c0d
commit
a1caea0705
3 changed files with 86 additions and 0 deletions
3
src/dispatching/dispatchers/filter/filters/mod.rs
Normal file
3
src/dispatching/dispatchers/filter/filters/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub use text::*;
|
||||
|
||||
mod text;
|
82
src/dispatching/dispatchers/filter/filters/text.rs
Normal file
82
src/dispatching/dispatchers/filter/filters/text.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
use crate::dispatching::Filter;
|
||||
use crate::types::Message;
|
||||
|
||||
pub struct MessageTextFilter {
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl Filter<Message> for MessageTextFilter {
|
||||
fn test(&self, value: &Message) -> bool {
|
||||
match value.text() {
|
||||
Some(text) => self.text == text,
|
||||
None => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MessageTextFilter {
|
||||
pub fn new(text: String) -> Self {
|
||||
Self {
|
||||
text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn texts_are_equal() {
|
||||
let filter = MessageTextFilter::new("text".to_string());
|
||||
let json = r#"{
|
||||
"message_id": 199785,
|
||||
"from": {
|
||||
"id": 250918540,
|
||||
"is_bot": false,
|
||||
"first_name": "Андрей",
|
||||
"last_name": "Власов",
|
||||
"username": "aka_dude",
|
||||
"language_code": "en"
|
||||
},
|
||||
"chat": {
|
||||
"id": 250918540,
|
||||
"first_name": "Андрей",
|
||||
"last_name": "Власов",
|
||||
"username": "aka_dude",
|
||||
"type": "private"
|
||||
},
|
||||
"date": 1568289890,
|
||||
"text": "text"
|
||||
}"#;
|
||||
let message = serde_json::from_str::<Message>(json).unwrap();
|
||||
assert!(filter.test(&message));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn texts_are_not_equal() {
|
||||
let filter = MessageTextFilter::new("text".to_string());
|
||||
let json = r#"{
|
||||
"message_id": 199785,
|
||||
"from": {
|
||||
"id": 250918540,
|
||||
"is_bot": false,
|
||||
"first_name": "Андрей",
|
||||
"last_name": "Власов",
|
||||
"username": "aka_dude",
|
||||
"language_code": "en"
|
||||
},
|
||||
"chat": {
|
||||
"id": 250918540,
|
||||
"first_name": "Андрей",
|
||||
"last_name": "Власов",
|
||||
"username": "aka_dude",
|
||||
"type": "private"
|
||||
},
|
||||
"date": 1568289890,
|
||||
"text": "not equal text"
|
||||
}"#;
|
||||
let message = serde_json::from_str::<Message>(json).unwrap();
|
||||
assert_eq!(filter.test(&message), false);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ use crate::{
|
|||
types::{CallbackQuery, ChosenInlineResult, Message, Update, UpdateKind},
|
||||
};
|
||||
|
||||
pub mod filters;
|
||||
pub mod error_policy;
|
||||
|
||||
type Handlers<'a, T, E> =
|
||||
|
|
Loading…
Reference in a new issue