Artifact
20a887e7dc0789870dc3e61a608b3bbdd17a110d5dd265c798780fd9cf8f3b8d:
0000: 6d 6f 64 20 63 6f 6d 6d 61 6e 64 3b 0a 6d 6f 64 mod command;.mod
0010: 20 63 6f 72 65 3b 0a 0a 75 73 65 20 66 75 74 75 core;..use futu
0020: 72 65 73 3a 3a 53 74 72 65 61 6d 45 78 74 3b 0a res::StreamExt;.
0030: 75 73 65 20 61 6e 79 68 6f 77 3a 3a 52 65 73 75 use anyhow::Resu
0040: 6c 74 3b 0a 0a 23 5b 74 6f 6b 69 6f 3a 3a 6d 61 lt;..#[tokio::ma
0050: 69 6e 5d 0a 61 73 79 6e 63 20 66 6e 20 6d 61 69 in].async fn mai
0060: 6e 28 29 20 2d 3e 20 52 65 73 75 6c 74 3c 28 29 n() -> Result<()
0070: 3e 20 7b 0a 09 6c 65 74 20 73 65 74 74 69 6e 67 > {..let setting
0080: 73 20 3d 20 63 6f 6e 66 69 67 3a 3a 43 6f 6e 66 s = config::Conf
0090: 69 67 3a 3a 62 75 69 6c 64 65 72 28 29 0a 09 09 ig::builder()...
00a0: 2e 61 64 64 5f 73 6f 75 72 63 65 28 63 6f 6e 66 .add_source(conf
00b0: 69 67 3a 3a 46 69 6c 65 3a 3a 77 69 74 68 5f 6e ig::File::with_n
00c0: 61 6d 65 28 22 72 73 73 74 67 22 29 29 0a 09 09 ame("rsstg"))...
00d0: 2e 62 75 69 6c 64 28 29 3f 3b 0a 0a 09 6c 65 74 .build()?;...let
00e0: 20 63 6f 72 65 20 3d 20 63 6f 72 65 3a 3a 43 6f core = core::Co
00f0: 72 65 3a 3a 6e 65 77 28 73 65 74 74 69 6e 67 73 re::new(settings
0100: 29 2e 61 77 61 69 74 3f 3b 0a 0a 09 6c 65 74 20 ).await?;...let
0110: 6d 75 74 20 73 74 72 65 61 6d 20 3d 20 63 6f 72 mut stream = cor
0120: 65 2e 73 74 72 65 61 6d 28 29 3b 0a 09 73 74 72 e.stream();..str
0130: 65 61 6d 2e 61 6c 6c 6f 77 65 64 5f 75 70 64 61 eam.allowed_upda
0140: 74 65 73 28 26 5b 74 65 6c 65 67 72 61 6d 5f 62 tes(&[telegram_b
0150: 6f 74 3a 3a 41 6c 6c 6f 77 65 64 55 70 64 61 74 ot::AllowedUpdat
0160: 65 3a 3a 4d 65 73 73 61 67 65 5d 29 3b 0a 09 6c e::Message]);..l
0170: 65 74 20 6d 75 74 20 72 65 70 6c 79 5f 74 6f 3a et mut reply_to:
0180: 20 4f 70 74 69 6f 6e 3c 74 65 6c 65 67 72 61 6d Option<telegram
0190: 5f 62 6f 74 3a 3a 55 73 65 72 49 64 3e 3b 0a 0a _bot::UserId>;..
01a0: 09 6c 6f 6f 70 20 7b 0a 09 09 72 65 70 6c 79 5f .loop {...reply_
01b0: 74 6f 20 3d 20 4e 6f 6e 65 3b 0a 09 09 6d 61 74 to = None;...mat
01c0: 63 68 20 73 74 72 65 61 6d 2e 6e 65 78 74 28 29 ch stream.next()
01d0: 2e 61 77 61 69 74 20 7b 0a 09 09 09 53 6f 6d 65 .await {....Some
01e0: 28 75 70 64 61 74 65 29 20 3d 3e 20 7b 0a 09 09 (update) => {...
01f0: 09 09 69 66 20 6c 65 74 20 45 72 72 28 65 72 72 ..if let Err(err
0200: 29 20 3d 20 68 61 6e 64 6c 65 28 75 70 64 61 74 ) = handle(updat
0210: 65 3f 2c 20 26 63 6f 72 65 2c 20 26 72 65 70 6c e?, &core, &repl
0220: 79 5f 74 6f 29 2e 61 77 61 69 74 20 7b 0a 09 09 y_to).await {...
0230: 09 09 09 63 6f 72 65 2e 73 65 6e 64 28 26 66 6f ...core.send(&fo
0240: 72 6d 61 74 21 28 22 f0 9f 9b 91 20 7b 3a 3f 7d rmat!("🛑 {:?}
0250: 22 2c 20 65 72 72 29 2c 20 72 65 70 6c 79 5f 74 ", err), reply_t
0260: 6f 2c 20 4e 6f 6e 65 29 2e 61 77 61 69 74 3f 3b o, None).await?;
0270: 0a 09 09 09 09 7d 3b 0a 09 09 09 7d 2c 0a 09 09 .....};....},...
0280: 09 4e 6f 6e 65 20 3d 3e 20 7b 0a 09 09 09 09 63 .None => {.....c
0290: 6f 72 65 2e 73 65 6e 64 28 22 f0 9f 9b 91 20 4e ore.send("🛑 N
02a0: 6f 6e 65 20 65 72 72 6f 72 2e 22 2c 20 4e 6f 6e one error.", Non
02b0: 65 2c 20 4e 6f 6e 65 29 2e 61 77 61 69 74 3f 3b e, None).await?;
02c0: 0a 09 09 09 7d 0a 09 09 7d 3b 0a 09 7d 0a 7d 0a ....}...};..}.}.
02d0: 0a 61 73 79 6e 63 20 66 6e 20 68 61 6e 64 6c 65 .async fn handle
02e0: 28 75 70 64 61 74 65 3a 20 74 65 6c 65 67 72 61 (update: telegra
02f0: 6d 5f 62 6f 74 3a 3a 55 70 64 61 74 65 2c 20 63 m_bot::Update, c
0300: 6f 72 65 3a 20 26 63 6f 72 65 3a 3a 43 6f 72 65 ore: &core::Core
0310: 2c 20 6d 75 74 20 5f 72 65 70 6c 79 5f 74 6f 3a , mut _reply_to:
0320: 20 26 4f 70 74 69 6f 6e 3c 74 65 6c 65 67 72 61 &Option<telegra
0330: 6d 5f 62 6f 74 3a 3a 55 73 65 72 49 64 3e 29 20 m_bot::UserId>)
0340: 2d 3e 20 52 65 73 75 6c 74 3c 28 29 3e 20 7b 0a -> Result<()> {.
0350: 09 69 66 20 6c 65 74 20 74 65 6c 65 67 72 61 6d .if let telegram
0360: 5f 62 6f 74 3a 3a 55 70 64 61 74 65 4b 69 6e 64 _bot::UpdateKind
0370: 3a 3a 4d 65 73 73 61 67 65 28 6d 65 73 73 61 67 ::Message(messag
0380: 65 29 20 3d 20 75 70 64 61 74 65 2e 6b 69 6e 64 e) = update.kind
0390: 20 7b 0a 09 09 69 66 20 6c 65 74 20 74 65 6c 65 {...if let tele
03a0: 67 72 61 6d 5f 62 6f 74 3a 3a 4d 65 73 73 61 67 gram_bot::Messag
03b0: 65 4b 69 6e 64 3a 3a 54 65 78 74 20 7b 20 72 65 eKind::Text { re
03c0: 66 20 64 61 74 61 2c 20 2e 2e 20 7d 20 3d 20 6d f data, .. } = m
03d0: 65 73 73 61 67 65 2e 6b 69 6e 64 20 7b 0a 09 09 essage.kind {...
03e0: 09 6c 65 74 20 73 65 6e 64 65 72 20 3d 20 6d 65 .let sender = me
03f0: 73 73 61 67 65 2e 66 72 6f 6d 2e 69 64 3b 0a 09 ssage.from.id;..
0400: 09 09 6c 65 74 20 77 6f 72 64 73 3a 20 56 65 63 ..let words: Vec
0410: 3c 26 73 74 72 3e 20 3d 20 64 61 74 61 2e 73 70 <&str> = data.sp
0420: 6c 69 74 5f 77 68 69 74 65 73 70 61 63 65 28 29 lit_whitespace()
0430: 2e 63 6f 6c 6c 65 63 74 28 29 3b 0a 09 09 09 6d .collect();....m
0440: 61 74 63 68 20 6d 61 74 63 68 20 77 6f 72 64 73 atch match words
0450: 5b 30 5d 20 7b 0a 09 09 09 09 22 2f 63 68 65 63 [0] {....."/chec
0460: 6b 22 20 7c 20 22 2f 63 6c 65 61 6e 22 20 7c 20 k" | "/clean" |
0470: 22 2f 65 6e 61 62 6c 65 22 20 7c 20 22 2f 64 65 "/enable" | "/de
0480: 6c 65 74 65 22 20 7c 20 22 2f 64 69 73 61 62 6c lete" | "/disabl
0490: 65 22 20 3d 3e 20 63 6f 6d 6d 61 6e 64 3a 3a 63 e" => command::c
04a0: 6f 6d 6d 61 6e 64 28 63 6f 72 65 2c 20 73 65 6e ommand(core, sen
04b0: 64 65 72 2c 20 77 6f 72 64 73 29 2e 61 77 61 69 der, words).awai
04c0: 74 2c 0a 09 09 09 09 22 2f 73 74 61 72 74 22 20 t,....."/start"
04d0: 3d 3e 20 63 6f 6d 6d 61 6e 64 3a 3a 73 74 61 72 => command::star
04e0: 74 28 63 6f 72 65 2c 20 73 65 6e 64 65 72 29 2e t(core, sender).
04f0: 61 77 61 69 74 2c 0a 09 09 09 09 22 2f 6c 69 73 await,....."/lis
0500: 74 22 20 3d 3e 20 63 6f 6d 6d 61 6e 64 3a 3a 6c t" => command::l
0510: 69 73 74 28 63 6f 72 65 2c 20 73 65 6e 64 65 72 ist(core, sender
0520: 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 22 2f 61 ).await,....."/a
0530: 64 64 22 20 7c 20 22 2f 75 70 64 61 74 65 22 20 dd" | "/update"
0540: 3d 3e 20 63 6f 6d 6d 61 6e 64 3a 3a 75 70 64 61 => command::upda
0550: 74 65 28 63 6f 72 65 2c 20 73 65 6e 64 65 72 2c te(core, sender,
0560: 20 77 6f 72 64 73 29 2e 61 77 61 69 74 2c 0a 09 words).await,..
0570: 09 09 09 5f 20 3d 3e 20 4f 6b 28 28 29 29 2c 0a ..._ => Ok(()),.
0580: 09 09 09 7d 20 7b 0a 09 09 09 09 45 72 72 28 65 ...} {.....Err(e
0590: 72 72 29 20 3d 3e 20 63 6f 72 65 2e 73 65 6e 64 rr) => core.send
05a0: 28 66 6f 72 6d 61 74 21 28 22 f0 9f 9b 91 20 7b (format!("🛑 {
05b0: 3a 3f 7d 22 2c 20 65 72 72 29 2c 20 53 6f 6d 65 :?}", err), Some
05c0: 28 73 65 6e 64 65 72 29 2c 20 4e 6f 6e 65 29 2e (sender), None).
05d0: 61 77 61 69 74 3f 2c 0a 09 09 09 09 4f 6b 28 28 await?,.....Ok((
05e0: 29 29 20 3d 3e 20 7b 7d 2c 0a 09 09 09 7d 3b 0a )) => {},....};.
05f0: 09 09 7d 3b 0a 09 7d 3b 0a 0a 09 4f 6b 28 28 29 ..};..};...Ok(()
0600: 29 0a 7d 0a ).}.