16 Commits
0.2 ... 0.4.0

Author SHA1 Message Date
AB
a1b272bd40 lint. 2021-01-03 22:42:49 +03:00
AB
3f00505659 Add /sql command. 2021-01-03 22:37:37 +03:00
AB
3fd5b124f3 Bump mystem lib. Lint code. 2020-12-31 01:56:20 +03:00
AB
17442819c4 Rewrite command parsing. 2020-12-31 01:42:36 +03:00
AB
7adc629292 Improve omedeto. Detect feminine by verbs. Fix 2020-12-30 15:25:33 +03:00
AB
39640139fa Improve omedeto. Detect feminine by verbs. 2020-12-30 15:12:17 +03:00
AB
0812c9e371 Improve omedeto. Nouns. 2020-12-30 14:30:53 +03:00
AB
f111f54606 Improve omedeto. Nouns. 2020-12-30 11:56:25 +03:00
AB
7e17851131 Improve omedeto 2020-12-30 11:50:19 +03:00
AB
b674ae5b15 Update omedeto 2020-12-30 09:58:17 +03:00
AB
412c3f313c Fix typo. 2020-12-30 09:47:33 +03:00
AB
1838674cab Fix typo. 2020-12-30 09:38:33 +03:00
AB
30d9d470cd Fix omedeto. 2020-12-30 09:24:15 +03:00
AB
3236131377 Move Mystem to external lib. add /omedeto 2020-12-29 17:09:49 +03:00
AB
9aaa8a94f1 Move Mystem to external lib. add /omedeto 2020-12-29 17:01:56 +03:00
AB
2d43a7d875 Fix mystem exec. 2020-12-16 23:25:25 +03:00
9 changed files with 714 additions and 2258 deletions

2064
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -21,8 +21,9 @@ hyper-tls = { version = "0.4", optional = true }
futures = "0.3" futures = "0.3"
hyper-rustls = { version = "0.19", optional = true } hyper-rustls = { version = "0.19", optional = true }
rusqlite = { version = "0.24.1", features = ["bundled"]} rusqlite = { version = "0.24.2", features = ["bundled"]}
html-escape = "0.2" html-escape = "0.2"
regex = "1"
reqwest = "0.10.9" reqwest = "0.10.9"
uuid = { version = "0.8", features = ["v4"] } uuid = { version = "0.8", features = ["v4"] }
sha1 = "*" sha1 = "*"
@ -32,3 +33,6 @@ subprocess = "0.2.6"
serde_json = "1.0" serde_json = "1.0"
markov = "1.1.0" markov = "1.1.0"
rand = "0.7.3" rand = "0.7.3"
mystem = "0.2.1"
async-trait = "0.1.42"
sqlparser = "0.7.0"

5
README
View File

@ -11,5 +11,6 @@ Telegram bot with light group statistic and heavy spy features.
== TODO == == TODO ==
* Syntax highlighting for code exported to image. * Syntax highlighting for code exported to image.
== Notes == == Important ==
* Desubot uses MyStem by Yandex for word stemming and assume that mystem binary is available in PATH. On Windows it may be placed on working directory. Both Linux and Windows mystem binary is in repo. * Desubot uses MyStem by Yandex for word stemming and assume that mystem binary is available in PATH.
On Windows it may be placed on working directory. Both Linux and Windows mystem binary is in repo.

View File

