mirror of
https://github.com/house-of-vanity/desubot.git
synced 2025-08-21 15:27:14 +00:00
Merge pull request #2 from house-of-vanity/matching_commands
Rewrite command parsing.
This commit is contained in:
@@ -33,4 +33,5 @@ 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"
|
137
src/commands.rs
137
src/commands.rs
@@ -1,19 +1,57 @@
|
|||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
|
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::Case::Nominative;
|
||||||
use mystem::Gender::Feminine;
|
use mystem::Gender::Feminine;
|
||||||
use mystem::Tense::{Inpresent, Past};
|
use mystem::Tense::{Inpresent, Past};
|
||||||
use mystem::VerbPerson::First;
|
use mystem::Person::First;
|
||||||
use mystem::{MyStem, VerbPerson};
|
use mystem::MyStem;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
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 enum Command {
|
||||||
|
// Here(Here),
|
||||||
|
// Top { data: String },
|
||||||
|
// MarkovAll { data: String },
|
||||||
|
// Markov { data: String },
|
||||||
|
// Omedeto { 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 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());
|
||||||
@@ -41,9 +79,22 @@ pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> {
|
|||||||
//api.send(message.chat.text("Text to message chat")).await?;
|
//api.send(message.chat.text("Text to message chat")).await?;
|
||||||
//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!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> {
|
#[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;
|
||||||
@@ -65,9 +116,22 @@ pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> {
|
|||||||
//api.send(message.chat.text("Text to message chat")).await?;
|
//api.send(message.chat.text("Text to message chat")).await?;
|
||||||
//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!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn markov_all(api: Api, message: Message) -> Result<(), Error> {
|
#[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 messages = db::get_messages_random_all().await?;
|
||||||
let mut chain = Chain::new();
|
let mut chain = Chain::new();
|
||||||
chain.feed(messages);
|
chain.feed(messages);
|
||||||
@@ -86,9 +150,22 @@ pub(crate) async fn markov_all(api: Api, message: Message) -> Result<(), Error>
|
|||||||
//api.send(message.chat.text("Text to message chat")).await?;
|
//api.send(message.chat.text("Text to message chat")).await?;
|
||||||
//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!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn markov(api: Api, message: Message) -> Result<(), Error> {
|
#[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 messages = db::get_messages_random_group(&message).await?;
|
||||||
let mut chain = Chain::new();
|
let mut chain = Chain::new();
|
||||||
chain.feed(messages);
|
chain.feed(messages);
|
||||||
@@ -107,9 +184,33 @@ pub(crate) async fn markov(api: Api, message: Message) -> Result<(), Error> {
|
|||||||
//api.send(message.chat.text("Text to message chat")).await?;
|
//api.send(message.chat.text("Text to message chat")).await?;
|
||||||
//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!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) -> Result<(), Error> {
|
#[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 all_msg = db::get_messages_user_all(&message).await?;
|
||||||
let re = Regex::new(r"^[яЯ] [а-яА-Я]+(-[а-яА-Я]+(_[а-яА-Я]+)*)*").unwrap();
|
let re = Regex::new(r"^[яЯ] [а-яА-Я]+(-[а-яА-Я]+(_[а-яА-Я]+)*)*").unwrap();
|
||||||
let mut nouns: Vec<String> = all_msg
|
let mut nouns: Vec<String> = all_msg
|
||||||
@@ -138,7 +239,7 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
|
|||||||
nouns.sort();
|
nouns.sort();
|
||||||
nouns.dedup();
|
nouns.dedup();
|
||||||
nouns.shuffle(&mut rand::thread_rng());
|
nouns.shuffle(&mut rand::thread_rng());
|
||||||
debug!("Found {} nouns. {:#?}", nouns.len(), nouns);
|
//debug!("Found {} nouns. {:#?}", nouns.len(), nouns);
|
||||||
|
|
||||||
let mut verbs_p: Vec<String> = all_msg
|
let mut verbs_p: Vec<String> = all_msg
|
||||||
.clone()
|
.clone()
|
||||||
@@ -166,7 +267,7 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
|
|||||||
verbs_p.sort();
|
verbs_p.sort();
|
||||||
verbs_p.dedup();
|
verbs_p.dedup();
|
||||||
verbs_p.shuffle(&mut rand::thread_rng());
|
verbs_p.shuffle(&mut rand::thread_rng());
|
||||||
debug!("Found {} past verbs. {:#?}", verbs_p.len(), verbs_p);
|
//debug!("Found {} past verbs. {:#?}", verbs_p.len(), verbs_p);
|
||||||
|
|
||||||
let mut verbs_i: Vec<String> = all_msg
|
let mut verbs_i: Vec<String> = all_msg
|
||||||
.clone()
|
.clone()
|
||||||
@@ -200,7 +301,7 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
|
|||||||
verbs_i.sort();
|
verbs_i.sort();
|
||||||
verbs_i.dedup();
|
verbs_i.dedup();
|
||||||
verbs_i.shuffle(&mut rand::thread_rng());
|
verbs_i.shuffle(&mut rand::thread_rng());
|
||||||
debug!("Found {} inpresent verbs. {:#?}", verbs_i.len(), verbs_i);
|
//debug!("Found {} inpresent verbs. {:#?}", verbs_i.len(), verbs_i);
|
||||||
|
|
||||||
if nouns.is_empty() {
|
if nouns.is_empty() {
|
||||||
nouns.push(message.from.first_name.to_string());
|
nouns.push(message.from.first_name.to_string());
|
||||||
@@ -260,34 +361,33 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<()>();
|
.for_each(drop);
|
||||||
debug!("fm - {}, mu - {}", fm, mu);
|
//debug!("fm - {}, mu - {}", fm, mu);
|
||||||
if fm >= mu {
|
if fm >= mu {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
debug!("Is Feminine - {}", fem);
|
//debug!("Is Feminine - {}", fem);
|
||||||
let result = format!(
|
let result = format!(
|
||||||
"{} {} известн{} как {}, {}, а так же конечно {}. В прошедшем году ты часто давал{} нам знать, что ты {}, {} и {}. Нередко ты говорил{} я {}, я {} или даже я {}. =*",
|
"{} {} известн{} как {}, {}, а так же конечно {}. В прошедшем году ты часто давал{} нам знать, что ты {}, {} и {}. Нередко ты говорил{} я {}, я {} или даже я {}. =*",
|
||||||
start.choose(&mut rand::thread_rng()).unwrap(),
|
start.choose(&mut rand::thread_rng()).unwrap(),
|
||||||
message.from.first_name.to_string(),
|
message.from.first_name.to_string(),
|
||||||
{if fem {"ая"} else {"ый"}},
|
{ 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()),
|
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 {""}},
|
{ 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()),
|
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 {""}},
|
{ 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()),
|
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()),
|
||||||
|
|
||||||
);
|
);
|
||||||
debug!("{:?}", result);
|
//debug!("{:?}", result);
|
||||||
match api
|
match api
|
||||||
.send(
|
.send(
|
||||||
message
|
message
|
||||||
@@ -300,4 +400,5 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
|
|||||||
Err(_) => warn!("/omedeto command sent failed to {}", message.chat.id()),
|
Err(_) => warn!("/omedeto command sent failed to {}", message.chat.id()),
|
||||||
}
|
}
|
||||||
Ok(())
|
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::*;
|
||||||
@@ -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> {
|
||||||
@@ -400,6 +400,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,
|
||||||
|
101
src/handlers.rs
101
src/handlers.rs
@@ -1,11 +1,56 @@
|
|||||||
use crate::commands;
|
//use crate::commands::Command;
|
||||||
|
use crate::commands::{Execute, Here, Markov, MarkovAll, Omedeto, Top};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::errors;
|
use crate::errors;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use mystem::MyStem;
|
use mystem::MyStem;
|
||||||
use telegram_bot::*;
|
use telegram_bot::*;
|
||||||
|
|
||||||
//async fn detector()
|
// struct Command {
|
||||||
|
// command: Commands,
|
||||||
|
// explicit: bool,
|
||||||
|
// rest: String,
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async fn detector(msg: String, me: &User) -> Result<Command, ()> {
|
||||||
|
// let cleaned_message = msg.replace(&format!("@{}", me.clone().username.unwrap()), "");
|
||||||
|
// match cleaned_message.as_str() {
|
||||||
|
// "/here" => Ok(Command::Here {
|
||||||
|
// data: "".to_string(),
|
||||||
|
// }),
|
||||||
|
// s if s.contains("/here") => Ok(Command::Here {
|
||||||
|
// data: s.to_string(),
|
||||||
|
// }),
|
||||||
|
// "/top" => Ok(Command::Top {
|
||||||
|
// data: "".to_string(),
|
||||||
|
// }),
|
||||||
|
// "/stat" => Ok(Command::Top {
|
||||||
|
// data: "".to_string(),
|
||||||
|
// }),
|
||||||
|
// s if s.contains(|z| z == "/top" || z == "/stat") => Ok(Command::Top {
|
||||||
|
// data: s.to_string(),
|
||||||
|
// }),
|
||||||
|
// "/markov_all" => Ok(Command::MarkovAll {
|
||||||
|
// data: "".to_string(),
|
||||||
|
// }),
|
||||||
|
// s if s.contains("/markov_all") => Ok(Command::MarkovAll {
|
||||||
|
// data: s.to_string(),
|
||||||
|
// }),
|
||||||
|
// "/markov" => Ok(Command::Markov {
|
||||||
|
// data: "".to_string(),
|
||||||
|
// }),
|
||||||
|
// s if s.contains("/markov") => Ok(Command::Markov {
|
||||||
|
// data: s.to_string(),
|
||||||
|
// }),
|
||||||
|
// "/omedeto" => Ok(Command::Omedeto {
|
||||||
|
// data: "".to_string(),
|
||||||
|
// }),
|
||||||
|
// s if s.contains("/Omedeto") => Ok(Command::Omedeto {
|
||||||
|
// data: s.to_string(),
|
||||||
|
// }),
|
||||||
|
// _ => Err(()),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
pub async fn handler(
|
pub async fn handler(
|
||||||
api: Api,
|
api: Api,
|
||||||
@@ -27,15 +72,51 @@ pub async fn handler(
|
|||||||
);
|
);
|
||||||
db::add_sentence(&message, mystem).await?;
|
db::add_sentence(&message, mystem).await?;
|
||||||
let cleaned_message = data
|
let cleaned_message = data
|
||||||
.to_string()
|
.replace(&format!("@{}", me.clone().username.unwrap()), "");
|
||||||
.replace(&format!("@{}", me.username.unwrap()), "");
|
debug!("Cleaned - {}", cleaned_message);
|
||||||
match cleaned_message.as_str() {
|
match cleaned_message.as_str() {
|
||||||
"/here" => commands::here(api, message).await?,
|
s if s.contains("/here") => {
|
||||||
"/top" => commands::top(api, message).await?,
|
Here {
|
||||||
"/stat" => commands::top(api, message).await?,
|
data: "".to_string(),
|
||||||
"/markov_all" => commands::markov_all(api, message).await?,
|
}
|
||||||
"/markov" => commands::markov(api, message).await?,
|
.run(api, message)
|
||||||
"/omedeto" => commands::omedeto(api, message, mystem).await?,
|
.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?
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user