9171c791eb 2021-11-13 1: mod command;
9171c791eb 2021-11-13 2: mod core;
6ac5737a72 2020-11-18 3:
0191d490fe 2020-11-18 4: use config;
b2297258c7 2021-11-06 5: use futures::StreamExt;
0191d490fe 2020-11-18 6: use tokio;
a7f91033c0 2021-11-13 7: use telegram_bot;
4acdad1942 2020-11-26 8:
4acdad1942 2020-11-26 9: #[macro_use]
4acdad1942 2020-11-26 10: extern crate lazy_static;
4acdad1942 2020-11-26 11:
9171c791eb 2021-11-13 12: use anyhow::Result;
ec616a2a43 2020-11-19 13:
ec616a2a43 2020-11-19 14: #[tokio::main]
61df933942 2020-11-18 15: async fn main() -> Result<()> {
61df933942 2020-11-18 16: let mut settings = config::Config::default();
61df933942 2020-11-18 17: settings.merge(config::File::with_name("rsstg"))?;
61df933942 2020-11-18 18:
9171c791eb 2021-11-13 19: let core = core::Core::new(settings).await?;
a68576cc1b 2020-12-15 20:
a68576cc1b 2020-12-15 21: let mut stream = core.stream();
a7f91033c0 2021-11-13 22: stream.allowed_updates(&[telegram_bot::AllowedUpdate::Message]);
a7f91033c0 2021-11-13 23: let mut reply_to: Option<telegram_bot::UserId>;
a68576cc1b 2020-12-15 24:
a68576cc1b 2020-12-15 25: loop {
b2b8005309 2020-12-18 26: reply_to = None;
a68576cc1b 2020-12-15 27: match stream.next().await {
a68576cc1b 2020-12-15 28: Some(update) => {
b2b8005309 2020-12-18 29: if let Err(err) = handle(update?, &core, &mut reply_to).await {
a7f91033c0 2021-11-13 30: core.send(&format!("🛑 {:?}", err), reply_to, None)?;
a68576cc1b 2020-12-15 31: };
a68576cc1b 2020-12-15 32: },
a68576cc1b 2020-12-15 33: None => {
a7f91033c0 2021-11-13 34: core.send(&format!("🛑 None error."), None, None)?;
a68576cc1b 2020-12-15 35: }
a68576cc1b 2020-12-15 36: };
a68576cc1b 2020-12-15 37: }
a68576cc1b 2020-12-15 38:
a68576cc1b 2020-12-15 39: //Ok(())
a68576cc1b 2020-12-15 40: }
a68576cc1b 2020-12-15 41:
a7f91033c0 2021-11-13 42: async fn handle(update: telegram_bot::Update, core: &core::Core, mut _reply_to: &Option<telegram_bot::UserId>) -> Result<()> {
a68576cc1b 2020-12-15 43: match update.kind {
a7f91033c0 2021-11-13 44: telegram_bot::UpdateKind::Message(message) => {
a68576cc1b 2020-12-15 45: match message.kind {
a7f91033c0 2021-11-13 46: telegram_bot::MessageKind::Text { ref data, .. } => {
9171c791eb 2021-11-13 47: let sender = message.from.id;
9171c791eb 2021-11-13 48: let words: Vec<&str> = data.split_whitespace().collect();
9171c791eb 2021-11-13 49: match words[0] {
9171c791eb 2021-11-13 50: "/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(core, sender, words).await?,
9171c791eb 2021-11-13 51: "/start" => command::start(core, sender).await?,
9171c791eb 2021-11-13 52: "/list" => command::list(core, sender).await?,
9171c791eb 2021-11-13 53: "/add" | "/update" => command::update(core, sender, words).await?,
4acdad1942 2020-11-26 54: _ => {
4acdad1942 2020-11-26 55: },
4acdad1942 2020-11-26 56: };
4acdad1942 2020-11-26 57: },
4acdad1942 2020-11-26 58: _ => {
4acdad1942 2020-11-26 59: },
4acdad1942 2020-11-26 60: };
4acdad1942 2020-11-26 61: },
4acdad1942 2020-11-26 62: _ => {},
4acdad1942 2020-11-26 63: };
61df933942 2020-11-18 64:
61df933942 2020-11-18 65: Ok(())
61df933942 2020-11-18 66: }