From d75b64e40a2d5a26151dd5329cf77a9e4076acca Mon Sep 17 00:00:00 2001 From: EAimTY Date: Fri, 29 Oct 2021 23:42:39 +0900 Subject: [PATCH] nicer descriptions --- Cargo.toml | 2 +- README.md | 12 ++- src/bot.rs | 233 ++++++++++++++++++++++++++++------------------------- 3 files changed, 133 insertions(+), 114 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 259c982..a0c5caa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bing-dict-telegram-bot" -description = "A Telegram bot uses Bing Dictionary to translate words from Chinese to English or English to Chinese" +description = "A Telegram bot uses Bing Dictionary to translate words and phrases from Chinese to English or English to Chinese" version = "0.2.2" authors = ["EAimTY "] edition = "2021" diff --git a/README.md b/README.md index dcde635..c8ee432 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # bing-dict-telegram-bot -A Telegram bot uses Bing Dictionary to translate words from Chinese to English or English to Chinese. +A Telegram bot uses Bing Dictionary to translate words and phrases from Chinese to English or English to Chinese. ## Usage @@ -16,6 +16,16 @@ Options: -h, --help print this help menu ``` +In chat: + +``` +/dict [word / phrase] - Translate a word / phrase +/toggle_command - Toggle translate-all-messages mode for the current chat (default: off) +/toggle_mention - Toggle if I should only react to non-command messages that mentions me in the group. You still need to @ me when using command (default: on) +/about - About this bot +/help - Get this help message +``` + ## Build ```bash diff --git a/src/bot.rs b/src/bot.rs index ab6873c..8fe456d 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -23,10 +23,12 @@ pub async fn run(config: &Config) -> Result<(), Error> { let bot_info = api.execute(GetMe).await?; if config.webhook == 0 { + println!("Running in longpoll mode"); LongPoll::new(api.clone(), Handler::new(api, bot_info)) .run() .await; } else { + println!("Running in webhook mode on port {}", config.webhook); webhook::run_server( ([0, 0, 0, 0], config.webhook), "/", @@ -68,16 +70,22 @@ impl UpdateHandler for Handler { Box::pin(async move { if let UpdateKind::Message(message) = update.kind { if let Some(text) = message.get_text() { + // Only handle messages that have text let chat_id = message.get_chat_id(); let mut result = None; if !text.data.starts_with('/') { + // The message is not a command let command_toggle = context.command_toggle.read().await; + + // Only handle the message if the chat is in the non-command-triggering list if command_toggle.contains(&chat_id) { let text = text.data.trim(); let mut input = None; let mention_toggle = context.mention_toggle.read().await; + + // Check if the chat is in the triggering-without-mention list or the chat is private if mention_toggle.contains(&chat_id) || (!matches!(message.kind, MessageKind::Group { .. }) && !matches!(message.kind, MessageKind::Supergroup { .. })) @@ -85,6 +93,7 @@ impl UpdateHandler for Handler { input = Some(text); } + // Trim the argument that mentions the bot if text.starts_with(&context.bot_username) { input = Some(text[context.bot_username.len()..].trim()); } else if text.ends_with(&context.bot_username) { @@ -108,151 +117,151 @@ impl UpdateHandler for Handler { } } } - } else { - if let Ok(command) = Command::try_from(message) { - #[derive(PartialEq)] - enum ArgPos { - Left, - Right, - None, - } + } else if let Ok(command) = Command::try_from(message) { + // The Position of the argument that mentions the bot in the command + #[derive(PartialEq)] + enum ArgPos { + Left, + Right, + None, + } - let mut pos = ArgPos::None; + let mut pos = ArgPos::None; - if command.get_args().first() == Some(&context.bot_username) { - pos = ArgPos::Left; - } else if command.get_args().last() == Some(&context.bot_username) { - pos = ArgPos::Right; - } + // Get the argument position + if command.get_args().first() == Some(&context.bot_username) { + pos = ArgPos::Left; + } else if command.get_args().last() == Some(&context.bot_username) { + pos = ArgPos::Right; + } - if pos != ArgPos::None - || (!matches!( - command.get_message().kind, - MessageKind::Group { .. } - ) && !matches!( + // Only handle the command if the chat is private or there is a argument that mentions the bot in the command + if pos != ArgPos::None + || (!matches!(command.get_message().kind, MessageKind::Group { .. }) + && !matches!( command.get_message().kind, MessageKind::Supergroup { .. } )) - { - match command.get_name() { - "/dict" => { - let input; - - match pos { - ArgPos::Left => { - input = Some( - command.get_message().get_text().unwrap().data - [5..] - .trim() - .trim_start_matches(&context.bot_username) - .trim(), - ) - } - ArgPos::Right => { - input = Some( - command.get_message().get_text().unwrap().data - [5..] - .trim() - .trim_end_matches(&context.bot_username) - .trim(), - ) - } - ArgPos::None => { - input = Some( - command.get_message().get_text().unwrap().data - [5..] - .trim(), - ) - } + { + match command.get_name() { + "/dict" => { + let input; + + match pos { + // Trim the command and the argument that mentions the bot + ArgPos::Left => { + input = Some( + command.get_message().get_text().unwrap().data[5..] + .trim() + .trim_start_matches(&context.bot_username) + .trim(), + ) } - - if let Some(input) = input { - if !input.is_empty() { - result = match bing_dict::translate(input).await { - Ok(result) => { - Some(result.unwrap_or_else(|| { - String::from("No paraphrase found") - })) - } - Err(err) => { - eprintln!("{}", err); - return; - } - }; - } else { - result = Some(String::from("No input")); - } + ArgPos::Right => { + input = Some( + command.get_message().get_text().unwrap().data[5..] + .trim() + .trim_end_matches(&context.bot_username) + .trim(), + ) + } + // No mentioning argument found, so this message must be sent in a private chat + // Trim the command + ArgPos::None => { + input = Some( + command.get_message().get_text().unwrap().data[5..] + .trim(), + ) } } - "/toggle_command" => { - let mut command_toggle = - context.command_toggle.write().await; - if command_toggle.insert(chat_id) { - result = Some(String::from("Okay. I will translate all non-command messages you send")); + if let Some(input) = input { + if !input.is_empty() { + result = match bing_dict::translate(input).await { + Ok(result) => Some(result.unwrap_or_else(|| { + String::from("No paraphrase found") + })), + Err(err) => { + eprintln!("{}", err); + return; + } + }; } else { - command_toggle.remove(&chat_id); - result = Some(String::from("OK. I will only translate the words after the /dict command")); + result = Some(String::from("No input")); } } + } + + "/toggle_command" => { + let mut command_toggle = context.command_toggle.write().await; + if command_toggle.insert(chat_id) { + result = Some(String::from("Okay. I will translate all non-command messages you send")); + } else { + command_toggle.remove(&chat_id); + result = Some(String::from("OK. I will only translate the words after the /dict command")); + } + } - "/toggle_mention" => { - if matches!( - command.get_message().kind, - MessageKind::Group { .. } - ) || matches!( - command.get_message().kind, - MessageKind::Supergroup { .. } - ) { - let mut mention_toggle = - context.mention_toggle.write().await; - if mention_toggle.insert(chat_id) { - result = Some(String::from("Okay. Now you don't need to @ me to trigger a non-command message translation anymore")); - } else { - mention_toggle.remove(&chat_id); - result = Some(String::from("Fine. I will only react to non-command messages that mentioned me")); - } + "/toggle_mention" => { + // Only handle the command in group chats + if matches!( + command.get_message().kind, + MessageKind::Group { .. } + ) || matches!( + command.get_message().kind, + MessageKind::Supergroup { .. } + ) { + let mut mention_toggle = + context.mention_toggle.write().await; + if mention_toggle.insert(chat_id) { + result = Some(String::from("Okay. Now you don't need to @ me to trigger a non-command message translation anymore")); } else { - result = Some(String::from("This is not a group chat")); + mention_toggle.remove(&chat_id); + result = Some(String::from("Fine. I will only react to non-command messages that mentioned me")); } + } else { + result = + Some(String::from("Hmm...This is not a group chat")); } + } - "/start" => { - result = Some(String::from( - r#" + "/start" => { + result = Some(String::from( + r#" This is a Telegram bot uses Bing Dictionary to translate words from Chinese to English or English to Chinese. -/dict [word] - translate a word -/toggle - toggle translate-all-messages mode for current chat +/dict [word / phrase] - Translate a word / phrase +/toggle_command - Toggle translate-all-messages mode for the current chat (default: off) +/toggle_mention - Toggle if I should only react to non-command messages that mentions me in the group. You still need to @ me when using command (default: on) Use "/help" to get more information. "#, - )); - } + )); + } - "/about" => { - result = Some(String::from( - r#" + "/about" => { + result = Some(String::from( + r#" A Telegram bot uses Bing Dictionary to translate words from Chinese to English or English to Chinese. https://github.com/EAimTY/bing-dict-telegram-bot "#, - )); - } + )); + } - "/help" => { - result = Some(String::from( - r#" -/dict [word] - translate a word -/toggle - toggle translate-all-messages mode for current chat + "/help" => { + result = Some(String::from( + r#" +/dict [word / phrase] - Translate a word / phrase +/toggle_command - Toggle translate-all-messages mode for the current chat (default: off) +/toggle_mention - Toggle if I should only react to non-command messages that mentions me in the group. You still need to @ me when using command (default: on) /about - About this bot /help - Get this help message "#, - )); - } - - _ => {} + )); } + + _ => {} } } }