2020-11-28 17:31:08 +03:00
|
|
|
use crate::db;
|
2020-12-08 12:31:21 +03:00
|
|
|
use crate::errors::Error;
|
2020-11-29 16:23:27 +03:00
|
|
|
use html_escape::encode_text;
|
2020-12-08 12:31:21 +03:00
|
|
|
use markov::Chain;
|
2020-11-28 17:31:08 +03:00
|
|
|
use telegram_bot::prelude::*;
|
2020-12-07 17:41:12 +03:00
|
|
|
use telegram_bot::{Api, Message, ParseMode};
|
2020-12-08 12:31:21 +03:00
|
|
|
use rand::Rng;
|
2020-11-28 17:31:08 +03:00
|
|
|
|
|
|
|
pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> {
|
2020-11-29 16:23:27 +03:00
|
|
|
let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap();
|
2020-11-29 00:26:00 +03:00
|
|
|
for u in &members {
|
2020-12-07 17:41:12 +03:00
|
|
|
debug!("Found user {:?} in chat {}", u, message.chat.id());
|
2020-11-29 00:26:00 +03:00
|
|
|
}
|
|
|
|
let mut msg = "<b>I summon you</b>, ".to_string();
|
2020-11-28 17:31:08 +03:00
|
|
|
for user in members {
|
|
|
|
let mention = match user.username {
|
|
|
|
Some(username) => format!("@{}", username),
|
2020-11-29 16:23:27 +03:00
|
|
|
_ => format!(
|
|
|
|
"<a href=\"tg://user?id={}\">{}</a>",
|
|
|
|
encode_text(&user.id.to_string()),
|
|
|
|
encode_text(&user.first_name)
|
|
|
|
),
|
2020-11-28 17:31:08 +03:00
|
|
|
};
|
|
|
|
msg = format!("{} {}", msg, mention);
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:55:25 +03:00
|
|
|
match api
|
|
|
|
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
|
|
|
|
.await
|
|
|
|
{
|
2020-12-07 17:41:12 +03:00
|
|
|
Ok(_) => debug!("/here command sent to {}", message.chat.id()),
|
|
|
|
Err(_) => warn!("/here command sent failed to {}", message.chat.id()),
|
2020-12-06 23:55:09 +03:00
|
|
|
}
|
2020-11-28 17:31:08 +03:00
|
|
|
//api.send(message.chat.text("Text to message chat")).await?;
|
|
|
|
//api.send(message.from.text("Private text")).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-07 17:41:12 +03:00
|
|
|
|
|
|
|
pub(crate) async fn top(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() {
|
2020-12-08 12:31:21 +03:00
|
|
|
msg = format!(
|
|
|
|
"{} <b>{}</b> {} - {}\n",
|
|
|
|
msg, counter, word.word, word.count
|
|
|
|
);
|
2020-12-07 17:41:12 +03:00
|
|
|
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()),
|
|
|
|
}
|
|
|
|
//api.send(message.chat.text("Text to message chat")).await?;
|
|
|
|
//api.send(message.from.text("Private text")).await?;
|
2020-12-08 12:31:21 +03:00
|
|
|
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(())
|
|
|
|
}
|