@ -1,12 +1,240 @@
use crate::db; use crate::db;
use crate::errors::Error; use crate::errors::Error;
use crate::errors::Error::SQLInvalidCommand;
use async_trait::async_trait;
use html_escape::encode_text; use html_escape::encode_text;
use markov::Chain; use markov::Chain;
use mystem::Case::Nominative;
use mystem::Gender::Feminine;
use mystem::MyStem;
use mystem::Person::First;
use mystem::Tense::{Inpresent, Past};
use rand::seq::SliceRandom;
use rand::Rng; use rand::Rng;
use regex::Regex;
use sqlparser::ast::Statement;
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;
use telegram_bot::prelude::*; use telegram_bot::prelude::*;
use telegram_bot::{Api, Message, ParseMode}; use telegram_bot::{Api, Message, ParseMode};
pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> { pub struct Here {
pub data: String,
}
pub struct Top {
pub data: String,
}
pub struct MarkovAll {
pub data: String,
}
pub struct Markov {
pub data: String,
}
pub struct Omedeto {
pub data: String,
}
pub struct Sql {
pub data: String,
}
#[async_trait]
pub trait Execute {
async fn run(&self, api: Api, message: Message) -> Result<(), Error>;
async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error>;
}
#[async_trait]
impl Execute for Sql {
async fn run(&self, api: Api, message: Message) -> Result<(), Error> {
let mut sql = self.data.to_uppercase();
let is_head = if sql.starts_with('-') {
sql = sql.replacen("-", "", 1);
false
} else {
true
};
let dialect = GenericDialect {};
let ast: Result<Vec<Statement>, Error> = match Parser::parse_sql(&dialect, &sql) {
Ok(ast) => Ok(ast),
Err(_) => {
warn!("Invalid SQL - {}", sql);
Err(SQLInvalidCommand)
}
};
let ast = match ast {
Err(_) => {
let _ = api
.send(
message
.text_reply(format!("❌ Invalid SQL. Syntax error ❌"))
.parse_mode(ParseMode::Html),
)
.await;
return Err(SQLInvalidCommand);
}
Ok(ast) => ast,
};
let msg: Result<String, Error> = match ast.len() {
l if l > 1 => {
//Max 1 request per message allowed only.
Err(Error::SQLBannedCommand)
}
_ => match ast[0] {
sqlparser::ast::Statement::Query { .. } => {
let conn = db::open()?;
let x = match conn.prepare_cached(&sql) {
Ok(mut stmt) => {
let query = match stmt.query(rusqlite::NO_PARAMS) {
Err(_) => Err(SQLInvalidCommand),
Ok(mut rows) => {
let mut res: Vec<Vec<String>> = match rows.column_names() {
Some(n) => vec![n
.into_iter()
.map(|s| {
let t = String::from(s);
if t.len() > 10 {
"EMSGSIZE".to_string()
} else {
t
}
})
.collect()],
None => return Err(SQLInvalidCommand),
};
let index_count = match rows.column_count() {
Some(c) => c,
None => return Err(SQLInvalidCommand),
};
while let Some(row) = rows.next().unwrap() {
let mut tmp: Vec<String> = Vec::new();
for i in 0..index_count {
match row.get(i).unwrap_or(None) {
Some(rusqlite::types::Value::Text(t)) => {
tmp.push(t)
}
Some(rusqlite::types::Value::Integer(t)) => {
tmp.push(t.to_string())
}
Some(rusqlite::types::Value::Blob(_)) => {
tmp.push("Binary".to_string())
}
Some(rusqlite::types::Value::Real(t)) => {
tmp.push(t.to_string())
}
Some(rusqlite::types::Value::Null) => {
tmp.push("Null".to_string())
}
None => tmp.push("Null".to_string()),
};
}
res.push(tmp);
}
// add Header
let mut msg = if is_head {
let mut x = String::from("<b>");
for head in res[0].iter() {
x = format!("{} {}", x, head);
}
format!("{}{}", x, "</b>\n")
} else {
String::new()
};
// remove header
res.remove(0);
msg = format!("{}{}", msg, "<pre>");
for line in res.iter() {
for field in line.iter() {
msg = format!("{}{}", msg, format!("{} ", field));
}
msg = format!("{}{}", msg, "\n");
}
msg = format!("{}{}", msg, "</pre>");
msg = if msg.len() > 4096 {
"🚫 Result is too big. Use LIMIT 🚫".into()
} else {
msg
};
Ok(msg)
}
};
query
}
Err(e) => Err(Error::SQLITE3Error(e)),
};
x
}
_ => {
warn!("SELECT requests allowed only.");
Err(Error::SQLBannedCommand)
}
},
};
match msg {
Ok(msg) => {
match api
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
.await
{
Ok(_) => debug!("/sql command sent to {}", message.chat.id()),
Err(_) => warn!("/sql command sent failed to {}", message.chat.id()),
}
}
Err(e) => match e {
Error::SQLITE3Error(e) => {
let _ = api
.send(
message
.text_reply(format!("❌ An error ocurred {}", e))
.parse_mode(ParseMode::Html),
)
.await;
}
Error::SQLBannedCommand => {
let _ = api
.send(
message
.text_reply(format!("🚫 SELECT requests allowed only 🚫"))
.parse_mode(ParseMode::Html),
)
.await;
}
Error::SQLInvalidCommand => {
let _ = api
.send(
message
.text_reply(format!("🚫 Invalid SQL. Check DB scheme. 🚫"))
.parse_mode(ParseMode::Html),
)
.await;
}
_ => {}
},
}
Ok(())
}
#[allow(unused_variables)]
async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for Here {
async fn run(&self, api: Api, message: Message) -> Result<(), Error> {
let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap(); let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap();
for u in &members { for u in &members {
debug!("Found user {:?} in chat {}", u, message.chat.id()); debug!("Found user {:?} in chat {}", u, message.chat.id());
@ -31,12 +259,23 @@ pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> {
Ok(_) => debug!("/here command sent to {}", message.chat.id()), Ok(_) => debug!("/here command sent to {}", message.chat.id()),
Err(_) => warn!("/here command sent failed to {}", message.chat.id()), Err(_) => warn!("/here command sent failed to {}", message.chat.id()),
} }
//api.send(message.chat.text("Text to message chat")).await?;
//api.send(message.from.text("Private text")).await?;
Ok(()) Ok(())
} }
pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> { #[allow(unused_variables)]
async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for Top {
async fn run(&self, api: Api, message: Message) -> Result<(), Error> {
let top = db::get_top(&message).await?; let top = db::get_top(&message).await?;
let mut msg = "<b>Your top using words:</b>\n<pre>".to_string(); let mut msg = "<b>Your top using words:</b>\n<pre>".to_string();
let mut counter = 1; let mut counter = 1;
@ -55,13 +294,24 @@ pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> {
Ok(_) => debug!("/top command sent to {}", message.chat.id()), Ok(_) => debug!("/top command sent to {}", message.chat.id()),
Err(_) => warn!("/top command sent failed to {}", message.chat.id()), Err(_) => warn!("/top command sent failed to {}", message.chat.id()),
} }
//api.send(message.chat.text("Text to message chat")).await?;
//api.send(message.from.text("Private text")).await?;
Ok(()) Ok(())
} }
pub(crate) async fn markov_all(api: Api, message: Message) -> Result<(), Error> { #[allow(unused_variables)]
let messages = db::get_random_messages().await?; async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for MarkovAll {
async fn run(&self, api: Api, message: Message) -> Result<(), Error> {
let messages = db::get_messages_random_all().await?;
let mut chain = Chain::new(); let mut chain = Chain::new();
chain.feed(messages); chain.feed(messages);
let mut sentences = chain.generate(); let mut sentences = chain.generate();
@ -81,8 +331,21 @@ pub(crate) async fn markov_all(api: Api, message: Message) -> Result<(), Error>
Ok(()) Ok(())
} }
pub(crate) async fn markov(api: Api, message: Message) -> Result<(), Error> { #[allow(unused_variables)]
let messages = db::get_random_messages_group(&message).await?; async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for Markov {
async fn run(&self, api: Api, message: Message) -> Result<(), Error> {
let messages = db::get_messages_random_group(&message).await?;
let mut chain = Chain::new(); let mut chain = Chain::new();
chain.feed(messages); chain.feed(messages);
let mut sentences = chain.generate(); let mut sentences = chain.generate();
@ -101,3 +364,219 @@ pub(crate) async fn markov(api: Api, message: Message) -> Result<(), Error> {
//api.send(message.from.text("Private text")).await?; //api.send(message.from.text("Private text")).await?;
Ok(()) Ok(())
} }
#[allow(unused_variables)]
async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for Omedeto {
#[allow(unused_variables)]
async fn run(&self, api: Api, message: Message) -> Result<(), Error> {
unimplemented!()
}
#[warn(unused_must_use)]
async fn run_mystem(
&self,
api: Api,
message: Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
let all_msg = db::get_messages_user_all(&message).await?;
let re = Regex::new(r"^[яЯ] [а-яА-Я]+(-[а-яА-Я]+(_[а-яА-Я]+)*)*").unwrap();
let mut nouns: Vec<String> = all_msg
.clone()
.into_iter()
.filter(|m| re.is_match(m))
.map(|m| m.split(' ').map(|s| s.to_string()).collect::<Vec<String>>()[1].clone())
.filter(|m| {
let stem = mystem.stemming(m.clone()).unwrap_or_default();
if stem.is_empty() {
false
} else if stem[0].lex.is_empty() {
false
} else {
match stem[0].lex[0].grammem.part_of_speech {
mystem::PartOfSpeech::Noun => stem[0].lex[0]
.grammem
.facts
.contains(&mystem::Fact::Case(Nominative)),
_ => false,
}
}
})
.map(|w| w.replace(|z| z == '.' || z == ',', ""))
.collect();
nouns.sort();
nouns.dedup();
nouns.shuffle(&mut rand::thread_rng());
//debug!("Found {} nouns. {:#?}", nouns.len(), nouns);
let mut verbs_p: Vec<String> = all_msg
.clone()
.into_iter()
.filter(|m| re.is_match(m))
.map(|m| m.split(' ').map(|s| s.to_string()).collect::<Vec<String>>()[1].clone())
.filter(|m| {
let stem = mystem.stemming(m.clone()).unwrap_or_default();
if stem.is_empty() {
false
} else if stem[0].lex.is_empty() {
false
} else {
match stem[0].lex[0].grammem.part_of_speech {
mystem::PartOfSpeech::Verb => stem[0].lex[0]
.grammem
.facts
.contains(&mystem::Fact::Tense(Past)),
_ => false,
}
}
})
.map(|w| w.replace(|z| z == '.' || z == ',', ""))
.collect();
verbs_p.sort();
verbs_p.dedup();
verbs_p.shuffle(&mut rand::thread_rng());
//debug!("Found {} past verbs. {:#?}", verbs_p.len(), verbs_p);
let mut verbs_i: Vec<String> = all_msg
.clone()
.into_iter()
.filter(|m| re.is_match(m))
.map(|m| m.split(' ').map(|s| s.to_string()).collect::<Vec<String>>()[1].clone())
.filter(|m| {
let stem = mystem.stemming(m.clone()).unwrap_or_default();
if stem.is_empty() {
false
} else if stem[0].lex.is_empty() {
false
} else {
match stem[0].lex[0].grammem.part_of_speech {
mystem::PartOfSpeech::Verb => {
stem[0].lex[0]
.grammem
.facts
.contains(&mystem::Fact::Tense(Inpresent))
&& stem[0].lex[0]
.grammem
.facts
.contains(&mystem::Fact::Person(First))
}
_ => false,
}
}
})
.map(|w| w.replace(|z| z == '.' || z == ',', ""))
.collect();
verbs_i.sort();
verbs_i.dedup();
verbs_i.shuffle(&mut rand::thread_rng());
//debug!("Found {} inpresent verbs. {:#?}", verbs_i.len(), verbs_i);
if nouns.is_empty() {
nouns.push(message.from.first_name.to_string());
}
let start: Vec<String> = vec![
"С новым годом".into(),
"С НГ тебя".into(),
"Поздравляю".into(),
"Поздравляю с НГ".into(),
];
let placeholders: Vec<String> = vec![
"[ДАННЫЕ УДАЛЕНЫ]".into(),
"[СЕКРЕТНО]".into(),
"[НЕТ ДАННЫХ]".into(),
"[ОШИБКА ДОСТУПА]".into(),
];
//debug!("Nouns: {:#?}", nouns);
//debug!("Verbs: {:#?}", verbs);
let fem = {
let mut fm = 0;
let mut mu = 0;
all_msg
.clone()
.into_iter()
.filter(|m| re.is_match(m))
.map(|m| m.split(' ').map(|s| s.to_string()).collect::<Vec<String>>()[1].clone())
.map(|m| {
let stem = mystem.stemming(m.clone()).unwrap_or_default();
if stem.is_empty() {
()
} else if stem[0].lex.is_empty() {
()
} else {
match stem[0].lex[0].grammem.part_of_speech {
mystem::PartOfSpeech::Verb => {
match stem[0].lex[0]
.grammem
.facts
.contains(&mystem::Fact::Tense(Past))
{
true => {
if stem[0].lex[0]
.grammem
.facts
.contains(&mystem::Fact::Gender(Feminine))
{
fm = fm + 1;
} else {
mu = mu + 1;
}
}
false => (),
}
}
_ => (),
}
}
})
.for_each(drop);
//debug!("fm - {}, mu - {}", fm, mu);
if fm >= mu {
true
} else {
false
}
};
//debug!("Is Feminine - {}", fem);
let result = format!(
"{} {} известн{} как {}, {}, а так же конечно {}. В прошедшем году ты часто давал{} нам знать, что ты {}, {} и {}. Нередко ты говорил{} я {}, я {} или даже я {}. =*",
start.choose(&mut rand::thread_rng()).unwrap(),
message.from.first_name.to_string(),
{ if fem { "ая" } else { "ый" } },
nouns.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
nouns.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
nouns.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
{ if fem { "а" } else { "" } },
verbs_p.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
verbs_p.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
verbs_p.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
{ if fem { "а" } else { "" } },
verbs_i.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
verbs_i.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
verbs_i.pop().unwrap_or(placeholders.choose(&mut rand::thread_rng()).unwrap().to_string()),
);
match api
.send(
message
.text_reply(result.trim())
.parse_mode(ParseMode::Html),
)
.await
{
Ok(_) => debug!("/omedeto command sent to {}", message.chat.id()),
Err(_) => warn!("/omedeto command sent failed to {}", message.chat.id()),
}
Ok(())
}
}

View File

@ -1,5 +1,4 @@
use crate::errors; use crate::errors;
use crate::mystem;
use crate::utils; use crate::utils;
use rusqlite::{named_params, params, Connection, Error, Result}; use rusqlite::{named_params, params, Connection, Error, Result};
use std::time::SystemTime; use std::time::SystemTime;
@ -82,7 +81,7 @@ pub(crate) fn get_conf(id: telegram_bot::ChatId) -> Result<Conf, errors::Error>
} }
} }
/* #[allow(dead_code)]
pub(crate) fn get_confs() -> Result<Vec<Conf>> { pub(crate) fn get_confs() -> Result<Vec<Conf>> {
let conn = open()?; let conn = open()?;
let mut stmt = conn.prepare("SELECT id, title, date FROM conf")?; let mut stmt = conn.prepare("SELECT id, title, date FROM conf")?;
@ -101,8 +100,8 @@ pub(crate) fn get_confs() -> Result<Vec<Conf>> {
Ok(confs) Ok(confs)
} }
*/
pub(crate) async fn get_random_messages() -> Result<Vec<String>, Error> { pub(crate) async fn get_messages_random_all() -> Result<Vec<String>, Error> {
let conn = open()?; let conn = open()?;
let mut stmt = conn.prepare_cached("SELECT text FROM messages ORDER BY RANDOM() LIMIT 50")?; let mut stmt = conn.prepare_cached("SELECT text FROM messages ORDER BY RANDOM() LIMIT 50")?;
let mut rows = stmt.query_named(named_params![])?; let mut rows = stmt.query_named(named_params![])?;
@ -114,17 +113,18 @@ pub(crate) async fn get_random_messages() -> Result<Vec<String>, Error> {
Ok(messages) Ok(messages)
} }
pub(crate) async fn get_random_messages_group( pub(crate) async fn get_messages_random_group(
message: &telegram_bot::Message message: &telegram_bot::Message,
) -> Result<Vec<String>, Error> { ) -> Result<Vec<String>, Error> {
let conf_id = i64::from(message.chat.id()); let conf_id = i64::from(message.chat.id());
let conn = open()?; let conn = open()?;
let mut stmt = conn.prepare_cached(" let mut stmt = conn.prepare_cached(
"
SELECT m.text FROM messages m SELECT m.text FROM messages m
LEFT JOIN relations r ON r.msg_id = m.id LEFT JOIN relations r ON r.msg_id = m.id
WHERE r.conf_id = :conf_id WHERE r.conf_id = :conf_id
ORDER BY RANDOM() LIMIT 50 ORDER BY RANDOM() LIMIT 50
" ",
)?; )?;
let mut rows = stmt.query_named(named_params! {":conf_id": conf_id})?; let mut rows = stmt.query_named(named_params! {":conf_id": conf_id})?;
let mut messages = Vec::new(); let mut messages = Vec::new();
@ -135,6 +135,51 @@ pub(crate) async fn get_random_messages_group(
Ok(messages) Ok(messages)
} }
#[allow(dead_code)]
pub(crate) async fn get_messages_user_group(
message: &telegram_bot::Message,
) -> Result<Vec<String>, Error> {
let conf_id = i64::from(message.chat.id());
let user_id = i64::from(message.from.id);
let conn = open()?;
let mut stmt = conn.prepare_cached(
"
SELECT m.text FROM messages m
LEFT JOIN relations r ON r.msg_id = m.id
WHERE r.conf_id = :conf_id
AND r.user_id = :user_id
",
)?;
let mut rows = stmt.query_named(named_params! {":conf_id": conf_id, ":user_id": user_id})?;
let mut messages = Vec::new();
while let Some(row) = rows.next()? {
messages.push(row.get(0)?)
}
Ok(messages)
}
pub(crate) async fn get_messages_user_all(
message: &telegram_bot::Message,
) -> Result<Vec<String>, Error> {
let user_id = i64::from(message.from.id);
let conn = open()?;
let mut stmt = conn.prepare_cached(
"
SELECT m.text FROM messages m
LEFT JOIN relations r ON r.msg_id = m.id
WHERE r.user_id = :user_id
",
)?;
let mut rows = stmt.query_named(named_params! {":user_id": user_id})?;
let mut messages = Vec::new();
while let Some(row) = rows.next()? {
messages.push(row.get(0)?)
}
Ok(messages)
}
pub(crate) fn get_members(id: telegram_bot::ChatId) -> Result<Vec<telegram_bot::User>> { pub(crate) fn get_members(id: telegram_bot::ChatId) -> Result<Vec<telegram_bot::User>> {
let conn = open()?; let conn = open()?;
let mut stmt = conn.prepare_cached( let mut stmt = conn.prepare_cached(
@ -300,7 +345,6 @@ pub(crate) async fn get_file(file_id: String) -> Result<i64, errors::Error> {
Ok(id) => Ok(id), Ok(id) => Ok(id),
Err(_) => Err(errors::Error::FileNotFound), Err(_) => Err(errors::Error::FileNotFound),
}; };
file_rowid file_rowid
} }
@ -355,6 +399,7 @@ async fn add_relation(word_id: i64, msg_id: i64, message: &Message) -> Result<i6
Ok(rowid) Ok(rowid)
} }
#[allow(unused_must_use)]
pub(crate) async fn add_sentence( pub(crate) async fn add_sentence(
message: &telegram_bot::Message, message: &telegram_bot::Message,
mystem: &mut mystem::MyStem, mystem: &mut mystem::MyStem,
@ -373,18 +418,21 @@ pub(crate) async fn add_sentence(
}; };
// Save stemmed words // Save stemmed words
let words = mystem.stemming(text).await?; let words = mystem.stemming(text)?;
conn.execute("BEGIN TRANSACTION", params![]); conn.execute("BEGIN TRANSACTION", params![]);
for word in words { for word in words {
match add_word(&word).await { if word.lex.is_empty() {
continue;
}
match add_word(&word.lex[0].lex).await {
Ok(id) => { Ok(id) => {
debug!("Added {}: rowid: {}", &word, id); debug!("Added {}: rowid: {}", &word.lex[0].lex, id);
match add_relation(id, msg_rowid, message).await { match add_relation(id, msg_rowid, message).await {
Ok(_) => {} Ok(_) => {}
Err(e) => panic!("SQLITE3 Error: Relations failed: {:?}", e), Err(e) => panic!("SQLITE3 Error: Relations failed: {:?}", e),
} }
} }
Err(_) => debug!("Word {} is in stop list.", &word), Err(_) => debug!("Word {} is in stop list.", &word.lex[0].lex),
} }
} }
conn.execute("END TRANSACTION", params![]); conn.execute("END TRANSACTION", params![]);
@ -423,5 +471,3 @@ pub(crate) async fn get_top(
} }
Ok(top) Ok(top)
} }

