Lines of
src/main.rs
from check-in 28da2e2a00
that are changed by the sequence of edits moving toward
check-in cb86e770f9:
1: mod command;
2: mod core;
3:
4: use futures::StreamExt;
28da2e2a00 2022-03-12 5: use anyhow::Result;
6:
28da2e2a00 2022-03-12 7: #[tokio::main]
28da2e2a00 2022-03-12 8: async fn main() -> Result<()> {
9: let settings = config::Config::builder()
10: .add_source(config::File::with_name("rsstg"))
11: .build()?;
12:
28da2e2a00 2022-03-12 13: let core = core::Core::new(settings).await?;
14:
15: let mut stream = core.stream();
16: stream.allowed_updates(&[telegram_bot::AllowedUpdate::Message]);
28da2e2a00 2022-03-12 17: let mut reply_to: Option<telegram_bot::UserId>;
28da2e2a00 2022-03-12 18:
28da2e2a00 2022-03-12 19: loop {
28da2e2a00 2022-03-12 20: reply_to = None;
28da2e2a00 2022-03-12 21: match stream.next().await {
28da2e2a00 2022-03-12 22: Some(update) => {
28da2e2a00 2022-03-12 23: if let Err(err) = handle(update?, &core, &reply_to).await {
28da2e2a00 2022-03-12 24: core.send(&format!("🛑 {:?}", err), reply_to, None).await?;
28da2e2a00 2022-03-12 25: };
28da2e2a00 2022-03-12 26: },
28da2e2a00 2022-03-12 27: None => {
28da2e2a00 2022-03-12 28: core.send("🛑 None error.", None, None).await?;
28da2e2a00 2022-03-12 29: }
28da2e2a00 2022-03-12 30: };
28da2e2a00 2022-03-12 31: }
32: }
33:
34: async fn handle(update: telegram_bot::Update, core: &core::Core, mut _reply_to: &Option<telegram_bot::UserId>) -> Result<()> {
35: if let telegram_bot::UpdateKind::Message(message) = update.kind {
36: if let telegram_bot::MessageKind::Text { ref data, .. } = message.kind {
37: let sender = message.from.id;
38: let words: Vec<&str> = data.split_whitespace().collect();
39: match match words[0] {
40: "/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(core, sender, words).await,
41: "/start" => command::start(core, sender).await,
42: "/list" => command::list(core, sender).await,
43: "/add" | "/update" => command::update(core, sender, words).await,
44: _ => Ok(()),
45: } {
46: Err(err) => core.send(format!("🛑 {:?}", err), Some(sender), None).await?,
47: Ok(()) => {},
48: };
49: };
50: };
51:
52: Ok(())
53: }