Jelajahi Sumber

[Telegram] Support for fully automatical categorization added

Slava Barinov 2 tahun lalu
induk
melakukan
3d9d299086
3 mengubah file dengan 38 tambahan dan 8 penghapusan
  1. 16 0
      src/convert.rs
  2. 1 0
      src/monitoring.rs
  3. 21 8
      src/telegram.rs

+ 16 - 0
src/convert.rs

@@ -5,6 +5,7 @@ use crate::receipt;
 use crate::user::User;
 use chrono::{DateTime, Utc};
 use qif_generator::{account::Account, split::Split, transaction::Transaction};
+use std::collections::HashMap;
 use std::fs;
 
 /// Read json file with receipt and convert it into `receipt::Purchase`
@@ -82,6 +83,21 @@ pub fn non_cat_items(filename: &str, user: &User) -> Vec<String> {
     result
 }
 
+/// Build a fully automatically categorized list
+#[cfg(feature = "telegram")]
+pub fn auto_cat_items(filename: &str, user: &User) -> Result<HashMap<String, String>, ()> {
+    let file = read_file(filename);
+    let mut result: HashMap<String, String> = HashMap::new();
+    for i in file.items {
+        if let Some(category) = get_top_category(i.name.as_str(), &user.catmap) {
+            result.insert(i.name, category.to_string());
+        } else {
+            return Err(());
+        }
+    }
+    Ok(result)
+}
+
 /// Convert `filename` into a QIF transaction
 pub fn convert<'a, F, C>(
     filename: &'a str,

+ 1 - 0
src/monitoring.rs

@@ -37,6 +37,7 @@ fn register_custom_metrics() {
     // Add more metrics here as needed
 }
 
+#[cfg(not(tarpaulin_include))]
 pub async fn web_main() {
     register_custom_metrics();
 

+ 21 - 8
src/telegram.rs

@@ -1,5 +1,5 @@
 use crate::categories;
-use crate::convert::{convert, non_cat_items};
+use crate::convert::{auto_cat_items, convert, non_cat_items};
 use qif_generator::account::{Account, AccountType};
 
 #[cfg(feature = "monitoring")]
@@ -234,13 +234,8 @@ async fn handle_json(
     }
 
     if let Ok(newfile) = download_file(&bot, &file_id).await {
-        bot.send_message(msg.chat.id, format!("File received: {:} ", newfile))
-            .await?;
+        log::info!("Active user: {:} File received: {:} ", msg.chat.id, newfile);
         let user = User::new(msg.chat.id.0, &None);
-        bot.send_message(msg.chat.id, format!("Active user: {:} ", msg.chat.id))
-            .await?;
-        let filepath = format!("{}.qif", &newfile);
-        log::info!("Received file {}", &filepath);
         let mut i = non_cat_items(&newfile, &user);
         if let Some(item) = i.pop() {
             log::info!("No category for {}", &item);
@@ -258,7 +253,25 @@ async fn handle_json(
                 })
                 .await?;
         } else {
-            log::info!("Empty state 2");
+            log::info!("No items to pop");
+            if let Ok(items) = auto_cat_items(&newfile, &user) {
+                bot.send_message(
+                    msg.chat.id,
+                    "All the items were categorized automatically\nEnter the memo line".to_string(),
+                )
+                .await?;
+                dialogue
+                    .update(State::Ready {
+                        filename: newfile,
+                        item_categories: items,
+                    })
+                    .await?;
+            } else {
+                log::warn!("Malformed json or categorization problem");
+                bot.send_message(msg.chat.id, "Can't parse the provided file".to_string())
+                    .await?;
+                dialogue.update(State::Idle).await?;
+            }
         }
     }
     Ok(())