Files
desubot/src/main.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2020-11-28 17:31:08 +03:00
use std::env;
use futures::StreamExt;
use telegram_bot::types::chat::MessageChat;
use telegram_bot::*;
mod commands;
mod db;
2020-11-29 16:23:27 +03:00
mod errors;
mod utils;
2020-11-28 17:31:08 +03:00
async fn handler(api: Api, message: Message) -> Result<(), Error> {
match message.kind {
MessageKind::Text { ref data, .. } => {
2020-11-29 16:23:27 +03:00
let title = utils::get_title(&message);
2020-11-28 17:31:08 +03:00
println!(
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
);
match data.as_str() {
"/here" => commands::here(api, message).await?,
_ => (),
}
}
_ => (),
};
Ok(())
}
#[tokio::main]
2020-11-29 16:23:27 +03:00
async fn main() -> Result<(), errors::Error> {
2020-11-28 17:31:08 +03:00
let token = env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set");
let api = Api::new(token);
// 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-11-29 16:23:27 +03:00
db::add_user(api.clone(), message.clone()).await?;
db::add_conf(api.clone(), message.clone()).await?;
2020-11-28 17:31:08 +03:00
handler(api.clone(), message).await?;
}
}
Ok(())
}