mirror of
https://github.com/house-of-vanity/desubot.git
synced 2025-07-08 21:04:07 +00:00
Add markov chain sentence generator.
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
use crate::db;
|
||||
use crate::errors::Error;
|
||||
use html_escape::encode_text;
|
||||
use markov::Chain;
|
||||
use telegram_bot::prelude::*;
|
||||
use telegram_bot::{Api, Message, ParseMode};
|
||||
use crate::errors::Error;
|
||||
use rand::Rng;
|
||||
|
||||
pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> {
|
||||
let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap();
|
||||
@ -39,7 +41,10 @@ pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> {
|
||||
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);
|
||||
msg = format!(
|
||||
"{} <b>{}</b> {} - {}\n",
|
||||
msg, counter, word.word, word.count
|
||||
);
|
||||
counter += 1;
|
||||
}
|
||||
msg = format!("{}{}", msg, "</pre>");
|
||||
@ -52,4 +57,26 @@ pub(crate) async fn top(api: Api, message: Message) -> Result<(), Error> {
|
||||
}
|
||||
//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> {
|
||||
let messages = db::get_random_messages().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!("/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(())
|
||||
}
|
||||
|
13
src/db.rs
13
src/db.rs
@ -1,6 +1,6 @@
|
||||
use crate::errors;
|
||||
use crate::utils;
|
||||
use rusqlite::{params, named_params, Connection, Error, Result};
|
||||
use rusqlite::{named_params, params, Connection, Error, Result};
|
||||
use std::time::SystemTime;
|
||||
use telegram_bot::*;
|
||||
|
||||
@ -95,6 +95,17 @@ pub(crate) fn get_confs() -> Result<Vec<Conf>> {
|
||||
Ok(confs)
|
||||
}
|
||||
*/
|
||||
pub(crate) async fn get_random_messages() -> Result<Vec<String>, Error> {
|
||||
let conn = open()?;
|
||||
let mut stmt = conn.prepare("SELECT text FROM messages 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()?;
|
||||
|
@ -28,6 +28,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
|
||||
"/here" => commands::here(api, message).await?,
|
||||
"/top" => commands::top(api, message).await?,
|
||||
"/stat" => commands::top(api, message).await?,
|
||||
"/markov_all" => commands::markov_all(api, message).await?,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user