mirror of
https://github.com/house-of-vanity/desubot.git
synced 2025-07-08 21:04:07 +00:00
markov single group
This commit is contained in:
@ -80,3 +80,24 @@ pub(crate) async fn markov_all(api: Api, message: Message) -> Result<(), Error>
|
||||
//api.send(message.from.text("Private text")).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn markov(api: Api, message: Message) -> Result<(), Error> {
|
||||
let messages = db::get_random_messages_group().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(())
|
||||
}
|
17
src/db.rs
17
src/db.rs
@ -115,6 +115,23 @@ pub(crate) async fn get_random_messages() -> Result<Vec<String>, Error> {
|
||||
Ok(messages)
|
||||
}
|
||||
|
||||
pub(crate) async fn get_random_messages_group() -> Result<Vec<String>, Error> {
|
||||
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
|
||||
ORDER BY RANDOM() LIMIT 50
|
||||
"
|
||||
)?;
|
||||
let mut rows = stmt.query_named(named_params![])?;
|
||||
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>> {
|
||||
let conn = open()?;
|
||||
let mut stmt = conn.prepare_cached(
|
||||
|
@ -31,6 +31,7 @@ pub async fn handler(
|
||||
"/top" => commands::top(api, message).await?,
|
||||
"/stat" => commands::top(api, message).await?,
|
||||
"/markov_all" => commands::markov_all(api, message).await?,
|
||||
"/markov" => commands::markov(api, message).await?,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user