Offtop handler

This commit is contained in:
Nefrace 2022-10-16 01:38:55 +03:00
parent a9205b6298
commit 379c161c3b
4 changed files with 40 additions and 10 deletions

2
Cargo.lock generated
View File

@ -347,10 +347,12 @@ dependencies = [
name = "godette" name = "godette"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static",
"log", "log",
"pretty_env_logger", "pretty_env_logger",
"teloxide", "teloxide",
"tokio", "tokio",
"url",
] ]
[[package]] [[package]]

View File

@ -9,4 +9,6 @@ edition = "2021"
teloxide = { version = "0.11", features = ["macros", "auto-send"] } teloxide = { version = "0.11", features = ["macros", "auto-send"] }
log = "0.4" log = "0.4"
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] } tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
lazy_static = "1.4.0"
url = "2.3.1"

View File

@ -1,9 +1,15 @@
use teloxide::prelude::*; use teloxide::{
prelude::*,
types::{InlineKeyboardButton, InlineKeyboardMarkup},
};
pub mod commands; pub mod commands;
mod handlers; mod handlers;
mod utils;
use commands::{AdminCommand, Command}; use commands::{AdminCommand, Command};
use url::Url;
use utils::get_text_or_empty;
pub struct Godette { pub struct Godette {
pub bot: Bot, pub bot: Bot,
} }
@ -57,12 +63,23 @@ impl Godette {
pub async fn message_dispatcher(bot: Bot, msg: Message) -> ResponseResult<()> { pub async fn message_dispatcher(bot: Bot, msg: Message) -> ResponseResult<()> {
// Checking if it's a reply // Checking if it's a reply
let message = msg.clone(); let reply = msg.reply_to_message();
let reply = message.reply_to_message();
match reply { match reply {
Some(reply) => return Godette::reply_dispatcher(bot, msg, reply.to_owned()).await, Some(reply) => {
Godette::reply_dispatcher(bot.clone(), msg.clone(), reply.to_owned()).await?
}
None => (), None => (),
}; };
let text = utils::get_text_or_empty(&msg).to_lowercase();
match text.find("оффтоп") {
Some(_) => {
bot.send_message(msg.chat.id, "Вот вам ссылка на оффтоп")
.reply_to_message_id(msg.id)
.reply_markup(Godette::make_offtop_keyboard())
.await?;
}
None => (),
}
Ok(()) Ok(())
} }
@ -78,11 +95,7 @@ impl Godette {
KarmaTrigger::new("👍", 1), KarmaTrigger::new("👍", 1),
KarmaTrigger::new("👎", -1), KarmaTrigger::new("👎", -1),
]; ];
println!("Working on reply"); let text = get_text_or_empty(&msg);
let text = msg
.text()
.unwrap_or(msg.caption().unwrap_or_default())
.to_string();
println!("{:?}", text); println!("{:?}", text);
for trigger in triggers { for trigger in triggers {
match text.to_lowercase().find(&trigger.text) { match text.to_lowercase().find(&trigger.text) {
@ -95,4 +108,10 @@ impl Godette {
} }
Ok(()) Ok(())
} }
fn make_offtop_keyboard() -> InlineKeyboardMarkup {
let link = Url::parse("https://t.me/Godot_Engine_Offtop").unwrap();
let button = InlineKeyboardButton::url("Godot Engine оффтоп чат".to_owned(), link);
return InlineKeyboardMarkup::new(vec![[button]]);
}
} }

7
src/godette/utils.rs Normal file
View File

@ -0,0 +1,7 @@
use teloxide::types::Message;
pub fn get_text_or_empty(msg: &Message) -> String {
msg.text()
.unwrap_or(msg.caption().unwrap_or(""))
.to_string()
}