Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EAimTY committed Oct 29, 2021
1 parent 7619a7c commit 63d0240
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bing-dict-telegram-bot"
description = "A Telegram bot uses Bing Dictionary to translate words and phrases from Chinese to English or English to Chinese"
version = "0.3.0"
version = "0.3.1"
authors = ["EAimTY <ea.imty@gmail.com>"]
edition = "2021"
readme = "README.md"
Expand Down
39 changes: 22 additions & 17 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ impl UpdateHandler for Handler {

// Trim the argument that mentions the bot
if text.starts_with(&context.bot_username) {
input = Some(text[context.bot_username.len()..].trim());
input = Some(text[context.bot_username.len()..].trim_start());
} else if text.ends_with(&context.bot_username) {
input =
Some(text[..text.len() - context.bot_username.len()].trim());
input = Some(
text[..text.len() - context.bot_username.len()].trim_end(),
);
}

if let Some(input) = input {
Expand All @@ -131,18 +132,22 @@ impl UpdateHandler for Handler {
// The Position of the argument that mentions the bot in the command
#[derive(PartialEq)]
enum ArgPos {
Left,
Right,
Start,
End,
None,
}

let text = &command.get_message().get_text().unwrap().data
[command.get_name().len()..]
.trim_start();

let mut pos = ArgPos::None;

// 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 text.starts_with(&context.bot_username) {
pos = ArgPos::Start;
} else if text.ends_with(&context.bot_username) {
pos = ArgPos::End;
}

// Only handle the command if the chat is private or there is a argument that mentions the bot in the command
Expand All @@ -159,28 +164,28 @@ impl UpdateHandler for Handler {

match pos {
// Trim the command and the argument that mentions the bot
ArgPos::Left => {
ArgPos::Start => {
input = Some(
command.get_message().get_text().unwrap().data[5..]
.trim()
.trim_start()
.trim_start_matches(&context.bot_username)
.trim(),
.trim_start(),
)
}
ArgPos::Right => {
ArgPos::End => {
input = Some(
command.get_message().get_text().unwrap().data[5..]
.trim()
.trim_start()
.trim_end_matches(&context.bot_username)
.trim(),
.trim_end(),
)
}
// 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(),
.trim_start(),
)
}
}
Expand Down Expand Up @@ -238,7 +243,7 @@ impl UpdateHandler for Handler {
"/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.
This Telegram bot uses Bing Dictionary to translate words from Chinese to English or English to Chinese.
/dict [word / phrase] - Translate a word or phrase
/toggle_command - Toggle translate-all-messages mode for the current chat (default: off)
Expand Down

0 comments on commit 63d0240

Please sign in to comment.