mirror of
https://github.com/house-of-vanity/desubot.git
synced 2025-07-08 21:04:07 +00:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
5d8d3441d2 | |||
34fa54e6f4 | |||
7a66034381 | |||
47906fe22d | |||
83a6045b18 | |||
a1b272bd40 | |||
3f00505659 | |||
3fd5b124f3 | |||
17442819c4 | |||
7adc629292 | |||
39640139fa | |||
0812c9e371 | |||
f111f54606 | |||
7e17851131 | |||
b674ae5b15 | |||
412c3f313c | |||
1838674cab |
2057
Cargo.lock
generated
Normal file
2057
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -33,4 +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"
|
mystem = "0.2.1"
|
||||||
|
async-trait = "0.1.42"
|
||||||
|
sqlparser = "0.7.0"
|
741
src/commands.rs
741
src/commands.rs
@ -1,254 +1,543 @@
|
|||||||
|
#![allow(unused_variables)]
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
|
use crate::errors::Error::{SQLITE3Error, 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::Gender::Feminine;
|
||||||
use mystem::MyStem;
|
use mystem::MyStem;
|
||||||
|
use mystem::Person::First;
|
||||||
use mystem::Tense::{Inpresent, Past};
|
use mystem::Tense::{Inpresent, Past};
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use regex::Regex;
|
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 {
|
||||||
let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap();
|
pub data: String,
|
||||||
for u in &members {
|
}
|
||||||
debug!("Found user {:?} in chat {}", u, message.chat.id());
|
pub struct Top {
|
||||||
}
|
pub data: String,
|
||||||
let mut msg = "<b>I summon you</b>, ".to_string();
|
}
|
||||||
for user in members {
|
pub struct MarkovAll {
|
||||||
let mention = match user.username {
|
pub data: String,
|
||||||
Some(username) => format!("@{}", username),
|
}
|
||||||
_ => format!(
|
pub struct Markov {
|
||||||
"<a href=\"tg://user?id={}\">{}</a>",
|
pub data: String,
|
||||||
encode_text(&user.id.to_string()),
|
}
|
||||||
encode_text(&user.first_name)
|
pub struct Omedeto {
|
||||||
),
|
pub data: String,
|
||||||
};
|
}
|
||||||
msg = format!("{} {}", msg, mention);
|
pub struct Sql {
|
||||||
}
|
pub data: String,
|
||||||
|
|
||||||
match api
|
|
||||||
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => debug!("/here command sent 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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> {
|
#[async_trait]
|
||||||
let top = db::get_top(&message).await?;
|
pub trait Execute {
|
||||||
let mut msg = "<b>Your top using words:</b>\n<pre>".to_string();
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error>;
|
||||||
let mut counter = 1;
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error>;
|
||||||
for word in top.iter() {
|
async fn exec_mystem(
|
||||||
msg = format!(
|
&self,
|
||||||
"{} <b>{}</b> {} - {}\n",
|
api: &Api,
|
||||||
msg, counter, word.word, word.count
|
message: &Message,
|
||||||
);
|
mystem: &mut MyStem,
|
||||||
counter += 1;
|
) -> Result<(), Error>;
|
||||||
}
|
|
||||||
msg = format!("{}{}", msg, "</pre>");
|
|
||||||
match api
|
|
||||||
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => debug!("/top command sent 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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn markov_all(api: Api, message: Message) -> Result<(), Error> {
|
#[async_trait]
|
||||||
let messages = db::get_messages_random_all().await?;
|
impl Execute for Sql {
|
||||||
let mut chain = Chain::new();
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
|
||||||
chain.feed(messages);
|
unimplemented!()
|
||||||
let mut sentences = chain.generate();
|
|
||||||
let mut msg = String::new();
|
|
||||||
for _ in 1..rand::thread_rng().gen_range(2, 10) {
|
|
||||||
msg = format!("{} {}", msg, sentences.pop().unwrap());
|
|
||||||
}
|
}
|
||||||
match api
|
|
||||||
.send(message.text_reply(msg.trim()).parse_mode(ParseMode::Html))
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => debug!("/markov_all command sent to {}", message.chat.id()),
|
|
||||||
Err(_) => warn!("/markov_all 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(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn markov(api: Api, message: Message) -> Result<(), Error> {
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
|
||||||
let messages = db::get_messages_random_group(&message).await?;
|
let mut sql = self.data.clone();
|
||||||
let mut chain = Chain::new();
|
let is_head = if sql.starts_with('-') {
|
||||||
chain.feed(messages);
|
sql = sql.replacen("-", "", 1);
|
||||||
let mut sentences = chain.generate();
|
|
||||||
let mut msg = String::new();
|
|
||||||
for _ in 1..rand::thread_rng().gen_range(2, 10) {
|
|
||||||
msg = format!("{} {}", msg, sentences.pop().unwrap());
|
|
||||||
}
|
|
||||||
match api
|
|
||||||
.send(message.text_reply(msg.trim()).parse_mode(ParseMode::Html))
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => debug!("/markov command sent to {}", message.chat.id()),
|
|
||||||
Err(_) => warn!("/markov 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(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn omedeto(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 => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
nouns.sort();
|
|
||||||
nouns.dedup();
|
|
||||||
nouns.shuffle(&mut rand::thread_rng());
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
verbs_p.sort();
|
|
||||||
verbs_p.dedup();
|
|
||||||
verbs_p.shuffle(&mut rand::thread_rng());
|
|
||||||
|
|
||||||
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)),
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
verbs_i.sort();
|
|
||||||
verbs_i.dedup();
|
|
||||||
verbs_i.shuffle(&mut rand::thread_rng());
|
|
||||||
|
|
||||||
if nouns.is_empty() {
|
|
||||||
nouns.push(message.from.first_name.to_string());
|
|
||||||
}
|
|
||||||
let start: Vec<String> = vec![
|
|
||||||
"С новыйм годом.".into(),
|
|
||||||
"С НГ тебя".into(),
|
|
||||||
"Поздравляю".into(),
|
|
||||||
"Поздравляю с НГ".into(),
|
|
||||||
];
|
|
||||||
//debug!("Nouns: {:#?}", nouns);
|
|
||||||
//debug!("Verbs: {:#?}", verbs);
|
|
||||||
|
|
||||||
let fem = {
|
|
||||||
let z = mystem
|
|
||||||
.stemming(message.from.first_name.to_string())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if z.is_empty() {
|
|
||||||
false
|
|
||||||
} else if z[0].lex.is_empty() {
|
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
if z[0].lex[0]
|
true
|
||||||
.grammem
|
};
|
||||||
.facts
|
let dialect = GenericDialect {};
|
||||||
.contains(&mystem::Fact::Gender(Feminine))
|
let ast: Vec<Statement> = match Parser::parse_sql(&dialect, &sql) {
|
||||||
{
|
Ok(ast) => ast,
|
||||||
|
Err(_) => {
|
||||||
|
warn!("Invalid SQL - {}", sql);
|
||||||
|
return Err(SQLInvalidCommand);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match ast.len() {
|
||||||
|
l if l > 1 => {
|
||||||
|
return Err(Error::SQLBannedCommand(
|
||||||
|
"🚫 One statement per message allowed 🚫".into(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
match ast[0] {
|
||||||
|
sqlparser::ast::Statement::Query { .. } => {}
|
||||||
|
_ => {
|
||||||
|
return Err(Error::SQLBannedCommand(
|
||||||
|
"🚫 SELECT requests allowed only 🚫".into(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let conn = db::open()?;
|
||||||
|
let mut stmt = conn.prepare_cached(&sql)?;
|
||||||
|
|
||||||
|
let mut rows = match stmt.query(rusqlite::NO_PARAMS) {
|
||||||
|
Err(e) => return Err(SQLITE3Error(e)),
|
||||||
|
Ok(rows) => 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);
|
||||||
|
}
|
||||||
|
if res.len() > 100 {
|
||||||
|
return Err(Error::SQLResultTooLong(
|
||||||
|
"SQL result too long. Lines limit is 100. Use LIMIT".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
async fn exec_mystem(
|
||||||
|
&self,
|
||||||
|
api: &Api,
|
||||||
|
message: &Message,
|
||||||
|
mystem: &mut MyStem,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Execute for Here {
|
||||||
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
|
||||||
|
let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap();
|
||||||
|
for u in &members {
|
||||||
|
debug!("Found user {:?} in chat {}", u, message.chat.id());
|
||||||
|
}
|
||||||
|
let mut msg = "<b>I summon you</b>, ".to_string();
|
||||||
|
for user in members {
|
||||||
|
let mention = match user.username {
|
||||||
|
Some(username) => format!("@{}", username),
|
||||||
|
_ => format!(
|
||||||
|
"<a href=\"tg://user?id={}\">{}</a>",
|
||||||
|
encode_text(&user.id.to_string()),
|
||||||
|
encode_text(&user.first_name)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
msg = format!("{} {}", msg, mention);
|
||||||
|
}
|
||||||
|
|
||||||
|
match api
|
||||||
|
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => debug!("/here command sent to {}", message.chat.id()),
|
||||||
|
Err(_) => warn!("/here command sent failed to {}", message.chat.id()),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
async fn exec_mystem(
|
||||||
|
&self,
|
||||||
|
api: &Api,
|
||||||
|
message: &Message,
|
||||||
|
mystem: &mut MyStem,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Execute for Top {
|
||||||
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
|
||||||
|
let top = db::get_top(&message).await?;
|
||||||
|
let mut msg = "<b>Your top using words:</b>\n<pre>".to_string();
|
||||||
|
let mut counter = 1;
|
||||||
|
for word in top.iter() {
|
||||||
|
msg = format!(
|
||||||
|
"{} <b>{}</b> {} - {}\n",
|
||||||
|
msg, counter, word.word, word.count
|
||||||
|
);
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
msg = format!("{}{}", msg, "</pre>");
|
||||||
|
match api
|
||||||
|
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => debug!("/top command sent to {}", message.chat.id()),
|
||||||
|
Err(_) => warn!("/top command sent failed to {}", message.chat.id()),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
async fn exec_mystem(
|
||||||
|
&self,
|
||||||
|
api: &Api,
|
||||||
|
message: &Message,
|
||||||
|
mystem: &mut MyStem,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Execute for MarkovAll {
|
||||||
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
|
||||||
|
let messages = db::get_messages_random_all().await?;
|
||||||
|
let mut chain = Chain::new();
|
||||||
|
chain.feed(messages);
|
||||||
|
let mut sentences = chain.generate();
|
||||||
|
let mut msg = String::new();
|
||||||
|
for _ in 1..rand::thread_rng().gen_range(2, 10) {
|
||||||
|
msg = format!("{} {}", msg, sentences.pop().unwrap());
|
||||||
|
}
|
||||||
|
match api
|
||||||
|
.send(message.text_reply(msg.trim()).parse_mode(ParseMode::Html))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => debug!("/markov_all command sent to {}", message.chat.id()),
|
||||||
|
Err(_) => warn!("/markov_all 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(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
async fn exec_mystem(
|
||||||
|
&self,
|
||||||
|
api: &Api,
|
||||||
|
message: &Message,
|
||||||
|
mystem: &mut MyStem,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Execute for Markov {
|
||||||
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
|
||||||
|
let messages = db::get_messages_random_group(&message).await?;
|
||||||
|
let mut chain = Chain::new();
|
||||||
|
chain.feed(messages);
|
||||||
|
let mut sentences = chain.generate();
|
||||||
|
let mut msg = String::new();
|
||||||
|
for _ in 1..rand::thread_rng().gen_range(2, 10) {
|
||||||
|
msg = format!("{} {}", msg, sentences.pop().unwrap());
|
||||||
|
}
|
||||||
|
match api
|
||||||
|
.send(message.text_reply(msg.trim()).parse_mode(ParseMode::Html))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => debug!("/markov command sent to {}", message.chat.id()),
|
||||||
|
Err(_) => warn!("/markov 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(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
async fn exec_mystem(
|
||||||
|
&self,
|
||||||
|
api: &Api,
|
||||||
|
message: &Message,
|
||||||
|
mystem: &mut MyStem,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Execute for Omedeto {
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[warn(unused_must_use)]
|
||||||
|
async fn exec_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
|
||||||
|
.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
|
||||||
|
.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
|
||||||
|
.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
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
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(())
|
||||||
let result = format!(
|
|
||||||
"{} {} известн{} как {}, {}, а так же конечно {}. В прошедшем году ты часто давал{} нам знать, что ты {}, {} и {}. Не редко ты говорил{} я {}, я {} или даже я {}. =*",
|
|
||||||
start.choose(&mut rand::thread_rng()).unwrap(),
|
|
||||||
message.from.first_name.to_string(),
|
|
||||||
{if fem {"ая"} else {"ый"}},
|
|
||||||
nouns.pop().unwrap_or("=(".to_string()),
|
|
||||||
nouns.pop().unwrap_or("=(".to_string()),
|
|
||||||
nouns.pop().unwrap_or("=(".to_string()),
|
|
||||||
{if fem {"а"} else {""}},
|
|
||||||
verbs_p.pop().unwrap_or("=(".to_string()),
|
|
||||||
verbs_p.pop().unwrap_or("=(".to_string()),
|
|
||||||
verbs_p.pop().unwrap_or("=(".to_string()),
|
|
||||||
{if fem {"а"} else {""}},
|
|
||||||
verbs_i.pop().unwrap_or("=(".to_string()),
|
|
||||||
verbs_i.pop().unwrap_or("=(".to_string()),
|
|
||||||
verbs_i.pop().unwrap_or("=(".to_string()),
|
|
||||||
|
|
||||||
);
|
|
||||||
debug!("{:?}", result);
|
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::errors;
|
use crate::errors;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use futures::StreamExt;
|
|
||||||
use rusqlite::{named_params, params, Connection, Error, Result};
|
use rusqlite::{named_params, params, Connection, Error, Result};
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use telegram_bot::*;
|
use telegram_bot::*;
|
||||||
@ -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,7 +100,7 @@ pub(crate) fn get_confs() -> Result<Vec<Conf>> {
|
|||||||
|
|
||||||
Ok(confs)
|
Ok(confs)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
pub(crate) async fn get_messages_random_all() -> 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")?;
|
||||||
@ -136,6 +135,7 @@ pub(crate) async fn get_messages_random_group(
|
|||||||
Ok(messages)
|
Ok(messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) async fn get_messages_user_group(
|
pub(crate) async fn get_messages_user_group(
|
||||||
message: &telegram_bot::Message,
|
message: &telegram_bot::Message,
|
||||||
) -> Result<Vec<String>, Error> {
|
) -> Result<Vec<String>, Error> {
|
||||||
@ -345,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -400,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,
|
||||||
|
@ -20,7 +20,11 @@ pub enum Error {
|
|||||||
JsonParseError(serde_error),
|
JsonParseError(serde_error),
|
||||||
PopenError(popen_error),
|
PopenError(popen_error),
|
||||||
MystemError(mystem_error),
|
MystemError(mystem_error),
|
||||||
|
SQLBannedCommand(String),
|
||||||
|
SQLInvalidCommand,
|
||||||
|
SQLResultTooLong(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
write!(f, "An error occurred.")
|
write!(f, "An error occurred.")
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::commands;
|
//use crate::commands::Command;
|
||||||
|
use crate::commands::{Execute, Here, Markov, MarkovAll, Omedeto, Sql, Top};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::errors;
|
use crate::errors;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
@ -24,13 +25,76 @@ 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?,
|
}
|
||||||
"/omedeto" => commands::omedeto(api, message, mystem).await?,
|
.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?
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,12 @@ async fn main() -> Result<(), errors::Error> {
|
|||||||
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(())
|
||||||
|
Reference in New Issue
Block a user