use Callbackinfo with internal Callback struct to handle telegram callbacks
This commit is contained in:
parent
58a16a5927
commit
cbe24cc134
63
src/main.rs
63
src/main.rs
@ -2,7 +2,8 @@ pub mod admin;
|
|||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod mongodb_storage;
|
pub mod mongodb_storage;
|
||||||
|
|
||||||
use log::info;
|
use db::callback_info::CallbackInfo;
|
||||||
|
use log::{info, warn};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::admin::{admin_command_handler, AdminCommands};
|
use crate::admin::{admin_command_handler, AdminCommands};
|
||||||
@ -70,6 +71,16 @@ pub enum State {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
#[serde(rename = "snake_case")]
|
||||||
|
pub enum Callback {
|
||||||
|
MoreInfo,
|
||||||
|
DemoProject { id: u32 },
|
||||||
|
}
|
||||||
|
|
||||||
|
type CallbackStore = CallbackInfo<Callback>;
|
||||||
|
|
||||||
pub struct BotController {
|
pub struct BotController {
|
||||||
pub bot: Bot,
|
pub bot: Bot,
|
||||||
pub db: DB,
|
pub db: DB,
|
||||||
@ -175,21 +186,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<()> {
|
async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<()> {
|
||||||
bot.answer_callback_query(&q.id).await?;
|
bot.answer_callback_query(&q.id).await?;
|
||||||
|
|
||||||
if let Some(ref data) = q.data {
|
let data = match q.data {
|
||||||
match data.as_str() {
|
Some(ref data) => data,
|
||||||
"more_info" => {
|
None => {
|
||||||
answer_message(
|
// not really our case to handle
|
||||||
&bot,
|
return Ok(());
|
||||||
q.chat_id().map(|i| i.0).unwrap_or(q.from.id.0 as i64),
|
|
||||||
&mut db,
|
|
||||||
"more_info",
|
|
||||||
None as Option<InlineKeyboardMarkup>,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
_ => {} // do nothing, yet
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
let callback = match CallbackStore::get_callback(&mut db, data).await? {
|
||||||
|
Some(callback) => callback,
|
||||||
|
None => {
|
||||||
|
warn!("Not found callback for data: {data}");
|
||||||
|
// doing this silently beacuse end user shouldn't know about backend internal data
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match callback {
|
||||||
|
Callback::MoreInfo => {
|
||||||
|
answer_message(
|
||||||
|
&bot,
|
||||||
|
q.chat_id().map(|i| i.0).unwrap_or(q.from.id.0 as i64),
|
||||||
|
&mut db,
|
||||||
|
"more_info",
|
||||||
|
None as Option<InlineKeyboardMarkup>,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -582,7 +610,10 @@ async fn make_start_buttons(db: &mut DB) -> BotResult<InlineKeyboardMarkup> {
|
|||||||
.collect();
|
.collect();
|
||||||
buttons.push(vec![InlineKeyboardButton::callback(
|
buttons.push(vec![InlineKeyboardButton::callback(
|
||||||
"More info",
|
"More info",
|
||||||
"more_info",
|
CallbackStore::new(Callback::MoreInfo)
|
||||||
|
.store(db)
|
||||||
|
.await?
|
||||||
|
.get_id(),
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
Ok(InlineKeyboardMarkup::new(buttons))
|
Ok(InlineKeyboardMarkup::new(buttons))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user