浏览代码

[Telegram] Command parser skeleton added

Signed-off-by: Slava Barinov <rayslava@gmail.com>
Slava Barinov 4 年之前
父节点
当前提交
c9c90fbe1e
共有 3 个文件被更改,包括 41 次插入4 次删除
  1. 12 2
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 28 1
      src/telegram.rs

+ 12 - 2
Cargo.lock

@@ -918,8 +918,6 @@ dependencies = [
 [[package]]
 name = "qif_generator"
 version = "0.1.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f5373139ce3690b7b53490402d93e53f21066e481caf5934026f9811c5912ca9"
 dependencies = [
  "chrono",
 ]
@@ -1317,6 +1315,7 @@ dependencies = [
  "serde_json",
  "serde_with_macros",
  "teloxide-core",
+ "teloxide-macros",
  "thiserror",
  "tokio",
  "tokio-stream",
@@ -1347,6 +1346,17 @@ dependencies = [
  "uuid",
 ]
 
+[[package]]
+name = "teloxide-macros"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44db3d9259fa57c56ab627277e42dc8f0b2c274fa86a502d18ef804ccf0d51f4"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "tempfile"
 version = "3.2.0"

+ 1 - 1
Cargo.toml

@@ -16,7 +16,7 @@ shellexpand = "2.1"
 radix_trie = { version = "0.2", features = ["serde"] }
 libc = { version = "0.2" }
 
-teloxide = { version = "0.4", features = ["auto-send"], optional = true }
+teloxide = { version = "0.4.0", features = ["auto-send", "macros"], optional = true }
 log = { version = "0.4.8", optional = true }
 pretty_env_logger = { version = "0.4.0", optional = true }
 tokio = { version =  "1.3", features = ["rt-multi-thread", "macros"], optional = true }

+ 28 - 1
src/telegram.rs

@@ -1,9 +1,9 @@
 // This bot throws a dice on each incoming message.
 
 use derive_more::From;
-use teloxide::prelude::*;
 use teloxide::types::*;
 use teloxide::{net::Download, types::File as TgFile, Bot};
+use teloxide::{prelude::*, utils::command::BotCommand};
 use teloxide::{DownloadError, RequestError};
 use thiserror::Error;
 use tokio::fs::File;
@@ -29,6 +29,15 @@ enum FileReceiveError {
     Io(#[source] std::io::Error),
 }
 
+#[derive(BotCommand, Debug)]
+#[command(rename = "lowercase", description = "These commands are supported:")]
+enum Command {
+    #[command(description = "display this text.")]
+    Help,
+    #[command(description = "Register new user in bot.")]
+    Start,
+}
+
 #[cfg(feature = "telegram")]
 async fn download_file(downloader: &Bot, file_id: &str) -> Result<String, FileReceiveError> {
     let TgFile {
@@ -60,6 +69,24 @@ async fn run() {
                 }
 
                 message.answer_dice().await?;
+            } else if let Some(line) = message.update.text() {
+                if let Ok(command) = Command::parse(line, "tgqif") {
+                    match command {
+                        Command::Help => {
+                            message.answer(Command::descriptions()).send().await?;
+                        }
+                        Command::Start => {
+                            if let Some(user) = message.update.from() {
+                                message
+                                    .answer(format!(
+                                        "You registered as @{} with id {}.",
+                                        user.first_name, user.id
+                                    ))
+                                    .await?;
+                            }
+                        }
+                    }
+                }
             }
         }
         respond(())