Hex Artifact Content
Logged in as anonymous

Artifact 1e478790748cdc156880ef168aadd22c81f4e750d73f451ecb4154c3fa147b5b:


0000: 75 73 65 20 63 72 61 74 65 3a 3a 75 74 69 6c 73  use crate::utils
0010: 3a 3a 7b 0a 09 41 74 74 61 63 68 6d 65 6e 74 2c  ::{..Attachment,
0020: 0a 09 52 45 5f 53 50 45 43 49 41 4c 2c 0a 7d 3b  ..RE_SPECIAL,.};
0030: 0a 0a 75 73 65 20 73 74 64 3a 3a 7b 0a 09 62 6f  ..use std::{..bo
0040: 72 72 6f 77 3a 3a 43 6f 77 2c 0a 09 63 6f 6c 6c  rrow::Cow,..coll
0050: 65 63 74 69 6f 6e 73 3a 3a 48 61 73 68 4d 61 70  ections::HashMap
0060: 2c 0a 09 66 6d 74 3a 3a 44 65 62 75 67 2c 0a 7d  ,..fmt::Debug,.}
0070: 3b 0a 0a 75 73 65 20 61 6e 79 68 6f 77 3a 3a 7b  ;..use anyhow::{
0080: 0a 09 43 6f 6e 74 65 78 74 2c 0a 09 52 65 73 75  ..Context,..Resu
0090: 6c 74 2c 0a 7d 3b 0a 75 73 65 20 74 67 62 6f 74  lt,.};.use tgbot
00a0: 3a 3a 7b 0a 09 61 70 69 3a 3a 43 6c 69 65 6e 74  ::{..api::Client
00b0: 2c 0a 09 74 79 70 65 73 3a 3a 7b 0a 09 09 43 68  ,..types::{...Ch
00c0: 61 74 50 65 65 72 49 64 2c 0a 09 09 49 6e 70 75  atPeerId,...Inpu
00d0: 74 46 69 6c 65 2c 0a 09 09 49 6e 70 75 74 46 69  tFile,...InputFi
00e0: 6c 65 52 65 61 64 65 72 2c 0a 09 09 49 6e 70 75  leReader,...Inpu
00f0: 74 4d 65 64 69 61 44 6f 63 75 6d 65 6e 74 2c 0a  tMediaDocument,.
0100: 09 09 4d 65 64 69 61 47 72 6f 75 70 2c 0a 09 09  ..MediaGroup,...
0110: 4d 65 64 69 61 47 72 6f 75 70 49 74 65 6d 2c 0a  MediaGroupItem,.
0120: 09 09 4d 65 73 73 61 67 65 2c 0a 09 09 50 61 72  ..Message,...Par
0130: 73 65 4d 6f 64 65 3a 3a 4d 61 72 6b 64 6f 77 6e  seMode::Markdown
0140: 56 32 2c 0a 09 09 53 65 6e 64 4d 65 64 69 61 47  V2,...SendMediaG
0150: 72 6f 75 70 2c 0a 09 09 53 65 6e 64 4d 65 73 73  roup,...SendMess
0160: 61 67 65 2c 0a 09 09 53 65 6e 64 44 6f 63 75 6d  age,...SendDocum
0170: 65 6e 74 2c 0a 09 7d 2c 0a 7d 3b 0a 0a 2f 2f 2f  ent,..},.};..///
0180: 20 45 6e 63 6f 64 65 73 20 73 70 65 63 69 61 6c   Encodes special
0190: 20 48 54 4d 4c 20 65 6e 74 69 74 69 65 73 20 74   HTML entities t
01a0: 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d 20 69  o prevent them i
01b0: 6e 74 65 72 66 65 72 69 6e 67 20 77 69 74 68 20  nterfering with 
01c0: 54 65 6c 65 67 72 61 6d 20 48 54 4d 4c 0a 70 75  Telegram HTML.pu
01d0: 62 20 66 6e 20 65 6e 63 6f 64 65 20 28 74 65 78  b fn encode (tex
01e0: 74 3a 20 26 73 74 72 29 20 2d 3e 20 43 6f 77 3c  t: &str) -> Cow<
01f0: 27 5f 2c 20 73 74 72 3e 20 7b 0a 09 52 45 5f 53  '_, str> {..RE_S
0200: 50 45 43 49 41 4c 2e 72 65 70 6c 61 63 65 5f 61  PECIAL.replace_a
0210: 6c 6c 28 74 65 78 74 2c 20 22 5c 5c 24 31 22 29  ll(text, "\\$1")
0220: 0a 7d 0a 0a 23 5b 64 65 72 69 76 65 28 44 65 62  .}..#[derive(Deb
0230: 75 67 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20  ug)].pub struct 
0240: 54 65 6c 65 67 72 61 6d 54 72 61 6e 73 70 6f 72  TelegramTranspor
0250: 74 20 7b 0a 09 74 67 3a 20 43 6c 69 65 6e 74 2c  t {..tg: Client,
0260: 0a 09 72 65 63 69 70 69 65 6e 74 73 3a 20 48 61  ..recipients: Ha
0270: 73 68 4d 61 70 3c 53 74 72 69 6e 67 2c 20 43 68  shMap<String, Ch
0280: 61 74 50 65 65 72 49 64 3e 2c 0a 09 70 75 62 20  atPeerId>,..pub 
0290: 64 65 66 61 75 6c 74 3a 20 43 68 61 74 50 65 65  default: ChatPee
02a0: 72 49 64 2c 0a 7d 0a 0a 69 6d 70 6c 20 54 65 6c  rId,.}..impl Tel
02b0: 65 67 72 61 6d 54 72 61 6e 73 70 6f 72 74 20 7b  egramTransport {
02c0: 0a 0a 09 70 75 62 20 66 6e 20 6e 65 77 20 28 61  ...pub fn new (a
02d0: 70 69 5f 6b 65 79 3a 20 53 74 72 69 6e 67 2c 20  pi_key: String, 
02e0: 72 65 63 69 70 69 65 6e 74 73 3a 20 48 61 73 68  recipients: Hash
02f0: 4d 61 70 3c 53 74 72 69 6e 67 2c 20 69 36 34 3e  Map<String, i64>
0300: 2c 20 64 65 66 61 75 6c 74 3a 20 69 36 34 29 20  , default: i64) 
0310: 2d 3e 20 52 65 73 75 6c 74 3c 54 65 6c 65 67 72  -> Result<Telegr
0320: 61 6d 54 72 61 6e 73 70 6f 72 74 3e 20 7b 0a 09  amTransport> {..
0330: 09 6c 65 74 20 74 67 20 3d 20 43 6c 69 65 6e 74  .let tg = Client
0340: 3a 3a 6e 65 77 28 61 70 69 5f 6b 65 79 29 0a 09  ::new(api_key)..
0350: 09 09 2e 63 6f 6e 74 65 78 74 28 22 46 61 69 6c  ...context("Fail
0360: 65 64 20 74 6f 20 63 72 65 61 74 65 20 41 50 49  ed to create API
0370: 2e 5c 6e 22 29 3f 3b 0a 09 09 6c 65 74 20 72 65  .\n")?;...let re
0380: 63 69 70 69 65 6e 74 73 20 3d 20 72 65 63 69 70  cipients = recip
0390: 69 65 6e 74 73 2e 69 6e 74 6f 5f 69 74 65 72 28  ients.into_iter(
03a0: 29 0a 09 09 09 2e 6d 61 70 28 7c 28 61 2c 20 62  ).....map(|(a, b
03b0: 29 7c 20 28 61 2c 20 43 68 61 74 50 65 65 72 49  )| (a, ChatPeerI
03c0: 64 3a 3a 66 72 6f 6d 28 62 29 29 29 2e 63 6f 6c  d::from(b))).col
03d0: 6c 65 63 74 28 29 3b 0a 09 09 6c 65 74 20 64 65  lect();...let de
03e0: 66 61 75 6c 74 20 3d 20 43 68 61 74 50 65 65 72  fault = ChatPeer
03f0: 49 64 3a 3a 66 72 6f 6d 28 64 65 66 61 75 6c 74  Id::from(default
0400: 29 3b 0a 0a 09 09 4f 6b 28 54 65 6c 65 67 72 61  );....Ok(Telegra
0410: 6d 54 72 61 6e 73 70 6f 72 74 20 7b 0a 09 09 09  mTransport {....
0420: 74 67 2c 0a 09 09 09 72 65 63 69 70 69 65 6e 74  tg,....recipient
0430: 73 2c 0a 09 09 09 64 65 66 61 75 6c 74 2c 0a 09  s,....default,..
0440: 09 7d 29 0a 09 7d 0a 0a 09 2f 2f 2f 20 53 65 6e  .})..}.../// Sen
0450: 64 20 6d 65 73 73 61 67 65 20 74 6f 20 64 65 66  d message to def
0460: 61 75 6c 74 20 75 73 65 72 2c 20 75 73 65 64 20  ault user, used 
0470: 66 6f 72 20 64 65 62 75 67 2f 6c 6f 67 2f 69 6e  for debug/log/in
0480: 66 6f 20 70 75 72 70 6f 73 65 73 0a 09 70 75 62  fo purposes..pub
0490: 20 61 73 79 6e 63 20 66 6e 20 64 65 62 75 67 20   async fn debug 
04a0: 28 26 73 65 6c 66 2c 20 6d 73 67 3a 20 26 73 74  (&self, msg: &st
04b0: 72 29 20 2d 3e 20 52 65 73 75 6c 74 3c 4d 65 73  r) -> Result<Mes
04c0: 73 61 67 65 3e 20 7b 0a 09 09 73 65 6c 66 2e 73  sage> {...self.s
04d0: 65 6e 64 28 26 73 65 6c 66 2e 64 65 66 61 75 6c  end(&self.defaul
04e0: 74 2c 20 65 6e 63 6f 64 65 28 6d 73 67 29 29 2e  t, encode(msg)).
04f0: 61 77 61 69 74 0a 09 7d 0a 0a 09 2f 2f 2f 20 47  await..}.../// G
0500: 65 74 20 72 65 63 69 70 69 65 6e 74 20 62 79 20  et recipient by 
0510: 61 64 64 72 65 73 73 0a 09 70 75 62 20 66 6e 20  address..pub fn 
0520: 67 65 74 20 28 26 73 65 6c 66 2c 20 6e 61 6d 65  get (&self, name
0530: 3a 20 26 73 74 72 29 20 2d 3e 20 52 65 73 75 6c  : &str) -> Resul
0540: 74 3c 26 43 68 61 74 50 65 65 72 49 64 3e 20 7b  t<&ChatPeerId> {
0550: 0a 09 09 73 65 6c 66 2e 72 65 63 69 70 69 65 6e  ...self.recipien
0560: 74 73 2e 67 65 74 28 6e 61 6d 65 29 0a 09 09 09  ts.get(name)....
0570: 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 28 7c 7c  .with_context(||
0580: 20 66 6f 72 6d 61 74 21 28 22 52 65 63 69 70 69   format!("Recipi
0590: 65 6e 74 20 5c 22 7b 6e 61 6d 65 7d 5c 22 20 6e  ent \"{name}\" n
05a0: 6f 74 20 66 6f 75 6e 64 20 69 6e 20 63 6f 6e 66  ot found in conf
05b0: 69 67 75 72 61 74 69 6f 6e 22 29 29 0a 09 7d 0a  iguration"))..}.
05c0: 0a 09 2f 2f 2f 20 53 65 6e 64 20 6d 65 73 73 61  ../// Send messa
05d0: 67 65 20 74 6f 20 73 70 65 63 69 66 69 65 64 20  ge to specified 
05e0: 75 73 65 72 0a 09 70 75 62 20 61 73 79 6e 63 20  user..pub async 
05f0: 66 6e 20 73 65 6e 64 20 3c 53 3e 20 28 26 73 65  fn send <S> (&se
0600: 6c 66 2c 20 74 6f 3a 20 26 43 68 61 74 50 65 65  lf, to: &ChatPee
0610: 72 49 64 2c 20 6d 73 67 3a 20 53 29 20 2d 3e 20  rId, msg: S) -> 
0620: 52 65 73 75 6c 74 3c 4d 65 73 73 61 67 65 3e 0a  Result<Message>.
0630: 09 77 68 65 72 65 20 53 3a 20 49 6e 74 6f 3c 53  .where S: Into<S
0640: 74 72 69 6e 67 3e 20 2b 20 44 65 62 75 67 7b 0a  tring> + Debug{.
0650: 09 09 4f 6b 28 73 65 6c 66 2e 74 67 2e 65 78 65  ..Ok(self.tg.exe
0660: 63 75 74 65 28 0a 09 09 09 53 65 6e 64 4d 65 73  cute(....SendMes
0670: 73 61 67 65 3a 3a 6e 65 77 28 2a 74 6f 2c 20 6d  sage::new(*to, m
0680: 73 67 29 0a 09 09 09 2e 77 69 74 68 5f 70 61 72  sg).....with_par
0690: 73 65 5f 6d 6f 64 65 28 4d 61 72 6b 64 6f 77 6e  se_mode(Markdown
06a0: 56 32 29 0a 09 09 29 2e 61 77 61 69 74 3f 29 0a  V2)...).await?).
06b0: 09 7d 0a 0a 09 2f 2f 2f 20 53 65 6e 64 20 6d 65  .}.../// Send me
06c0: 64 69 61 20 74 6f 20 73 70 65 63 69 66 69 65 64  dia to specified
06d0: 20 75 73 65 72 0a 09 70 75 62 20 61 73 79 6e 63   user..pub async
06e0: 20 66 6e 20 73 65 6e 64 67 72 6f 75 70 20 28 26   fn sendgroup (&
06f0: 73 65 6c 66 2c 20 74 6f 3a 20 26 43 68 61 74 50  self, to: &ChatP
0700: 65 65 72 49 64 2c 20 6d 65 64 69 61 3a 20 56 65  eerId, media: Ve
0710: 63 3c 41 74 74 61 63 68 6d 65 6e 74 3e 2c 20 6d  c<Attachment>, m
0720: 73 67 3a 20 26 73 74 72 29 20 2d 3e 20 52 65 73  sg: &str) -> Res
0730: 75 6c 74 3c 28 29 3e 20 7b 0a 09 09 69 66 20 6d  ult<()> {...if m
0740: 65 64 69 61 2e 6c 65 6e 28 29 20 3e 20 31 20 7b  edia.len() > 1 {
0750: 0a 09 09 09 6c 65 74 20 6d 75 74 20 61 74 74 61  ....let mut atta
0760: 63 68 20 3d 20 76 65 63 21 5b 5d 3b 0a 09 09 09  ch = vec![];....
0770: 6c 65 74 20 6d 75 74 20 70 6f 73 20 3d 20 6d 65  let mut pos = me
0780: 64 69 61 2e 6c 65 6e 28 29 3b 0a 09 09 09 66 6f  dia.len();....fo
0790: 72 20 66 69 6c 65 20 69 6e 20 6d 65 64 69 61 20  r file in media 
07a0: 7b 0a 09 09 09 09 6c 65 74 20 6d 75 74 20 63 61  {.....let mut ca
07b0: 70 74 69 6f 6e 20 3d 20 49 6e 70 75 74 4d 65 64  ption = InputMed
07c0: 69 61 44 6f 63 75 6d 65 6e 74 3a 3a 64 65 66 61  iaDocument::defa
07d0: 75 6c 74 28 29 3b 0a 09 09 09 09 69 66 20 70 6f  ult();.....if po
07e0: 73 20 3d 3d 20 31 20 7b 0a 09 09 09 09 09 63 61  s == 1 {......ca
07f0: 70 74 69 6f 6e 20 3d 20 63 61 70 74 69 6f 6e 2e  ption = caption.
0800: 77 69 74 68 5f 63 61 70 74 69 6f 6e 28 6d 73 67  with_caption(msg
0810: 29 0a 09 09 09 09 09 09 2e 77 69 74 68 5f 63 61  )........with_ca
0820: 70 74 69 6f 6e 5f 70 61 72 73 65 5f 6d 6f 64 65  ption_parse_mode
0830: 28 4d 61 72 6b 64 6f 77 6e 56 32 29 3b 0a 09 09  (MarkdownV2);...
0840: 09 09 7d 0a 09 09 09 09 70 6f 73 20 2d 3d 20 31  ..}.....pos -= 1
0850: 3b 0a 09 09 09 09 61 74 74 61 63 68 2e 70 75 73  ;.....attach.pus
0860: 68 28 0a 09 09 09 09 09 4d 65 64 69 61 47 72 6f  h(......MediaGro
0870: 75 70 49 74 65 6d 3a 3a 66 6f 72 5f 64 6f 63 75  upItem::for_docu
0880: 6d 65 6e 74 28 0a 09 09 09 09 09 09 49 6e 70 75  ment(.......Inpu
0890: 74 46 69 6c 65 3a 3a 66 72 6f 6d 28 0a 09 09 09  tFile::from(....
08a0: 09 09 09 09 49 6e 70 75 74 46 69 6c 65 52 65 61  ....InputFileRea
08b0: 64 65 72 3a 3a 66 72 6f 6d 28 66 69 6c 65 2e 64  der::from(file.d
08c0: 61 74 61 29 0a 09 09 09 09 09 09 09 09 2e 77 69  ata)..........wi
08d0: 74 68 5f 66 69 6c 65 5f 6e 61 6d 65 28 66 69 6c  th_file_name(fil
08e0: 65 2e 6e 61 6d 65 29 0a 09 09 09 09 09 09 29 2c  e.name).......),
08f0: 0a 09 09 09 09 09 09 63 61 70 74 69 6f 6e 0a 09  .......caption..
0900: 09 09 09 09 29 0a 09 09 09 09 29 3b 0a 09 09 09  ....).....);....
0910: 7d 0a 09 09 09 73 65 6c 66 2e 74 67 2e 65 78 65  }....self.tg.exe
0920: 63 75 74 65 28 53 65 6e 64 4d 65 64 69 61 47 72  cute(SendMediaGr
0930: 6f 75 70 3a 3a 6e 65 77 28 2a 74 6f 2c 20 4d 65  oup::new(*to, Me
0940: 64 69 61 47 72 6f 75 70 3a 3a 6e 65 77 28 61 74  diaGroup::new(at
0950: 74 61 63 68 29 3f 29 29 2e 61 77 61 69 74 3f 3b  tach)?)).await?;
0960: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 73  ...} else {....s
0970: 65 6c 66 2e 74 67 2e 65 78 65 63 75 74 65 28 0a  elf.tg.execute(.
0980: 09 09 09 09 53 65 6e 64 44 6f 63 75 6d 65 6e 74  ....SendDocument
0990: 3a 3a 6e 65 77 28 0a 09 09 09 09 09 2a 74 6f 2c  ::new(......*to,
09a0: 0a 09 09 09 09 09 49 6e 70 75 74 46 69 6c 65 52  ......InputFileR
09b0: 65 61 64 65 72 3a 3a 66 72 6f 6d 28 6d 65 64 69  eader::from(medi
09c0: 61 5b 30 5d 2e 64 61 74 61 2e 63 6c 6f 6e 65 28  a[0].data.clone(
09d0: 29 29 0a 09 09 09 09 09 2e 77 69 74 68 5f 66 69  )).......with_fi
09e0: 6c 65 5f 6e 61 6d 65 28 6d 65 64 69 61 5b 30 5d  le_name(media[0]
09f0: 2e 6e 61 6d 65 2e 63 6c 6f 6e 65 28 29 29 0a 09  .name.clone())..
0a00: 09 09 09 29 2e 77 69 74 68 5f 63 61 70 74 69 6f  ...).with_captio
0a10: 6e 28 6d 73 67 29 0a 09 09 09 09 2e 77 69 74 68  n(msg)......with
0a20: 5f 63 61 70 74 69 6f 6e 5f 70 61 72 73 65 5f 6d  _caption_parse_m
0a30: 6f 64 65 28 4d 61 72 6b 64 6f 77 6e 56 32 29 0a  ode(MarkdownV2).
0a40: 09 09 09 29 2e 61 77 61 69 74 3f 3b 0a 09 09 7d  ...).await?;...}
0a50: 0a 09 09 4f 6b 28 28 29 29 0a 09 7d 0a 7d 0a     ...Ok(())..}.}.