Prechádzať zdrojové kódy

[Telegram] Add telegram bot support

Signed-off-by: Slava Barinov <rayslava@gmail.com>
Slava Barinov 4 rokov pred
rodič
commit
7a4eeb1b2f
4 zmenil súbory, kde vykonal 962 pridanie a 139 odobranie
  1. 898 138
      Cargo.lock
  2. 5 1
      Cargo.toml
  3. 3 0
      src/main.rs
  4. 56 0
      src/telegram.rs

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 898 - 138
Cargo.lock


+ 5 - 1
Cargo.toml

@@ -15,10 +15,14 @@ structopt = "0.3"
 shellexpand = "2.1"
 radix_trie = { version = "0.2", features = ["serde"] }
 libc = "0.2"
+teloxide = { version = "0.4", features = ["auto-send"] }
+log = "0.4.8"
+pretty_env_logger = "0.4.0"
+tokio = { version =  "1.3", features = ["rt-multi-thread", "macros"] }
 
 [build-dependencies]
 cc = "1.0"
 pkg-config = "0.3"
 
 [features]
-tv = []
+tv = []

+ 3 - 0
src/main.rs

@@ -18,6 +18,7 @@ use categories::CatStats;
 
 mod import;
 mod receipt;
+mod telegram;
 mod ui;
 
 fn read_file(f: &str) -> receipt::Purchase {
@@ -129,6 +130,8 @@ fn main() {
         db.set("accounts", &accounts).unwrap();
     }
 
+    telegram::bot();
+
     let purchase = read_file(&args.filename);
     let splits = gen_splits(&purchase.items, &mut catmap);
     let acc = Account::new()

+ 56 - 0
src/telegram.rs

@@ -0,0 +1,56 @@
+// This bot throws a dice on each incoming message.
+
+use teloxide::prelude::*;
+use teloxide::types::*;
+use teloxide::{net::Download, types::File as TgFile, Bot};
+use tokio::fs::File;
+
+#[tokio::main]
+pub async fn bot() {
+    run().await;
+}
+
+async fn run() {
+    teloxide::enable_logging!();
+    log::info!("Starting dices_bot...");
+
+    let bot = Bot::from_env().auto_send();
+
+    teloxide::repl(bot, |message| async move {
+        let update = &message.update;
+        if let MessageKind::Common(msg) = &update.kind {
+            if let MediaKind::Document(doc) = &msg.media_kind {
+                log::info!("{:?}", &doc.document);
+                if let Ok(TgFile {
+                    file_id,
+                    file_path,
+                    file_size,
+                    ..
+                }) = &message
+                    .requester
+                    .get_file(&doc.document.file_id)
+                    .send()
+                    .await
+                {
+                    let filepath = format!("/tmp/{}", file_id);
+                    if let Ok(mut file) = File::create(filepath).await {
+                        if message
+                            .requester
+                            .download_file(&file_path, &mut file)
+                            .await
+                            .is_ok()
+                        {
+                            message
+                                .answer(format!("File received: {:} bytes", file_size))
+                                .await?;
+                        }
+                    }
+                }
+
+                message.answer_dice().await?;
+            }
+        }
+        respond(())
+    })
+    .await;
+}

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov