-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: skeleton of card cmd, without db interaction #6
Draft
NoeTerrier
wants to merge
2
commits into
main
Choose a base branch
from
3-add-carte-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use teloxide::{ | ||
dispatching::dialogue::{GetChatId, InMemStorage}, | ||
payloads::SendMessageSetters, | ||
prelude::Dialogue, | ||
requests::Requester, | ||
types::{ | ||
CallbackQuery, ChatId, InlineKeyboardButton, InlineKeyboardMarkup, Message, MessageId, ReplyMarkup | ||
}, | ||
Bot, | ||
}; | ||
|
||
use crate::HandlerResult; | ||
|
||
#[derive(Default, Clone, Debug)] | ||
pub enum CardState { | ||
#[default] | ||
Start, | ||
ChooseCardOption, | ||
GiveCard { | ||
/// ID of the message querying the target of the /carte. | ||
/// Used to delete the message after the selection. | ||
message_id: MessageId, | ||
}, | ||
// ReturnCard { | ||
// /// ID of the message. | ||
// /// Used to delete the message after the selection. | ||
// message_id: MessageId, | ||
// }, | ||
} | ||
pub type CarteDialogue = Dialogue<CardState, InMemStorage<CardState>>; | ||
|
||
|
||
// when /carte: message with where the card is (either in office, either in CLIC or the name of the holder) | ||
// if in CLIC, propose to give the card, or to do nothing | ||
// if a holder have the card, propose to give back the card to the office or do nothing | ||
|
||
|
||
/// Starts the /carte dialogue. | ||
pub async fn start_card_dialogue( | ||
bot: Bot, | ||
msg: Message, | ||
dialogue: CarteDialogue, | ||
) -> HandlerResult { | ||
log::info!("Starting /carte dialogue"); | ||
|
||
// log::debug!("Removing /carte message"); | ||
// bot.delete_message(msg.chat.id, msg.id).await?; | ||
|
||
// if there is a holder: | ||
let cartd_status = "CLIC"; // TODO get DB info from who hold the card | ||
|
||
let is_at_office = cartd_status == "CLIC"; | ||
let (text, token) = if is_at_office {("Donner la carte", "give_card")} else {("Rendre la carte", "return_card")}; | ||
|
||
let row = vec![ | ||
InlineKeyboardButton::callback(text, token), | ||
InlineKeyboardButton::callback("Cancel", "nothing") | ||
]; | ||
|
||
log::debug!("Sending message with inline keyboard for callback"); | ||
|
||
bot | ||
.send_message(msg.chat.id, cartd_status) | ||
.reply_markup(ReplyMarkup::InlineKeyboard(InlineKeyboardMarkup::new(vec![row]))) | ||
.await?; | ||
|
||
log::debug!("Updating dialogue to ChooseTarget"); | ||
dialogue | ||
.update(CardState::ChooseCardOption) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
|
||
/// Handles the callback from the inline keyboard, and sends a message to confirm selection of option. | ||
/// The CallbackQuery data contains the action to perform. | ||
pub async fn choose_option( | ||
bot: Bot, | ||
callback_query: CallbackQuery, | ||
dialogue: CarteDialogue, | ||
message_id: MessageId, | ||
) -> HandlerResult { | ||
if let Some(id) = callback_query.chat_id() { | ||
|
||
log::debug!("Removing option query message"); | ||
bot.delete_message(dialogue.chat_id(), message_id).await?; | ||
|
||
log::debug!("Sending target selection message"); | ||
|
||
return match callback_query.data.unwrap_or_default().as_str() { | ||
"give_card" => { | ||
|
||
let msg = bot.send_message(id, "Qui prend la carte ?").await?; | ||
dialogue.update(CardState::GiveCard { message_id: msg.id }).await?; | ||
Ok(()) | ||
}, | ||
"return_card" => return_card(bot, id).await, | ||
_ => Ok(()), // TODO close the dialogue | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
||
pub async fn give_card( | ||
bot: Bot, | ||
msg: Message, | ||
dialogue: CarteDialogue | ||
) -> HandlerResult { | ||
|
||
let card_holder = msg.text(); | ||
|
||
// TODO set holder in the db | ||
bot.send_message(dialogue.chat_id(), "{} est désormais en posséssion de la carte invité").await?; | ||
|
||
|
||
Ok(()) | ||
} | ||
|
||
async fn return_card(bot: Bot, chat_id: ChatId) -> HandlerResult { | ||
bot.send_message(chat_id, "{} a rendu la carte au bureau !").await?; | ||
Ok(()) // TODO change state in the db to bureau | ||
} | ||
NoeTerrier marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to also have the possibility to "forward" the card, e.g. when a commission holds it, they can give it to another one.