create filter to handle botscript-defined commands
This commit is contained in:
parent
e0c00d68f9
commit
2ccfc19a6c
18
src/main.rs
18
src/main.rs
@ -6,11 +6,15 @@ pub mod mongodb_storage;
|
|||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use botscript::{BotMessage, Runner, RunnerConfig};
|
use botscript::{BotMessage, Runner, RunnerConfig};
|
||||||
|
use commands::BotCommand;
|
||||||
use db::application::Application;
|
use db::application::Application;
|
||||||
use db::callback_info::CallbackInfo;
|
use db::callback_info::CallbackInfo;
|
||||||
use db::message_forward::MessageForward;
|
use db::message_forward::MessageForward;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use std::sync::RwLock;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use teloxide::sugar::request::RequestReplyExt;
|
use teloxide::sugar::request::RequestReplyExt;
|
||||||
use utils::create_callback_button;
|
use utils::create_callback_button;
|
||||||
@ -158,11 +162,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
|
let cmdmap: std::sync::Arc<RwLock<HashMap<_, _>>> =
|
||||||
|
std::sync::Arc::new(RwLock::new(bc.rc.dialog.commands));
|
||||||
|
|
||||||
let handler = dptree::entry()
|
let handler = dptree::entry()
|
||||||
.inspect(|u: Update| {
|
.inspect(|u: Update| {
|
||||||
info!("{u:#?}"); // Print the update to the console with inspect
|
info!("{u:#?}"); // Print the update to the console with inspect
|
||||||
})
|
})
|
||||||
|
.branch(
|
||||||
|
Update::filter_message()
|
||||||
|
.filter_map(|m: Message| m.text().and_then(|t| BotCommand::from_str(t).ok()))
|
||||||
|
.filter_map(move |bc: BotCommand| {
|
||||||
|
let rc = std::sync::Arc::clone(&cmdmap);
|
||||||
|
|
||||||
|
let cmdmap = rc.read().expect("RwLock lock on commands map failed");
|
||||||
|
|
||||||
|
cmdmap.get(bc.command()).cloned()
|
||||||
|
})
|
||||||
|
.endpoint(botscript_command_handler),
|
||||||
|
)
|
||||||
.branch(
|
.branch(
|
||||||
Update::filter_callback_query()
|
Update::filter_callback_query()
|
||||||
.filter_async(async |q: CallbackQuery, mut db: DB| {
|
.filter_async(async |q: CallbackQuery, mut db: DB| {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user