mirror of
https://github.com/house-of-vanity/desubot.git
synced 2025-07-06 20:24:08 +00:00
Add /code feature.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "desubot"
|
||||
version = "0.1.0"
|
||||
version = "0.5.0"
|
||||
authors = ["AB <ab@hexor.ru>"]
|
||||
edition = "2018"
|
||||
|
||||
@ -32,4 +32,9 @@ markov = "1.1.0"
|
||||
rand = "0.7.3"
|
||||
mystem = "0.2.1"
|
||||
async-trait = "0.1.42"
|
||||
sqlparser = "0.7.0"
|
||||
sqlparser = "0.7.0"
|
||||
|
||||
[dependencies.syntect]
|
||||
version = "4.4"
|
||||
default-features = false
|
||||
features = ["parsing", "dump-load", "regex-onig"]
|
2
README
2
README
@ -9,7 +9,7 @@ Telegram bot with light group statistic and heavy spy features.
|
||||
* Generate sentences using Markov Chains trained on history with /markov_all.
|
||||
|
||||
== TODO ==
|
||||
* Syntax highlighting for code exported to image.
|
||||
* Syntax highlighting for CODE exported to image.
|
||||
|
||||
== Important ==
|
||||
* Desubot uses MyStem by Yandex for word stemming and assume that mystem binary is available in PATH.
|
||||
|
36
assets/help_text.rs
Normal file
36
assets/help_text.rs
Normal file
@ -0,0 +1,36 @@
|
||||
static CODE: &str = "<b>Code highlighter</b>
|
||||
|
||||
<i>Usage</i>
|
||||
<pre>/CODE
|
||||
#<theme - Dracula by default>
|
||||
<CODE>
|
||||
#<lang - JS by default></pre>
|
||||
|
||||
Language may be defined by both name and extension - Rust, rs...
|
||||
Max lines - 80
|
||||
|
||||
List of themes:
|
||||
1337
|
||||
DarkNeon
|
||||
Dracula
|
||||
GitHub
|
||||
Monokai_Extended
|
||||
Monokai_Extended_Bright
|
||||
Monokai_Extended_Light
|
||||
Monokai_Extended_Origin
|
||||
Nord
|
||||
OneHalfDark
|
||||
OneHalfLight
|
||||
Solarized_(dark)
|
||||
Solarized_(light)
|
||||
Sublime_Snazzy
|
||||
TwoDark
|
||||
ansi-dark
|
||||
ansi-light
|
||||
base16
|
||||
base16-256
|
||||
gruvbox
|
||||
gruvbox-light
|
||||
gruvbox-white
|
||||
zenburn
|
||||
";
|
1210
src/commands.rs
1210
src/commands.rs
File diff suppressed because it is too large
Load Diff
@ -348,10 +348,9 @@ pub(crate) async fn get_file(file_id: String) -> Result<i64, errors::Error> {
|
||||
file_rowid
|
||||
}
|
||||
|
||||
async fn add_word(word: &String) -> Result<i64, errors::Error> {
|
||||
match get_stop_word(&word).await {
|
||||
Err(_) => return Err(errors::Error::WordInStopList),
|
||||
_ => {}
|
||||
async fn add_word(word: &str) -> Result<i64, errors::Error> {
|
||||
if get_stop_word(&word).await.is_err() {
|
||||
return Err(errors::Error::WordInStopList);
|
||||
}
|
||||
let conn = open()?;
|
||||
let word_rowid =
|
||||
@ -365,7 +364,7 @@ async fn add_word(word: &String) -> Result<i64, errors::Error> {
|
||||
Ok(word_rowid)
|
||||
}
|
||||
|
||||
async fn get_stop_word(stop_word: &String) -> Result<(), errors::Error> {
|
||||
async fn get_stop_word(stop_word: &str) -> Result<(), errors::Error> {
|
||||
let conn = open()?;
|
||||
match conn.execute_named(
|
||||
"SELECT rowid FROM stop_words WHERE word = (:stop_word)",
|
||||
|
@ -23,11 +23,39 @@ pub enum Error {
|
||||
SQLBannedCommand(String),
|
||||
SQLInvalidCommand,
|
||||
SQLResultTooLong(String),
|
||||
CodeHighlightningError,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "An error occurred.")
|
||||
// match self {
|
||||
// _ => write!(f, "An error occurred."),
|
||||
// // Error::UserNotFound => {}
|
||||
// // Error::SQLITE3Error(_) => {}
|
||||
// // Error::TelegramError(_) => {}
|
||||
// // Error::ReqwestError(_) => {}
|
||||
// // Error::ConfNotFound => {}
|
||||
// // Error::WordNotFound => {}
|
||||
// // Error::WordInStopList => {}
|
||||
// // Error::IOError(_) => {}
|
||||
// // Error::FileNotFound => {}
|
||||
// // Error::JsonParseError(_) => {}
|
||||
// // Error::PopenError(_) => {}
|
||||
// // Error::MystemError(_) => {}
|
||||
// // Error::SQLBannedCommand(_) => {}
|
||||
// // Error::SQLInvalidCommand => {}
|
||||
// // Error::SQLResultTooLong(_) => {}
|
||||
// // Error::CodeHighlightningError(Help) => write!(f, "Code highlighter.\
|
||||
// // <b>Usage</b><pre>/CODE\
|
||||
// // #<theme>\
|
||||
// // <CODE>\
|
||||
// // #<lang></pre>\
|
||||
// // \
|
||||
// // List of themes:\
|
||||
// // .")
|
||||
// Error::CodeHighlightningError(help) => write!(f, "{}", help.description)
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
386
src/handlers.rs
386
src/handlers.rs
@ -1,184 +1,202 @@
|
||||
//use crate::commands::Command;
|
||||
use crate::commands::{Code, Execute, Here, Markov, MarkovAll, Omedeto, Sql, Top};
|
||||
use crate::db;
|
||||
use crate::errors;
|
||||
use crate::utils;
|
||||
use mystem::MyStem;
|
||||
use telegram_bot::*;
|
||||
|
||||
pub async fn handler(
|
||||
api: Api,
|
||||
message: Message,
|
||||
token: String,
|
||||
mystem: &mut MyStem,
|
||||
me: User,
|
||||
) -> Result<(), errors::Error> {
|
||||
match message.kind {
|
||||
MessageKind::Text { ref data, .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
data
|
||||
);
|
||||
db::add_sentence(&message, mystem).await?;
|
||||
let cleaned_message = data.replace(&format!("@{}", me.clone().username.unwrap()), "");
|
||||
match cleaned_message.as_str() {
|
||||
s if s.contains("/here") => {
|
||||
Here {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
s if s.to_string().starts_with("/sql") => match {
|
||||
Sql {
|
||||
data: s.replace("/sql ", ""),
|
||||
}
|
||||
.exec_with_result(&api, &message)
|
||||
.await
|
||||
} {
|
||||
Ok(msg) => {
|
||||
let _ = api
|
||||
.send(
|
||||
message
|
||||
.text_reply(msg)
|
||||
.parse_mode(ParseMode::Html),
|
||||
)
|
||||
.await?;
|
||||
},
|
||||
Err(e) => {
|
||||
let _ = api
|
||||
.send(
|
||||
message
|
||||
.text_reply(format!("Error: {:#?}", e))
|
||||
.parse_mode(ParseMode::Html),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
},
|
||||
s if s.to_string().starts_with("/code") => {
|
||||
Code {
|
||||
data: s.to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/top" => {
|
||||
Top {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/stat" => {
|
||||
Top {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/markov_all" => {
|
||||
MarkovAll {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/markov" => {
|
||||
Markov {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/omedeto" => {
|
||||
Omedeto {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec_mystem(&api, &message, mystem)
|
||||
.await?
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
MessageKind::Photo { ref caption, .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *PHOTO* {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
caption.clone().unwrap_or("NO_TITLE".to_string())
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Document { ref caption, .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *DOCUMENT* {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
caption.clone().unwrap_or("NO_TITLE".to_string())
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Sticker { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *STICKER*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Voice { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *VOICE*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Video { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *VIDEO*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::VideoNote { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *VIDEO_NOTE*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
//use crate::commands::Command;
|
||||
use crate::commands::{Code, Execute, Here, Markov, MarkovAll, Omedeto, Sql, Top};
|
||||
use crate::db;
|
||||
use crate::errors;
|
||||
use crate::utils;
|
||||
use mystem::MyStem;
|
||||
use telegram_bot::*;
|
||||
|
||||
|
||||
include!("../assets/help_text.rs");
|
||||
|
||||
pub async fn handler(
|
||||
api: Api,
|
||||
message: Message,
|
||||
token: String,
|
||||
mystem: &mut MyStem,
|
||||
me: User,
|
||||
) -> Result<(), errors::Error> {
|
||||
match message.kind {
|
||||
MessageKind::Text { ref data, .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
{if data.len() <= 200 {data} else {&data[..200]}}.replace("\n", " ")
|
||||
);
|
||||
|
||||
let cleaned_message = data.replace(&format!("@{}", me.clone().username.unwrap()), "");
|
||||
match cleaned_message.as_str() {
|
||||
s if s.to_string().starts_with("/code") => {
|
||||
|
||||
match {
|
||||
Code {
|
||||
data: s.replace("/code", ""),
|
||||
}
|
||||
.exec_with_result(&api, &message)
|
||||
.await
|
||||
} {
|
||||
Ok(path) => {
|
||||
let file = InputFileUpload::with_path(path.clone());
|
||||
// api.send(message.chat.document(&file)).await?;
|
||||
//
|
||||
// // Send an image from disk
|
||||
api.send(message.chat.photo(&file)).await?;
|
||||
//debug!("{:#?}", formatter);
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
Err(_) => {
|
||||
let _ = api
|
||||
.send(message.text_reply(CODE).parse_mode(ParseMode::Html))
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
s if s.contains("/here") => {
|
||||
db::add_sentence(&message, mystem).await?;
|
||||
Here {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
s if s.to_string().starts_with("/sql") => match {
|
||||
Sql {
|
||||
data: s.replace("/sql ", ""),
|
||||
}
|
||||
.exec_with_result(&api, &message)
|
||||
.await
|
||||
} {
|
||||
Ok(msg) => {
|
||||
let _ = api
|
||||
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
|
||||
.await?;
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = api
|
||||
.send(
|
||||
message
|
||||
.text_reply(format!("Error: {:#?}", e))
|
||||
.parse_mode(ParseMode::Html),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
},
|
||||
"/top" => {
|
||||
Top {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/stat" => {
|
||||
Top {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/markov_all" => {
|
||||
MarkovAll {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/markov" => {
|
||||
Markov {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec(&api, &message)
|
||||
.await?
|
||||
}
|
||||
"/omedeto" => {
|
||||
Omedeto {
|
||||
data: "".to_string(),
|
||||
}
|
||||
.exec_mystem(&api, &message, mystem)
|
||||
.await?
|
||||
}
|
||||
_ => db::add_sentence(&message, mystem).await?,
|
||||
}
|
||||
}
|
||||
MessageKind::Photo { ref caption, .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *PHOTO* {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
caption.clone().unwrap_or_else(|| "NO_TITLE".to_string())
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Document { ref caption, .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *DOCUMENT* {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
caption.clone().unwrap_or_else(|| "NO_TITLE".to_string())
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Sticker { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *STICKER*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Voice { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *VOICE*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::Video { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *VIDEO*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
|
||||
MessageKind::VideoNote { .. } => {
|
||||
let title = utils::get_title(&message);
|
||||
info!(
|
||||
"<{}({})>[{}({})]: *VIDEO_NOTE*",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
);
|
||||
utils::get_files(api, message, token).await?;
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
@ -18,10 +18,9 @@ pub(crate) fn get_title(message: &Message) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn create_dir(dir: &String) {
|
||||
match fs_create_dir(dir) {
|
||||
Ok(_) => info!("Dir {} created.", dir),
|
||||
Err(_) => (),
|
||||
pub(crate) async fn create_dir(dir: &str) {
|
||||
if fs_create_dir(dir).is_ok() {
|
||||
info!("Dir {} created.", dir)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user