소스 검색

[Telegram] /accounts command added to list user expense accounts

Slava Barinov 2 년 전
부모
커밋
fc9c7661b9
1개의 변경된 파일27개의 추가작업 그리고 0개의 파일을 삭제
  1. 27 0
      src/telegram.rs

+ 27 - 0
src/telegram.rs

@@ -77,6 +77,9 @@ enum Command {
 
     #[command(description = "Create new account")]
     NewAccount { account: String },
+
+    #[command(description = "List accounts")]
+    Accounts,
 }
 
 async fn command_handler(
@@ -115,6 +118,30 @@ async fn command_handler(
             bot.send_message(msg.chat.id, "Account added".to_string())
                 .await?
         }
+        Command::Accounts => {
+            let user = User::new(msg.chat.id.0, &None);
+
+            let list = |expense_set: &HashSet<String>| {
+                let mut sorted_expenses: Vec<String> = expense_set
+                    .iter()
+                    .filter(|s| s.starts_with("Expenses:"))
+                    .map(|s| s.trim_start_matches("Expenses:").to_owned())
+                    .collect();
+
+                sorted_expenses.sort();
+
+                sorted_expenses
+                    .into_iter()
+                    .map(|s| s + "\n")
+                    .collect::<String>()
+            };
+
+            bot.send_message(
+                msg.chat.id,
+                format!("Expense accounts:\n\n{}", list(&user.accounts)),
+            )
+            .await?
+        }
     };
 
     Ok(())