Artifact
039d4ea3890df8c89a6467360f7361eeca419affbfec218ac25b4d59222ea1de:
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 61 6e 79 68 core;..use anyh
0020: 6f 77 3a 3a 52 65 73 75 6c 74 3b 0a 75 73 65 20 ow::Result;.use
0030: 61 73 79 6e 63 5f 73 74 64 3a 3a 74 61 73 6b 3b async_std::task;
0040: 0a 75 73 65 20 61 73 79 6e 63 5f 73 74 64 3a 3a .use async_std::
0050: 73 74 72 65 61 6d 3a 3a 53 74 72 65 61 6d 45 78 stream::StreamEx
0060: 74 3b 0a 0a 23 5b 61 73 79 6e 63 5f 73 74 64 3a t;..#[async_std:
0070: 3a 6d 61 69 6e 5d 0a 61 73 79 6e 63 20 66 6e 20 :main].async fn
0080: 6d 61 69 6e 28 29 20 2d 3e 20 52 65 73 75 6c 74 main() -> Result
0090: 3c 28 29 3e 20 7b 0a 09 6c 65 74 20 73 65 74 74 <()> {..let sett
00a0: 69 6e 67 73 20 3d 20 63 6f 6e 66 69 67 3a 3a 43 ings = config::C
00b0: 6f 6e 66 69 67 3a 3a 62 75 69 6c 64 65 72 28 29 onfig::builder()
00c0: 0a 09 09 2e 61 64 64 5f 73 6f 75 72 63 65 28 63 ....add_source(c
00d0: 6f 6e 66 69 67 3a 3a 46 69 6c 65 3a 3a 77 69 74 onfig::File::wit
00e0: 68 5f 6e 61 6d 65 28 22 72 73 73 74 67 22 29 29 h_name("rsstg"))
00f0: 0a 09 09 2e 62 75 69 6c 64 28 29 3f 3b 0a 0a 09 ....build()?;...
0100: 6c 65 74 20 63 6f 72 65 20 3d 20 63 6f 72 65 3a let core = core:
0110: 3a 43 6f 72 65 3a 3a 6e 65 77 28 73 65 74 74 69 :Core::new(setti
0120: 6e 67 73 29 3f 3b 0a 0a 09 6c 65 74 20 6d 75 74 ngs)?;...let mut
0130: 20 73 74 72 65 61 6d 20 3d 20 63 6f 72 65 2e 73 stream = core.s
0140: 74 72 65 61 6d 28 29 3b 0a 09 73 74 72 65 61 6d tream();..stream
0150: 2e 61 6c 6c 6f 77 65 64 5f 75 70 64 61 74 65 73 .allowed_updates
0160: 28 26 5b 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a (&[telegram_bot:
0170: 3a 41 6c 6c 6f 77 65 64 55 70 64 61 74 65 3a 3a :AllowedUpdate::
0180: 4d 65 73 73 61 67 65 5d 29 3b 0a 0a 09 74 61 73 Message]);...tas
0190: 6b 3a 3a 62 6c 6f 63 6b 5f 6f 6e 28 61 73 79 6e k::block_on(asyn
01a0: 63 20 7b 0a 09 09 6c 65 74 20 6d 75 74 20 72 65 c {...let mut re
01b0: 70 6c 79 5f 74 6f 3a 20 4f 70 74 69 6f 6e 3c 74 ply_to: Option<t
01c0: 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 73 65 elegram_bot::Use
01d0: 72 49 64 3e 3b 0a 09 09 6c 6f 6f 70 20 7b 0a 09 rId>;...loop {..
01e0: 09 09 72 65 70 6c 79 5f 74 6f 20 3d 20 4e 6f 6e ..reply_to = Non
01f0: 65 3b 0a 09 09 09 6d 61 74 63 68 20 73 74 72 65 e;....match stre
0200: 61 6d 2e 6e 65 78 74 28 29 2e 61 77 61 69 74 20 am.next().await
0210: 7b 0a 09 09 09 09 53 6f 6d 65 28 75 70 64 61 74 {.....Some(updat
0220: 65 29 20 3d 3e 20 7b 0a 09 09 09 09 09 69 66 20 e) => {......if
0230: 6c 65 74 20 45 72 72 28 65 72 72 29 20 3d 20 68 let Err(err) = h
0240: 61 6e 64 6c 65 28 75 70 64 61 74 65 3f 2c 20 26 andle(update?, &
0250: 63 6f 72 65 2c 20 26 72 65 70 6c 79 5f 74 6f 29 core, &reply_to)
0260: 2e 61 77 61 69 74 20 7b 0a 09 09 09 09 09 09 63 .await {.......c
0270: 6f 72 65 2e 73 65 6e 64 28 26 66 6f 72 6d 61 74 ore.send(&format
0280: 21 28 22 f0 9f 9b 91 20 7b 3a 3f 7d 22 2c 20 65 !("🛑 {:?}", e
0290: 72 72 29 2c 20 72 65 70 6c 79 5f 74 6f 2c 20 4e rr), reply_to, N
02a0: 6f 6e 65 29 2e 61 77 61 69 74 3f 3b 0a 09 09 09 one).await?;....
02b0: 09 09 7d 3b 0a 09 09 09 09 7d 2c 0a 09 09 09 09 ..};.....},.....
02c0: 4e 6f 6e 65 20 3d 3e 20 7b 0a 09 09 09 09 09 63 None => {......c
02d0: 6f 72 65 2e 73 65 6e 64 28 22 f0 9f 9b 91 20 4e ore.send("🛑 N
02e0: 6f 6e 65 20 65 72 72 6f 72 2e 22 2c 20 4e 6f 6e one error.", Non
02f0: 65 2c 20 4e 6f 6e 65 29 2e 61 77 61 69 74 3f 3b e, None).await?;
0300: 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 09 7d .....}....};...}
0310: 0a 09 7d 29 0a 7d 0a 0a 61 73 79 6e 63 20 66 6e ..}).}..async fn
0320: 20 68 61 6e 64 6c 65 28 75 70 64 61 74 65 3a 20 handle(update:
0330: 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 70 telegram_bot::Up
0340: 64 61 74 65 2c 20 63 6f 72 65 3a 20 26 63 6f 72 date, core: &cor
0350: 65 3a 3a 43 6f 72 65 2c 20 6d 75 74 20 5f 72 65 e::Core, mut _re
0360: 70 6c 79 5f 74 6f 3a 20 26 4f 70 74 69 6f 6e 3c ply_to: &Option<
0370: 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 73 telegram_bot::Us
0380: 65 72 49 64 3e 29 20 2d 3e 20 52 65 73 75 6c 74 erId>) -> Result
0390: 3c 28 29 3e 20 7b 0a 09 69 66 20 6c 65 74 20 74 <()> {..if let t
03a0: 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 70 64 elegram_bot::Upd
03b0: 61 74 65 4b 69 6e 64 3a 3a 4d 65 73 73 61 67 65 ateKind::Message
03c0: 28 6d 65 73 73 61 67 65 29 20 3d 20 75 70 64 61 (message) = upda
03d0: 74 65 2e 6b 69 6e 64 20 7b 0a 09 09 69 66 20 6c te.kind {...if l
03e0: 65 74 20 53 6f 6d 65 28 66 72 6f 6d 29 20 3d 20 et Some(from) =
03f0: 6d 65 73 73 61 67 65 2e 66 72 6f 6d 20 7b 0a 09 message.from {..
0400: 09 09 69 66 20 6c 65 74 20 74 65 6c 65 67 72 61 ..if let telegra
0410: 6d 5f 62 6f 74 3a 3a 4d 65 73 73 61 67 65 4b 69 m_bot::MessageKi
0420: 6e 64 3a 3a 54 65 78 74 7b 20 72 65 66 20 64 61 nd::Text{ ref da
0430: 74 61 2c 20 2e 2e 20 7d 20 3d 20 6d 65 73 73 61 ta, .. } = messa
0440: 67 65 2e 6b 69 6e 64 20 7b 0a 09 09 09 09 6c 65 ge.kind {.....le
0450: 74 20 73 65 6e 64 65 72 20 3d 20 66 72 6f 6d 2e t sender = from.
0460: 69 64 3b 0a 09 09 09 09 6c 65 74 20 77 6f 72 64 id;.....let word
0470: 73 3a 20 56 65 63 3c 26 73 74 72 3e 20 3d 20 64 s: Vec<&str> = d
0480: 61 74 61 2e 73 70 6c 69 74 5f 77 68 69 74 65 73 ata.split_whites
0490: 70 61 63 65 28 29 2e 63 6f 6c 6c 65 63 74 28 29 pace().collect()
04a0: 3b 0a 09 09 09 09 69 66 20 6c 65 74 20 45 72 72 ;.....if let Err
04b0: 28 65 72 72 29 20 3d 20 6d 61 74 63 68 20 77 6f (err) = match wo
04c0: 72 64 73 5b 30 5d 20 7b 0a 09 09 09 09 09 22 2f rds[0] {......"/
04d0: 63 68 65 63 6b 22 20 7c 20 22 2f 63 6c 65 61 6e check" | "/clean
04e0: 22 20 7c 20 22 2f 65 6e 61 62 6c 65 22 20 7c 20 " | "/enable" |
04f0: 22 2f 64 65 6c 65 74 65 22 20 7c 20 22 2f 64 69 "/delete" | "/di
0500: 73 61 62 6c 65 22 20 3d 3e 20 63 6f 6d 6d 61 6e sable" => comman
0510: 64 3a 3a 63 6f 6d 6d 61 6e 64 28 63 6f 72 65 2c d::command(core,
0520: 20 73 65 6e 64 65 72 2c 20 77 6f 72 64 73 29 2e sender, words).
0530: 61 77 61 69 74 2c 0a 09 09 09 09 09 22 2f 73 74 await,......"/st
0540: 61 72 74 22 20 3d 3e 20 63 6f 6d 6d 61 6e 64 3a art" => command:
0550: 3a 73 74 61 72 74 28 63 6f 72 65 2c 20 73 65 6e :start(core, sen
0560: 64 65 72 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 der).await,.....
0570: 09 22 2f 6c 69 73 74 22 20 3d 3e 20 63 6f 6d 6d ."/list" => comm
0580: 61 6e 64 3a 3a 6c 69 73 74 28 63 6f 72 65 2c 20 and::list(core,
0590: 73 65 6e 64 65 72 29 2e 61 77 61 69 74 2c 0a 09 sender).await,..
05a0: 09 09 09 09 22 2f 61 64 64 22 20 7c 20 22 2f 75 ...."/add" | "/u
05b0: 70 64 61 74 65 22 20 3d 3e 20 63 6f 6d 6d 61 6e pdate" => comman
05c0: 64 3a 3a 75 70 64 61 74 65 28 63 6f 72 65 2c 20 d::update(core,
05d0: 73 65 6e 64 65 72 2c 20 77 6f 72 64 73 29 2e 61 sender, words).a
05e0: 77 61 69 74 2c 0a 09 09 09 09 09 5f 20 3d 3e 20 wait,......_ =>
05f0: 4f 6b 28 28 29 29 2c 0a 09 09 09 09 7d 20 7b 0a Ok(()),.....} {.
0600: 09 09 09 09 09 63 6f 72 65 2e 73 65 6e 64 28 66 .....core.send(f
0610: 6f 72 6d 61 74 21 28 22 f0 9f 9b 91 20 7b 3a 3f ormat!("🛑 {:?
0620: 7d 22 2c 20 65 72 72 29 2c 20 53 6f 6d 65 28 73 }", err), Some(s
0630: 65 6e 64 65 72 29 2c 20 4e 6f 6e 65 29 2e 61 77 ender), None).aw
0640: 61 69 74 3f 3b 0a 09 09 09 09 7d 3b 0a 09 09 09 ait?;.....};....
0650: 7d 3b 0a 09 09 7d 3b 0a 09 7d 3b 0a 0a 09 4f 6b };...};..};...Ok
0660: 28 28 29 29 0a 7d 0a (()).}.