162 lines
5.3 KiB
Rust
162 lines
5.3 KiB
Rust
mod bidding;
|
|
mod bot_result;
|
|
mod commands;
|
|
mod config;
|
|
mod db;
|
|
mod dptree_utils;
|
|
mod handle_error;
|
|
mod handler_utils;
|
|
mod keyboard_utils;
|
|
mod message_utils;
|
|
mod sqlite_storage;
|
|
mod start_command_data;
|
|
#[cfg(test)]
|
|
mod test_utils;
|
|
mod wrap_endpoint;
|
|
|
|
use crate::bidding::{bidding_handler, BiddingState};
|
|
use crate::commands::{
|
|
my_listings::{my_listings_handler, my_listings_inline_handler, MyListingsState},
|
|
new_listing::{new_listing_handler, NewListingState},
|
|
};
|
|
use crate::db::{ListingDAO, UserDAO};
|
|
use crate::handle_error::with_error_handler;
|
|
use crate::handler_utils::{find_or_create_db_user_from_update, update_into_message_target};
|
|
use crate::sqlite_storage::SqliteStorage;
|
|
use anyhow::Result;
|
|
pub use bot_result::*;
|
|
use commands::*;
|
|
use config::Config;
|
|
use log::info;
|
|
use serde::{Deserialize, Serialize};
|
|
use teloxide::dispatching::dialogue::serializer::Json;
|
|
use teloxide::{prelude::*, types::BotCommand, utils::command::BotCommands};
|
|
pub use wrap_endpoint::*;
|
|
|
|
/// Set up the bot's command menu that appears when users tap the menu button
|
|
async fn setup_bot_commands(bot: &Bot) -> Result<()> {
|
|
info!("Setting up bot command menu...");
|
|
|
|
// Convert our Command enum to Telegram BotCommand structs
|
|
let commands: Vec<BotCommand> = Command::bot_commands()
|
|
.into_iter()
|
|
.map(|cmd| BotCommand::new(cmd.command, cmd.description))
|
|
.collect();
|
|
|
|
// Set the commands for the bot's menu
|
|
bot.set_my_commands(commands).await?;
|
|
info!("Bot command menu configured successfully");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[derive(BotCommands, Clone)]
|
|
#[command(rename_rule = "lowercase", description = "Auction Bot Commands")]
|
|
pub enum Command {
|
|
#[command(description = "Show welcome message")]
|
|
Start,
|
|
#[command(description = "Show help message")]
|
|
Help,
|
|
#[command(description = "Create a new listing or auction")]
|
|
NewListing,
|
|
#[command(description = "View your listings and auctions")]
|
|
MyListings,
|
|
#[command(description = "View your active bids")]
|
|
MyBids,
|
|
#[command(description = "Configure notifications")]
|
|
Settings,
|
|
}
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
enum DialogueRootState {
|
|
#[default]
|
|
Start,
|
|
MainMenu,
|
|
NewListing(NewListingState),
|
|
MyListings(MyListingsState),
|
|
Bidding(BiddingState),
|
|
}
|
|
|
|
type RootDialogue = Dialogue<DialogueRootState, SqliteStorage<Json>>;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
// Load and validate configuration from environment/.env file
|
|
let config = Config::from_env()?;
|
|
|
|
// Create database connection pool
|
|
let db_pool = config.create_database_pool().await?;
|
|
|
|
info!("Starting Pawctioneer Bot...");
|
|
let bot = Bot::new(&config.telegram_token);
|
|
|
|
// Set up the bot's command menu
|
|
setup_bot_commands(&bot).await?;
|
|
|
|
let dialog_storage = SqliteStorage::new(db_pool.clone(), Json).await?;
|
|
|
|
// Create dispatcher with dialogue system
|
|
Dispatcher::builder(
|
|
bot,
|
|
dptree::entry()
|
|
.filter_map(update_into_message_target)
|
|
.filter_map_async(find_or_create_db_user_from_update)
|
|
.branch(my_listings_inline_handler())
|
|
.branch(
|
|
dptree::entry()
|
|
.enter_dialogue::<Update, SqliteStorage<Json>, DialogueRootState>()
|
|
.branch(new_listing_handler())
|
|
.branch(my_listings_handler())
|
|
.branch(bidding_handler())
|
|
.branch(
|
|
Update::filter_callback_query().branch(
|
|
dptree::case![DialogueRootState::MainMenu]
|
|
.endpoint(with_error_handler(handle_main_menu_callback)),
|
|
),
|
|
)
|
|
.branch(
|
|
Update::filter_message()
|
|
.filter_command::<Command>()
|
|
.branch(
|
|
dptree::case![Command::Start]
|
|
.endpoint(with_error_handler(handle_start)),
|
|
)
|
|
.branch(
|
|
dptree::case![Command::Help]
|
|
.endpoint(with_error_handler(handle_help)),
|
|
)
|
|
.branch(
|
|
dptree::case![Command::MyBids]
|
|
.endpoint(with_error_handler(handle_my_bids)),
|
|
)
|
|
.branch(
|
|
dptree::case![Command::Settings]
|
|
.endpoint(with_error_handler(handle_settings)),
|
|
),
|
|
),
|
|
)
|
|
.branch(Update::filter_message().endpoint(with_error_handler(unknown_message_handler))),
|
|
)
|
|
.dependencies(dptree::deps![
|
|
dialog_storage,
|
|
ListingDAO::new(db_pool.clone()),
|
|
UserDAO::new(db_pool.clone())
|
|
])
|
|
.enable_ctrlc_handler()
|
|
.worker_queue_size(1)
|
|
.build()
|
|
.dispatch()
|
|
.await;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn unknown_message_handler(msg: Message) -> BotResult {
|
|
Err(BotError::UserVisibleError(format!(
|
|
"Unknown command: `{}`\n\n\
|
|
Try /help to see the list of commands.\
|
|
",
|
|
msg.text().unwrap_or("")
|
|
)))
|
|
}
|