View File

@ -1,3 +1,4 @@
use mystem::AppError as mystem_error;
use reqwest::Error as reqwest_error; use reqwest::Error as reqwest_error;
use rusqlite::Error as sqlite_error; use rusqlite::Error as sqlite_error;
use serde_json::Error as serde_error; use serde_json::Error as serde_error;
@ -18,6 +19,9 @@ pub enum Error {
FileNotFound, FileNotFound,
JsonParseError(serde_error), JsonParseError(serde_error),
PopenError(popen_error), PopenError(popen_error),
MystemError(mystem_error),
SQLBannedCommand,
SQLInvalidCommand,
} }
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -60,3 +64,9 @@ impl From<popen_error> for Error {
return Error::PopenError(e); return Error::PopenError(e);
} }
} }
impl From<mystem_error> for Error {
fn from(e: mystem_error) -> Error {
return Error::MystemError(e);
}
}

View File

@ -1,10 +1,10 @@
use telegram_bot::*; //use crate::commands::Command;
use crate::mystem::MyStem; use crate::commands::{Execute, Here, Markov, MarkovAll, Omedeto, Sql, Top};
use crate::errors;
use crate::db; use crate::db;
use crate::commands; use crate::errors;
use crate::utils; use crate::utils;
use mystem::MyStem;
use telegram_bot::*;
pub async fn handler( pub async fn handler(
api: Api, api: Api,
@ -13,7 +13,6 @@ pub async fn handler(
mystem: &mut MyStem, mystem: &mut MyStem,
me: User, me: User,
) -> Result<(), errors::Error> { ) -> Result<(), errors::Error> {
match message.kind { match message.kind {
MessageKind::Text { ref data, .. } => { MessageKind::Text { ref data, .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
@ -26,12 +25,57 @@ pub async fn handler(
data data
); );
db::add_sentence(&message, mystem).await?; db::add_sentence(&message, mystem).await?;
match data.as_str() { let cleaned_message = data.replace(&format!("@{}", me.clone().username.unwrap()), "");
"/here" => commands::here(api, message).await?, match cleaned_message.as_str() {
"/top" => commands::top(api, message).await?, s if s.contains("/here") => {
"/stat" => commands::top(api, message).await?, Here {
"/markov_all" => commands::markov_all(api, message).await?, data: "".to_string(),
"/markov" => commands::markov(api, message).await?, }
.run(api, message)
.await?
}
s if s.to_string().starts_with("/sql") => {
Sql {
data: s.replace("/sql ", ""),
}
.run(api, message)
.await?
}
"/top" => {
Top {
data: "".to_string(),
}
.run(api, message)
.await?
}
"/stat" => {
Top {
data: "".to_string(),
}
.run(api, message)
.await?
}
"/markov_all" => {
MarkovAll {
data: "".to_string(),
}
.run(api, message)
.await?
}
"/markov" => {
Markov {
data: "".to_string(),
}
.run(api, message)
.await?
}
"/omedeto" => {
Omedeto {
data: "".to_string(),
}
.run_mystem(api, message, mystem)
.await?
}
_ => (), _ => (),
} }
} }

View File

@ -9,9 +9,8 @@ use env_logger::Env;
mod commands; mod commands;
mod db; mod db;
mod errors; mod errors;
mod mystem;
mod utils;
mod handlers; mod handlers;
mod utils;
use mystem::MyStem; use mystem::MyStem;
@ -39,13 +38,23 @@ async fn main() -> Result<(), errors::Error> {
let api = Api::new(token.clone()); let api = Api::new(token.clone());
let mut stream = api.stream(); let mut stream = api.stream();
let me = api.send(GetMe).await?; let me = api.send(GetMe).await?;
info!("GetMe result: Username: {}, First Name: {}, ID {}", me.username.as_ref().unwrap(), me.first_name, me.id); info!(
"GetMe result: Username: {}, First Name: {}, ID {}",
me.username.as_ref().unwrap(),
me.first_name,
me.id
);
while let Some(update) = stream.next().await { while let Some(update) = stream.next().await {
let update = update?; let update = update?;
if let UpdateKind::Message(message) = update.kind { if let UpdateKind::Message(message) = update.kind {
db::add_conf(message.clone()).await?; db::add_conf(message.clone()).await?;
db::add_user(message.clone()).await?; db::add_user(message.clone()).await?;
handlers::handler(api.clone(), message, token.clone(), &mut mystem, me.clone()).await?; match handlers::handler(api.clone(), message, token.clone(), &mut mystem, me.clone())
.await
{
Ok(_) => {}
Err(e) => warn!("An error occurred handling command. {:?}", e),
}
} }
} }
Ok(()) Ok(())

View File

@ -1,73 +0,0 @@
use crate::errors;
use serde_json::Value;
use std::io::{Error, Write, BufReader, prelude::*};
use subprocess::{Popen, PopenConfig, PopenError, Redirection};
pub struct MyStem {
pub process: Popen,
}
impl MyStem {
pub fn new() -> Result<Self, PopenError> {
Ok(Self {
process: MyStem::open_process()?,
})
}
fn open_process() -> Result<Popen, PopenError> {
Popen::create(
&["mystem", "-d", "--format", "json"],
PopenConfig {
stdout: Redirection::Pipe,
stdin: Redirection::Pipe,
..Default::default()
},
)
}
#[allow(dead_code)]
pub fn terminate(&mut self) -> Result<(), Error> {
self.process.terminate()
}
#[allow(unused_must_use)]
pub async fn stemming(&mut self, text: String) -> Result<Vec<String>, errors::Error> {
if let Some(exit_status) = self.process.poll() {
warn!(
"MyStem process exited with: {:?}. Restarting...",
exit_status
);
self.process = MyStem::open_process()?;
}
let mut words: Vec<String> = vec![];
let clean_text = format!("{}{}", text.trim(), "\n");
self.process
.stdin
.as_ref()
.unwrap()
.write(clean_text.as_bytes());
let mut contents = String::new();
let mut buf_reader = BufReader::new(self.process.stdout.as_ref().unwrap());
buf_reader.read_line(&mut contents);
match Some(contents) {
Some(contents) => {
let v: Vec<Value> = match serde_json::from_str(contents.as_str()) {
Ok(val) => val,
Err(_) => return Ok(vec![]),
};
for i in v {
words.push(i["analysis"][0]["lex"].to_string().replace("\"", ""));
}
words.retain(|x| x != "null");
debug!(
"Mystem PID: {}. Parsed words: {}.",
self.process.pid().unwrap(),
words.join(", ")
);
Ok(words)
}
None => return Ok(vec![]),
}
}
}