Artifact
81a78d8ceead01e0549fe2d0f944955836c9e8156b868a1548742d39ba39f96a:
0000: 75 73 65 20 73 74 61 63 6b 65 64 5f 65 72 72 6f use stacked_erro
0010: 72 73 3a 3a 7b 0a 09 52 65 73 75 6c 74 2c 0a 09 rs::{..Result,..
0020: 53 74 61 63 6b 61 62 6c 65 45 72 72 2c 0a 7d 3b StackableErr,.};
0030: 0a 75 73 65 20 74 67 62 6f 74 3a 3a 7b 0a 09 61 .use tgbot::{..a
0040: 70 69 3a 3a 43 6c 69 65 6e 74 2c 0a 09 74 79 70 pi::Client,..typ
0050: 65 73 3a 3a 7b 0a 09 09 42 6f 74 2c 0a 09 09 43 es::{...Bot,...C
0060: 68 61 74 50 65 65 72 49 64 2c 0a 09 09 47 65 74 hatPeerId,...Get
0070: 42 6f 74 2c 0a 09 09 4d 65 73 73 61 67 65 2c 0a Bot,...Message,.
0080: 09 09 50 61 72 73 65 4d 6f 64 65 2c 0a 09 09 53 ..ParseMode,...S
0090: 65 6e 64 4d 65 73 73 61 67 65 2c 0a 09 7d 2c 0a endMessage,..},.
00a0: 7d 3b 0a 0a 23 5b 64 65 72 69 76 65 28 43 6c 6f };..#[derive(Clo
00b0: 6e 65 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20 ne)].pub struct
00c0: 54 67 20 7b 0a 09 70 75 62 20 6d 65 3a 20 42 6f Tg {..pub me: Bo
00d0: 74 2c 0a 09 70 75 62 20 6f 77 6e 65 72 3a 20 43 t,..pub owner: C
00e0: 68 61 74 50 65 65 72 49 64 2c 0a 09 70 75 62 20 hatPeerId,..pub
00f0: 63 6c 69 65 6e 74 3a 20 43 6c 69 65 6e 74 2c 0a client: Client,.
0100: 7d 0a 0a 69 6d 70 6c 20 54 67 20 7b 0a 09 2f 2f }..impl Tg {..//
0110: 2f 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 / Construct a ne
0120: 77 20 60 54 67 60 20 69 6e 73 74 61 6e 63 65 20 w `Tg` instance
0130: 66 72 6f 6d 20 63 6f 6e 66 69 67 75 72 61 74 69 from configurati
0140: 6f 6e 2e 0a 09 2f 2f 2f 0a 09 2f 2f 2f 20 54 68 on...///../// Th
0150: 65 20 60 73 65 74 74 69 6e 67 73 60 20 6d 75 73 e `settings` mus
0160: 74 20 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f t provide the fo
0170: 6c 6c 6f 77 69 6e 67 20 6b 65 79 73 3a 0a 09 2f llowing keys:../
0180: 2f 2f 20 20 2d 20 60 22 61 70 69 5f 6b 65 79 22 // - `"api_key"
0190: 60 20 28 73 74 72 69 6e 67 29 2c 0a 09 2f 2f 2f ` (string),..///
01a0: 20 20 2d 20 60 22 6f 77 6e 65 72 22 60 20 28 69 - `"owner"` (i
01b0: 6e 74 65 67 65 72 20 63 68 61 74 20 69 64 29 2c nteger chat id),
01c0: 0a 09 2f 2f 2f 20 20 2d 20 60 22 61 70 69 5f 67 ../// - `"api_g
01d0: 61 74 65 77 61 79 22 60 20 28 73 74 72 69 6e 67 ateway"` (string
01e0: 29 2e 0a 09 2f 2f 2f 0a 09 2f 2f 2f 20 54 68 65 )...///../// The
01f0: 20 66 75 6e 63 74 69 6f 6e 20 69 6e 69 74 69 61 function initia
0200: 6c 69 73 65 73 20 74 68 65 20 63 6c 69 65 6e 74 lises the client
0210: 2c 20 63 6f 6e 66 69 67 75 72 65 73 20 74 68 65 , configures the
0220: 20 67 61 74 65 77 61 79 20 61 6e 64 20 66 65 74 gateway and fet
0230: 63 68 65 73 20 74 68 65 20 62 6f 74 20 69 64 65 ches the bot ide
0240: 6e 74 69 74 79 0a 09 2f 2f 2f 20 62 65 66 6f 72 ntity../// befor
0250: 65 20 72 65 74 75 72 6e 69 6e 67 20 74 68 65 20 e returning the
0260: 63 6f 6e 73 74 72 75 63 74 65 64 20 60 54 67 60 constructed `Tg`
0270: 2e 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 ...pub async fn
0280: 6e 65 77 20 28 73 65 74 74 69 6e 67 73 3a 20 26 new (settings: &
0290: 63 6f 6e 66 69 67 3a 3a 43 6f 6e 66 69 67 29 20 config::Config)
02a0: 2d 3e 20 52 65 73 75 6c 74 3c 54 67 3e 20 7b 0a -> Result<Tg> {.
02b0: 09 09 6c 65 74 20 61 70 69 5f 6b 65 79 20 3d 20 ..let api_key =
02c0: 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 73 74 72 settings.get_str
02d0: 69 6e 67 28 22 61 70 69 5f 6b 65 79 22 29 2e 73 ing("api_key").s
02e0: 74 61 63 6b 28 29 3f 3b 0a 0a 09 09 6c 65 74 20 tack()?;....let
02f0: 6f 77 6e 65 72 20 3d 20 43 68 61 74 50 65 65 72 owner = ChatPeer
0300: 49 64 3a 3a 66 72 6f 6d 28 73 65 74 74 69 6e 67 Id::from(setting
0310: 73 2e 67 65 74 5f 69 6e 74 28 22 6f 77 6e 65 72 s.get_int("owner
0320: 22 29 2e 73 74 61 63 6b 28 29 3f 29 3b 0a 09 09 ").stack()?);...
0330: 6c 65 74 20 63 6c 69 65 6e 74 20 3d 20 43 6c 69 let client = Cli
0340: 65 6e 74 3a 3a 6e 65 77 28 26 61 70 69 5f 6b 65 ent::new(&api_ke
0350: 79 29 2e 73 74 61 63 6b 28 29 3f 0a 09 09 09 2e y).stack()?.....
0360: 77 69 74 68 5f 68 6f 73 74 28 73 65 74 74 69 6e with_host(settin
0370: 67 73 2e 67 65 74 5f 73 74 72 69 6e 67 28 22 61 gs.get_string("a
0380: 70 69 5f 67 61 74 65 77 61 79 22 29 2e 73 74 61 pi_gateway").sta
0390: 63 6b 28 29 3f 29 0a 09 09 09 2e 77 69 74 68 5f ck()?).....with_
03a0: 6d 61 78 5f 72 65 74 72 69 65 73 28 30 29 3b 0a max_retries(0);.
03b0: 09 09 6c 65 74 20 6d 65 20 3d 20 63 6c 69 65 6e ..let me = clien
03c0: 74 2e 65 78 65 63 75 74 65 28 47 65 74 42 6f 74 t.execute(GetBot
03d0: 29 2e 61 77 61 69 74 2e 73 74 61 63 6b 28 29 3f ).await.stack()?
03e0: 3b 0a 09 09 4f 6b 28 54 67 20 7b 0a 09 09 09 6d ;...Ok(Tg {....m
03f0: 65 2c 0a 09 09 09 6f 77 6e 65 72 2c 0a 09 09 09 e,....owner,....
0400: 63 6c 69 65 6e 74 2c 0a 09 09 7d 29 0a 09 7d 0a client,...})..}.
0410: 0a 09 2f 2f 2f 20 53 65 6e 64 20 61 20 74 65 78 ../// Send a tex
0420: 74 20 6d 65 73 73 61 67 65 20 74 6f 20 61 20 63 t message to a c
0430: 68 61 74 2c 20 75 73 69 6e 67 20 61 6e 20 6f 70 hat, using an op
0440: 74 69 6f 6e 61 6c 20 74 61 72 67 65 74 20 61 6e tional target an
0450: 64 20 70 61 72 73 65 20 6d 6f 64 65 2e 0a 09 2f d parse mode.../
0460: 2f 2f 0a 09 2f 2f 2f 20 23 20 52 65 74 75 72 6e //../// # Return
0470: 73 0a 09 2f 2f 2f 20 54 68 65 20 73 65 6e 74 20 s../// The sent
0480: 60 4d 65 73 73 61 67 65 60 20 6f 6e 20 73 75 63 `Message` on suc
0490: 63 65 73 73 2e 0a 09 70 75 62 20 61 73 79 6e 63 cess...pub async
04a0: 20 66 6e 20 73 65 6e 64 20 3c 53 3e 28 26 73 65 fn send <S>(&se
04b0: 6c 66 2c 20 6d 73 67 3a 20 53 2c 20 74 61 72 67 lf, msg: S, targ
04c0: 65 74 3a 20 4f 70 74 69 6f 6e 3c 43 68 61 74 50 et: Option<ChatP
04d0: 65 65 72 49 64 3e 2c 20 6d 6f 64 65 3a 20 4f 70 eerId>, mode: Op
04e0: 74 69 6f 6e 3c 50 61 72 73 65 4d 6f 64 65 3e 29 tion<ParseMode>)
04f0: 20 2d 3e 20 52 65 73 75 6c 74 3c 4d 65 73 73 61 -> Result<Messa
0500: 67 65 3e 0a 09 77 68 65 72 65 20 53 3a 20 49 6e ge>..where S: In
0510: 74 6f 3c 53 74 72 69 6e 67 3e 20 7b 0a 09 09 6c to<String> {...l
0520: 65 74 20 6d 73 67 20 3d 20 6d 73 67 2e 69 6e 74 et msg = msg.int
0530: 6f 28 29 3b 0a 0a 09 09 6c 65 74 20 6d 6f 64 65 o();....let mode
0540: 20 3d 20 6d 6f 64 65 2e 75 6e 77 72 61 70 5f 6f = mode.unwrap_o
0550: 72 28 50 61 72 73 65 4d 6f 64 65 3a 3a 48 74 6d r(ParseMode::Htm
0560: 6c 29 3b 0a 09 09 6c 65 74 20 74 61 72 67 65 74 l);...let target
0570: 20 3d 20 74 61 72 67 65 74 2e 75 6e 77 72 61 70 = target.unwrap
0580: 5f 6f 72 28 73 65 6c 66 2e 6f 77 6e 65 72 29 3b _or(self.owner);
0590: 0a 09 09 73 65 6c 66 2e 63 6c 69 65 6e 74 2e 65 ...self.client.e
05a0: 78 65 63 75 74 65 28 0a 09 09 09 53 65 6e 64 4d xecute(....SendM
05b0: 65 73 73 61 67 65 3a 3a 6e 65 77 28 74 61 72 67 essage::new(targ
05c0: 65 74 2c 20 6d 73 67 29 0a 09 09 09 09 2e 77 69 et, msg)......wi
05d0: 74 68 5f 70 61 72 73 65 5f 6d 6f 64 65 28 6d 6f th_parse_mode(mo
05e0: 64 65 29 0a 09 09 29 2e 61 77 61 69 74 2e 73 74 de)...).await.st
05f0: 61 63 6b 28 29 0a 09 7d 0a 0a 09 2f 2f 2f 20 43 ack()..}.../// C
0600: 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 reate a copy of
0610: 74 68 69 73 20 60 54 67 60 20 77 69 74 68 20 74 this `Tg` with t
0620: 68 65 20 6f 77 6e 65 72 20 72 65 70 6c 61 63 65 he owner replace
0630: 64 20 62 79 20 74 68 65 20 67 69 76 65 6e 20 63 d by the given c
0640: 68 61 74 20 49 44 2e 0a 09 2f 2f 2f 0a 09 2f 2f hat ID...///..//
0650: 2f 20 23 20 50 61 72 61 6d 65 74 65 72 73 0a 09 / # Parameters..
0660: 2f 2f 2f 20 2d 20 60 6f 77 6e 65 72 60 3a 20 54 /// - `owner`: T
0670: 68 65 20 54 65 6c 65 67 72 61 6d 20 63 68 61 74 he Telegram chat
0680: 20 69 64 65 6e 74 69 66 69 65 72 20 74 6f 20 73 identifier to s
0690: 65 74 20 61 73 20 74 68 65 20 6e 65 77 20 6f 77 et as the new ow
06a0: 6e 65 72 20 28 65 78 70 72 65 73 73 65 64 20 61 ner (expressed a
06b0: 73 20 61 6e 20 60 69 36 34 60 29 2e 0a 09 2f 2f s an `i64`)...//
06c0: 2f 0a 09 2f 2f 2f 20 23 20 52 65 74 75 72 6e 73 /../// # Returns
06d0: 0a 09 2f 2f 2f 20 41 20 6e 65 77 20 60 54 67 60 ../// A new `Tg`
06e0: 20 69 6e 73 74 61 6e 63 65 20 69 64 65 6e 74 69 instance identi
06f0: 63 61 6c 20 74 6f 20 74 68 65 20 6f 72 69 67 69 cal to the origi
0700: 6e 61 6c 20 65 78 63 65 70 74 20 69 74 73 20 60 nal except its `
0710: 6f 77 6e 65 72 60 20 66 69 65 6c 64 20 69 73 20 owner` field is
0720: 73 65 74 20 74 6f 20 74 68 65 20 70 72 6f 76 69 set to the provi
0730: 64 65 64 20 63 68 61 74 20 49 44 2e 0a 09 70 75 ded chat ID...pu
0740: 62 20 66 6e 20 77 69 74 68 5f 6f 77 6e 65 72 20 b fn with_owner
0750: 3c 4f 3e 28 26 73 65 6c 66 2c 20 6f 77 6e 65 72 <O>(&self, owner
0760: 3a 20 4f 29 20 2d 3e 20 54 67 0a 09 77 68 65 72 : O) -> Tg..wher
0770: 65 20 4f 3a 20 49 6e 74 6f 3c 69 36 34 3e 20 7b e O: Into<i64> {
0780: 0a 09 09 54 67 20 7b 0a 09 09 09 6f 77 6e 65 72 ...Tg {....owner
0790: 3a 20 43 68 61 74 50 65 65 72 49 64 3a 3a 66 72 : ChatPeerId::fr
07a0: 6f 6d 28 6f 77 6e 65 72 2e 69 6e 74 6f 28 29 29 om(owner.into())
07b0: 2c 0a 09 09 09 2e 2e 73 65 6c 66 2e 63 6c 6f 6e ,......self.clon
07c0: 65 28 29 0a 09 09 7d 0a 09 7d 0a 7d 0a e()...}..}.}.