Skip to content

Commit b432d4a

Browse files
committed
feat: skeleton of card cmd, without db interaction
1 parent 999a384 commit b432d4a

File tree

3 files changed

+132
-7
lines changed

3 files changed

+132
-7
lines changed

src/cmd_carte.rs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use teloxide::{
2+
dispatching::dialogue::{GetChatId, InMemStorage},
3+
payloads::SendMessageSetters,
4+
prelude::Dialogue,
5+
requests::Requester,
6+
types::{
7+
CallbackQuery, ChatId, InlineKeyboardButton, InlineKeyboardMarkup, Message, MessageId, ReplyMarkup
8+
},
9+
Bot,
10+
};
11+
12+
use crate::HandlerResult;
13+
14+
#[derive(Default, Clone, Debug)]
15+
pub enum CardState {
16+
#[default]
17+
Start,
18+
ChooseCardOption,
19+
GiveCard {
20+
/// ID of the message querying the target of the /carte.
21+
/// Used to delete the message after the selection.
22+
message_id: MessageId,
23+
},
24+
// ReturnCard {
25+
// /// ID of the message.
26+
// /// Used to delete the message after the selection.
27+
// message_id: MessageId,
28+
// },
29+
}
30+
pub type CarteDialogue = Dialogue<CardState, InMemStorage<CardState>>;
31+
32+
33+
// when /carte: message with where the card is (either in office, either in CLIC or the name of the holder)
34+
// if in CLIC, propose to give the card, or to do nothing
35+
// if a holder have the card, propose to give back the card to the office or do nothing
36+
37+
38+
/// Starts the /carte dialogue.
39+
pub async fn start_card_dialogue(
40+
bot: Bot,
41+
msg: Message,
42+
dialogue: CarteDialogue,
43+
) -> HandlerResult {
44+
log::info!("Starting /carte dialogue");
45+
46+
// log::debug!("Removing /carte message");
47+
// bot.delete_message(msg.chat.id, msg.id).await?;
48+
49+
// if there is a holder:
50+
let cartd_status = "random"; // TODO get DB info from who hold the card
51+
52+
log::debug!("Sending message with inline keyboard for callback");
53+
54+
let keyboard: Vec<Vec<InlineKeyboardButton>> = vec![vec![InlineKeyboardButton::callback("Donnez la carte", "give_card"), InlineKeyboardButton::callback("Cancel", "nothing")]];
55+
56+
bot
57+
.send_message(msg.chat.id, cartd_status)
58+
.reply_markup(ReplyMarkup::InlineKeyboard(InlineKeyboardMarkup::new(keyboard)))
59+
.await?;
60+
61+
log::debug!("Updating dialogue to ChooseTarget");
62+
dialogue
63+
.update(CardState::ChooseCardOption)
64+
.await?;
65+
66+
Ok(())
67+
}
68+
69+
70+
/// Handles the callback from the inline keyboard, and sends a message to confirm selection of option.
71+
/// The CallbackQuery data contains the action to perform.
72+
pub async fn choose_option(
73+
bot: Bot,
74+
callback_query: CallbackQuery,
75+
dialogue: CarteDialogue,
76+
message_id: MessageId,
77+
) -> HandlerResult {
78+
if let Some(id) = callback_query.chat_id() {
79+
80+
log::debug!("Removing option query message");
81+
bot.delete_message(dialogue.chat_id(), message_id).await?;
82+
83+
log::debug!("Sending target selection message");
84+
85+
return match callback_query.data.unwrap_or_default().as_str() {
86+
"give_card" => {
87+
88+
let msg = bot.send_message(id, "Qui prend la carte ?").await?;
89+
dialogue.update(CardState::GiveCard { message_id: msg.id }).await?;
90+
Ok(())
91+
},
92+
"return_card" => return_card(bot, id).await,
93+
_ => Ok(()), // TODO close the dialogue
94+
}
95+
}
96+
97+
Ok(())
98+
}
99+
100+
101+
pub async fn give_card(
102+
bot: Bot,
103+
msg: Message,
104+
dialogue: CarteDialogue
105+
) -> HandlerResult {
106+
107+
let card_holder = msg.text();
108+
109+
// TODO set holder in the db
110+
bot.send_message(dialogue.chat_id(), "{} est désormais en posséssion de la carte invité").await?;
111+
112+
113+
Ok(())
114+
}
115+
116+
async fn return_card(bot: Bot, chat_id: ChatId) -> HandlerResult {
117+
bot.send_message(chat_id, "{} a rendu la carte au bureau !").await?;
118+
Ok(()) // TODO change state in the db to bureau
119+
}

src/commands.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ use teloxide::{
1212
use crate::{
1313
cmd_authentication::{
1414
admin_list, admin_remove, authenticate, authorizations, authorize, unauthorize
15-
},
16-
cmd_bureau::bureau,
17-
cmd_poll::{
15+
}, cmd_bureau::bureau, cmd_carte::{choose_option, give_card, start_card_dialogue, CardState}, cmd_poll::{
1816
choose_target,
1917
set_quote,
2018
start_poll_dialogue,
2119
stats, PollState
22-
},
23-
HandlerResult
20+
}, HandlerResult
2421
};
2522

2623
pub fn command_message_handler(
@@ -35,7 +32,8 @@ pub fn command_message_handler(
3532
require_authorization()
3633
.branch(dptree::case![Command::Bureau].endpoint(bureau))
3734
.branch(dptree::case![Command::Poll].endpoint(start_poll_dialogue))
38-
.branch(dptree::case![Command::Stats].endpoint(stats)),
35+
.branch(dptree::case![Command::Stats].endpoint(stats))
36+
.branch(dptree::case![Command::Carte].endpoint(start_card_dialogue)),
3937
)
4038
.branch(
4139
require_admin().chain(
@@ -55,11 +53,15 @@ pub fn command_message_handler(
5553
),
5654
)
5755
.branch(dptree::case![PollState::SetQuote { message_id, target }].endpoint(set_quote))
56+
.branch(dptree::case![CardState::GiveCard { message_id }].endpoint(give_card))
57+
5858
}
5959

6060
pub fn command_callback_query_handler(
6161
) -> Endpoint<'static, DependencyMap, HandlerResult, DpHandlerDescription> {
62-
dptree::case![PollState::ChooseTarget { message_id }].endpoint(choose_target)
62+
dptree::entry()
63+
.branch(dptree::case![PollState::ChooseTarget { message_id }].endpoint(choose_target))
64+
.branch(dptree::case![CardState::ChooseCardOption].endpoint(choose_option))
6365
}
6466

6567
// ----------------------------- ACCESS CONTROL -------------------------------
@@ -147,6 +149,8 @@ pub enum Command {
147149
Authorizations,
148150
#[command(description = "(Admin) Affiche les stats des membres du comité")]
149151
Stats,
152+
#[command(description = "Traque la carte invité CLIC")]
153+
Carte
150154
}
151155

152156
impl Command {
@@ -163,6 +167,7 @@ impl Command {
163167
Self::Unauthorize(..) => "unauthorize",
164168
Self::Authorizations => "authorizations",
165169
Self::Stats => "stats",
170+
Self::Carte => "carte",
166171
}
167172
}
168173
}

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod directus;
2020
mod cmd_poll;
2121
mod cmd_bureau;
2222
mod cmd_authentication;
23+
mod cmd_carte;
2324

2425
pub type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
2526

0 commit comments

Comments
 (0)