Artifact
d38ced8c9ce28cd6236fa70647390a4d58ca5fc117af720e09f698186119c6a1:
0000: 2f 2f 21 20 54 68 69 73 20 69 73 20 74 65 6c 65 //! This is tele
0010: 67 72 61 6d 20 62 6f 74 20 74 6f 20 66 65 74 63 gram bot to fetc
0020: 68 20 52 53 53 2f 41 54 4f 4d 20 66 65 65 64 73 h RSS/ATOM feeds
0030: 20 61 6e 64 20 70 6f 73 74 20 72 65 73 75 6c 74 and post result
0040: 73 20 6f 6e 20 70 75 62 6c 69 63 0a 2f 2f 21 20 s on public.//!
0050: 63 68 61 6e 6e 65 6c 73 0a 0a 23 21 5b 77 61 72 channels..#![war
0060: 6e 28 6d 69 73 73 69 6e 67 5f 64 6f 63 73 29 5d n(missing_docs)]
0070: 0a 0a 6d 6f 64 20 63 6f 6d 6d 61 6e 64 3b 0a 6d ..mod command;.m
0080: 6f 64 20 63 6f 72 65 3b 0a 0a 75 73 65 20 61 6e od core;..use an
0090: 79 68 6f 77 3a 3a 52 65 73 75 6c 74 3b 0a 75 73 yhow::Result;.us
00a0: 65 20 61 73 79 6e 63 5f 73 74 64 3a 3a 74 61 73 e async_std::tas
00b0: 6b 3b 0a 75 73 65 20 61 73 79 6e 63 5f 73 74 64 k;.use async_std
00c0: 3a 3a 73 74 72 65 61 6d 3a 3a 53 74 72 65 61 6d ::stream::Stream
00d0: 45 78 74 3b 0a 0a 23 5b 61 73 79 6e 63 5f 73 74 Ext;..#[async_st
00e0: 64 3a 3a 6d 61 69 6e 5d 0a 61 73 79 6e 63 20 66 d::main].async f
00f0: 6e 20 6d 61 69 6e 28 29 20 2d 3e 20 52 65 73 75 n main() -> Resu
0100: 6c 74 3c 28 29 3e 20 7b 0a 09 6c 65 74 20 73 65 lt<()> {..let se
0110: 74 74 69 6e 67 73 20 3d 20 63 6f 6e 66 69 67 3a ttings = config:
0120: 3a 43 6f 6e 66 69 67 3a 3a 62 75 69 6c 64 65 72 :Config::builder
0130: 28 29 0a 09 09 2e 61 64 64 5f 73 6f 75 72 63 65 ()....add_source
0140: 28 63 6f 6e 66 69 67 3a 3a 46 69 6c 65 3a 3a 77 (config::File::w
0150: 69 74 68 5f 6e 61 6d 65 28 22 72 73 73 74 67 22 ith_name("rsstg"
0160: 29 29 0a 09 09 2e 62 75 69 6c 64 28 29 3f 3b 0a ))....build()?;.
0170: 0a 09 6c 65 74 20 63 6f 72 65 20 3d 20 63 6f 72 ..let core = cor
0180: 65 3a 3a 43 6f 72 65 3a 3a 6e 65 77 28 73 65 74 e::Core::new(set
0190: 74 69 6e 67 73 29 3f 3b 0a 0a 09 6c 65 74 20 6d tings)?;...let m
01a0: 75 74 20 73 74 72 65 61 6d 20 3d 20 63 6f 72 65 ut stream = core
01b0: 2e 73 74 72 65 61 6d 28 29 3b 0a 09 73 74 72 65 .stream();..stre
01c0: 61 6d 2e 61 6c 6c 6f 77 65 64 5f 75 70 64 61 74 am.allowed_updat
01d0: 65 73 28 26 5b 74 65 6c 65 67 72 61 6d 5f 62 6f es(&[telegram_bo
01e0: 74 3a 3a 41 6c 6c 6f 77 65 64 55 70 64 61 74 65 t::AllowedUpdate
01f0: 3a 3a 4d 65 73 73 61 67 65 5d 29 3b 0a 0a 09 74 ::Message]);...t
0200: 61 73 6b 3a 3a 62 6c 6f 63 6b 5f 6f 6e 28 61 73 ask::block_on(as
0210: 79 6e 63 20 7b 0a 09 09 6c 65 74 20 6d 75 74 20 ync {...let mut
0220: 72 65 70 6c 79 5f 74 6f 3a 20 4f 70 74 69 6f 6e reply_to: Option
0230: 3c 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 <telegram_bot::U
0240: 73 65 72 49 64 3e 3b 0a 09 09 6c 6f 6f 70 20 7b serId>;...loop {
0250: 0a 09 09 09 72 65 70 6c 79 5f 74 6f 20 3d 20 4e ....reply_to = N
0260: 6f 6e 65 3b 0a 09 09 09 6d 61 74 63 68 20 73 74 one;....match st
0270: 72 65 61 6d 2e 6e 65 78 74 28 29 2e 61 77 61 69 ream.next().awai
0280: 74 20 7b 0a 09 09 09 09 53 6f 6d 65 28 75 70 64 t {.....Some(upd
0290: 61 74 65 29 20 3d 3e 20 7b 0a 09 09 09 09 09 69 ate) => {......i
02a0: 66 20 6c 65 74 20 45 72 72 28 65 72 72 29 20 3d f let Err(err) =
02b0: 20 68 61 6e 64 6c 65 28 75 70 64 61 74 65 3f 2c handle(update?,
02c0: 20 26 63 6f 72 65 2c 20 26 72 65 70 6c 79 5f 74 &core, &reply_t
02d0: 6f 29 2e 61 77 61 69 74 20 7b 0a 09 09 09 09 09 o).await {......
02e0: 09 63 6f 72 65 2e 73 65 6e 64 28 26 66 6f 72 6d .core.send(&form
02f0: 61 74 21 28 22 f0 9f 9b 91 20 7b 3a 3f 7d 22 2c at!("🛑 {:?}",
0300: 20 65 72 72 29 2c 20 72 65 70 6c 79 5f 74 6f 2c err), reply_to,
0310: 20 4e 6f 6e 65 29 2e 61 77 61 69 74 3f 3b 0a 09 None).await?;..
0320: 09 09 09 09 7d 3b 0a 09 09 09 09 7d 2c 0a 09 09 ....};.....},...
0330: 09 09 4e 6f 6e 65 20 3d 3e 20 7b 0a 09 09 09 09 ..None => {.....
0340: 09 63 6f 72 65 2e 73 65 6e 64 28 22 f0 9f 9b 91 .core.send("🛑
0350: 20 4e 6f 6e 65 20 65 72 72 6f 72 2e 22 2c 20 4e None error.", N
0360: 6f 6e 65 2c 20 4e 6f 6e 65 29 2e 61 77 61 69 74 one, None).await
0370: 3f 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 ?;.....}....};..
0380: 09 7d 0a 09 7d 29 0a 7d 0a 0a 61 73 79 6e 63 20 .}..}).}..async
0390: 66 6e 20 68 61 6e 64 6c 65 28 75 70 64 61 74 65 fn handle(update
03a0: 3a 20 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a : telegram_bot::
03b0: 55 70 64 61 74 65 2c 20 63 6f 72 65 3a 20 26 63 Update, core: &c
03c0: 6f 72 65 3a 3a 43 6f 72 65 2c 20 6d 75 74 20 5f ore::Core, mut _
03d0: 72 65 70 6c 79 5f 74 6f 3a 20 26 4f 70 74 69 6f reply_to: &Optio
03e0: 6e 3c 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a n<telegram_bot::
03f0: 55 73 65 72 49 64 3e 29 20 2d 3e 20 52 65 73 75 UserId>) -> Resu
0400: 6c 74 3c 28 29 3e 20 7b 0a 09 69 66 20 6c 65 74 lt<()> {..if let
0410: 20 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 telegram_bot::U
0420: 70 64 61 74 65 4b 69 6e 64 3a 3a 4d 65 73 73 61 pdateKind::Messa
0430: 67 65 28 6d 65 73 73 61 67 65 29 20 3d 20 75 70 ge(message) = up
0440: 64 61 74 65 2e 6b 69 6e 64 20 7b 0a 09 09 69 66 date.kind {...if
0450: 20 6c 65 74 20 53 6f 6d 65 28 66 72 6f 6d 29 20 let Some(from)
0460: 3d 20 6d 65 73 73 61 67 65 2e 66 72 6f 6d 20 7b = message.from {
0470: 0a 09 09 09 69 66 20 6c 65 74 20 74 65 6c 65 67 ....if let teleg
0480: 72 61 6d 5f 62 6f 74 3a 3a 4d 65 73 73 61 67 65 ram_bot::Message
0490: 4b 69 6e 64 3a 3a 54 65 78 74 7b 20 72 65 66 20 Kind::Text{ ref
04a0: 64 61 74 61 2c 20 2e 2e 20 7d 20 3d 20 6d 65 73 data, .. } = mes
04b0: 73 61 67 65 2e 6b 69 6e 64 20 7b 0a 09 09 09 09 sage.kind {.....
04c0: 6c 65 74 20 73 65 6e 64 65 72 20 3d 20 66 72 6f let sender = fro
04d0: 6d 2e 69 64 3b 0a 09 09 09 09 6c 65 74 20 77 6f m.id;.....let wo
04e0: 72 64 73 3a 20 56 65 63 3c 26 73 74 72 3e 20 3d rds: Vec<&str> =
04f0: 20 64 61 74 61 2e 73 70 6c 69 74 5f 77 68 69 74 data.split_whit
0500: 65 73 70 61 63 65 28 29 2e 63 6f 6c 6c 65 63 74 espace().collect
0510: 28 29 3b 0a 09 09 09 09 69 66 20 6c 65 74 20 45 ();.....if let E
0520: 72 72 28 65 72 72 29 20 3d 20 6d 61 74 63 68 20 rr(err) = match
0530: 77 6f 72 64 73 5b 30 5d 20 7b 0a 09 09 09 09 09 words[0] {......
0540: 22 2f 63 68 65 63 6b 22 20 7c 20 22 2f 63 6c 65 "/check" | "/cle
0550: 61 6e 22 20 7c 20 22 2f 65 6e 61 62 6c 65 22 20 an" | "/enable"
0560: 7c 20 22 2f 64 65 6c 65 74 65 22 20 7c 20 22 2f | "/delete" | "/
0570: 64 69 73 61 62 6c 65 22 20 3d 3e 20 63 6f 6d 6d disable" => comm
0580: 61 6e 64 3a 3a 63 6f 6d 6d 61 6e 64 28 63 6f 72 and::command(cor
0590: 65 2c 20 73 65 6e 64 65 72 2c 20 77 6f 72 64 73 e, sender, words
05a0: 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 09 22 2f ).await,......"/
05b0: 73 74 61 72 74 22 20 3d 3e 20 63 6f 6d 6d 61 6e start" => comman
05c0: 64 3a 3a 73 74 61 72 74 28 63 6f 72 65 2c 20 73 d::start(core, s
05d0: 65 6e 64 65 72 29 2e 61 77 61 69 74 2c 0a 09 09 ender).await,...
05e0: 09 09 09 22 2f 6c 69 73 74 22 20 3d 3e 20 63 6f ..."/list" => co
05f0: 6d 6d 61 6e 64 3a 3a 6c 69 73 74 28 63 6f 72 65 mmand::list(core
0600: 2c 20 73 65 6e 64 65 72 29 2e 61 77 61 69 74 2c , sender).await,
0610: 0a 09 09 09 09 09 22 2f 61 64 64 22 20 7c 20 22 ......"/add" | "
0620: 2f 75 70 64 61 74 65 22 20 3d 3e 20 63 6f 6d 6d /update" => comm
0630: 61 6e 64 3a 3a 75 70 64 61 74 65 28 63 6f 72 65 and::update(core
0640: 2c 20 73 65 6e 64 65 72 2c 20 77 6f 72 64 73 29 , sender, words)
0650: 2e 61 77 61 69 74 2c 0a 09 09 09 09 09 5f 20 3d .await,......_ =
0660: 3e 20 4f 6b 28 28 29 29 2c 0a 09 09 09 09 7d 20 > Ok(()),.....}
0670: 7b 0a 09 09 09 09 09 63 6f 72 65 2e 73 65 6e 64 {......core.send
0680: 28 66 6f 72 6d 61 74 21 28 22 f0 9f 9b 91 20 7b (format!("🛑 {
0690: 3a 3f 7d 22 2c 20 65 72 72 29 2c 20 53 6f 6d 65 :?}", err), Some
06a0: 28 73 65 6e 64 65 72 29 2c 20 4e 6f 6e 65 29 2e (sender), None).
06b0: 61 77 61 69 74 3f 3b 0a 09 09 09 09 7d 3b 0a 09 await?;.....};..
06c0: 09 09 7d 3b 0a 09 09 7d 3b 0a 09 7d 3b 0a 0a 09 ..};...};..};...
06d0: 4f 6b 28 28 29 29 0a 7d 0a Ok(()).}.