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
+ }
0 commit comments