2020-12-06 23:55:09 +03:00
|
|
|
use std::{env, process};
|
2020-11-28 17:31:08 +03:00
|
|
|
|
|
|
|
use futures::StreamExt;
|
|
|
|
use telegram_bot::*;
|
2020-12-05 15:57:11 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
use env_logger::Env;
|
2020-11-28 17:31:08 +03:00
|
|
|
|
|
|
|
mod commands;
|
|
|
|
mod db;
|
2020-11-29 16:23:27 +03:00
|
|
|
mod errors;
|
2020-12-10 14:46:19 +03:00
|
|
|
mod mystem;
|
2020-11-29 16:23:27 +03:00
|
|
|
mod utils;
|
2020-12-10 14:46:19 +03:00
|
|
|
use mystem::MyStem;
|
2020-11-28 17:31:08 +03:00
|
|
|
|
2020-12-10 14:46:19 +03:00
|
|
|
async fn handler(
|
|
|
|
api: Api,
|
|
|
|
message: Message,
|
|
|
|
token: String,
|
|
|
|
mystem: &mut MyStem,
|
|
|
|
) -> Result<(), errors::Error> {
|
2020-11-28 17:31:08 +03:00
|
|
|
match message.kind {
|
|
|
|
MessageKind::Text { ref data, .. } => {
|
2020-11-29 16:23:27 +03:00
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-11-29 16:23:27 +03:00
|
|
|
"<{}({})>[{}({})]: {}",
|
2020-11-28 17:31:08 +03:00
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
data
|
|
|
|
);
|
2020-12-10 14:46:19 +03:00
|
|
|
db::add_sentence(&message, mystem).await?;
|
2020-11-28 17:31:08 +03:00
|
|
|
match data.as_str() {
|
|
|
|
"/here" => commands::here(api, message).await?,
|
2020-12-07 17:41:12 +03:00
|
|
|
"/top" => commands::top(api, message).await?,
|
2020-12-07 17:51:39 +03:00
|
|
|
"/stat" => commands::top(api, message).await?,
|
2020-12-08 12:31:21 +03:00
|
|
|
"/markov_all" => commands::markov_all(api, message).await?,
|
2020-11-28 17:31:08 +03:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 17:41:12 +03:00
|
|
|
MessageKind::Photo { ref caption, .. } => {
|
2020-12-05 15:57:11 +03:00
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-12-05 15:57:11 +03:00
|
|
|
"<{}({})>[{}({})]: *PHOTO* {}",
|
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
caption.clone().unwrap_or("NO_TITLE".to_string())
|
|
|
|
);
|
|
|
|
utils::get_files(api, message, token).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageKind::Document { ref caption, .. } => {
|
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-12-05 15:57:11 +03:00
|
|
|
"<{}({})>[{}({})]: *DOCUMENT* {}",
|
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
caption.clone().unwrap_or("NO_TITLE".to_string())
|
|
|
|
);
|
|
|
|
utils::get_files(api, message, token).await?;
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:55:25 +03:00
|
|
|
MessageKind::Sticker { .. } => {
|
2020-12-05 15:57:11 +03:00
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-12-05 15:57:11 +03:00
|
|
|
"<{}({})>[{}({})]: *STICKER*",
|
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
);
|
|
|
|
utils::get_files(api, message, token).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageKind::Voice { .. } => {
|
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-12-05 15:57:11 +03:00
|
|
|
"<{}({})>[{}({})]: *VOICE*",
|
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
);
|
|
|
|
utils::get_files(api, message, token).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageKind::Video { .. } => {
|
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-12-05 15:57:11 +03:00
|
|
|
"<{}({})>[{}({})]: *VIDEO*",
|
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
);
|
|
|
|
utils::get_files(api, message, token).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageKind::VideoNote { .. } => {
|
|
|
|
let title = utils::get_title(&message);
|
2020-12-06 13:37:54 +03:00
|
|
|
info!(
|
2020-12-05 15:57:11 +03:00
|
|
|
"<{}({})>[{}({})]: *VIDEO_NOTE*",
|
|
|
|
&message.chat.id(),
|
|
|
|
title,
|
|
|
|
&message.from.id,
|
|
|
|
&message.from.first_name,
|
|
|
|
);
|
|
|
|
utils::get_files(api, message, token).await?;
|
|
|
|
}
|
2020-11-28 17:31:08 +03:00
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
2020-11-29 16:23:27 +03:00
|
|
|
async fn main() -> Result<(), errors::Error> {
|
2020-12-07 15:55:25 +03:00
|
|
|
env_logger::from_env(Env::default().default_filter_or("info")).init();
|
2020-12-10 14:46:19 +03:00
|
|
|
let mut mystem = MyStem::new()?;
|
2020-12-07 15:55:25 +03:00
|
|
|
match db::update_scheme() {
|
2020-12-07 17:41:12 +03:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => panic!("Database error: {:?}", e),
|
2020-12-07 15:55:25 +03:00
|
|
|
}
|
2020-12-05 15:57:11 +03:00
|
|
|
let token = match env::var("TELEGRAM_BOT_TOKEN") {
|
|
|
|
Ok(token) => token,
|
|
|
|
Err(_) => {
|
|
|
|
error!("TELEGRAM_BOT_TOKEN not set");
|
|
|
|
process::exit(0x0001);
|
2020-12-06 23:55:09 +03:00
|
|
|
}
|
2020-12-05 15:57:11 +03:00
|
|
|
};
|
|
|
|
let api = Api::new(token.clone());
|
2020-11-28 17:31:08 +03:00
|
|
|
// Fetch new updates via long poll method
|
|
|
|
let mut stream = api.stream();
|
|
|
|
while let Some(update) = stream.next().await {
|
|
|
|
// If the received update contains a new message...
|
|
|
|
let update = update?;
|
|
|
|
if let UpdateKind::Message(message) = update.kind {
|
2020-12-05 15:57:11 +03:00
|
|
|
db::add_user(message.clone()).await?;
|
|
|
|
db::add_conf(message.clone()).await?;
|
2020-12-10 14:46:19 +03:00
|
|
|
handler(api.clone(), message, token.clone(), &mut mystem).await?;
|
2020-11-28 17:31:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|