From aae1f386456a605c12263e2ba5f09dc36bb180b3 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 27 Mar 2022 17:40:18 +0800 Subject: Finally, it works. Further cleanup work is needed. --- src/bot.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/bot.rs (limited to 'src/bot.rs') diff --git a/src/bot.rs b/src/bot.rs new file mode 100644 index 0000000..b1a4ea2 --- /dev/null +++ b/src/bot.rs @@ -0,0 +1,63 @@ +use std::error::Error; +use std::ops::Deref; +use std::sync::Arc; + +use teloxide::{prelude2::*, types::MessageKind, utils::command::BotCommand}; + +use crate::{database, DbPool, token}; +use crate::user::User; + +#[derive(BotCommand, Clone)] +#[command(rename = "lowercase", description = "These commands are supported:")] +enum Command { + #[command(description = "display this text.")] + Help, + #[command(description = "get your personal token.")] + Start, +} + +async fn answer(bot: AutoSend, message: Message, command: Command, db: Arc) + -> Result<(), Box> { + match command { + Command::Help => { + bot.send_message(message.chat.id, Command::descriptions()).await?; + } + Command::Start => { + if let MessageKind::Common(msg) = message.kind { + if msg.from.is_none() { + return Ok(()); // ignore messages from channel + } + let from = msg.from.unwrap(); + let db = db.deref(); + let chat_id = message.chat.id; + match database::create_user(db, User { + id: from.id as u64, + name: from.username.unwrap_or_else(|| String::from("")), + token: token::generate(), + chat_id: chat_id as u64, + }).await { + Ok(_) => {} + Err(why) => println!("cannot create user: {:?}", why), + } + bot.send_message( + chat_id, + match database::get_user_by_chat_id(db, chat_id as u64).await? { + Some(user) => + format!("Your token is `{}`. Treat it as a secret!", user.token), + _ => + String::from("Error: cannot fetch token.") + }, + ).await?; + } + } + }; + Ok(()) +} + +pub async fn repl(bot: Bot, db: Arc) { + teloxide::repls2::commands_repl( + bot.auto_send(), + move |bot, msg, cmd| + answer(bot, msg, cmd, Arc::clone(&db)), Command::ty(), + ).await; +} -- cgit v1.2.3