Skip to content

Commit

Permalink
[Telegram] Split message sending for more than 10 media
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Nov 14, 2024
1 parent 4ab2c41 commit 975b22e
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/notify/platform/telegram/request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, io::Cursor, ops::Range};
use std::{borrow::Cow, io::Cursor, mem, ops::Range};

use anyhow::anyhow;
use bytes::Bytes;
Expand Down Expand Up @@ -593,11 +593,43 @@ impl<'a> SendMediaGroup<'a> {
}
}

pub async fn send(self) -> anyhow::Result<Response<Vec<ResultMessage>>> {
pub async fn send(mut self) -> anyhow::Result<Response<Vec<ResultMessage>>> {
assert!(!self.medias.is_empty());

let mut media = self
.medias
let mut ret = vec![];

let mut medias = vec![];
mem::swap(&mut self.medias, &mut medias);

let mut iter = medias.chunks(10).peekable();
while let Some(chunk) = iter.next() {
let is_last_chunk = iter.peek().is_none();
let text = is_last_chunk.then(|| self.text.take()).flatten();

let resp = self.send_inner(chunk, text).await?;

// If any chunk fails, return the response immediately
if !resp.ok {
return Ok(resp);
}
ret.append(&mut resp.result.unwrap());
}

Ok(Response {
ok: true,
description: None,
result: Some(ret),
})
}

async fn send_inner(
&self,
medias: impl IntoIterator<Item = &'a Media<'a>>,
text: Option<Text<'a>>,
) -> anyhow::Result<Response<Vec<ResultMessage>>> {
let medias = medias.into_iter().collect_vec();

let mut media = medias
.iter()
.map(|media| match media {
Media::Photo(photo) => {
Expand All @@ -616,7 +648,7 @@ impl<'a> SendMediaGroup<'a> {
}
})
.collect::<Vec<_>>();
if let Some(text) = self.text {
if let Some(text) = text {
let (text, entities) = text.into_json();
let first_media = media.first_mut().unwrap().as_object_mut().unwrap();
first_media.insert("caption".into(), text);
Expand All @@ -638,7 +670,7 @@ impl<'a> SendMediaGroup<'a> {
.as_array_mut()
.unwrap()
.iter_mut()
.zip(self.medias)
.zip(medias)
.enumerate()
.map(|(i, (media, kind))| {
media["media"] = format_attach_url(i).into();
Expand Down

0 comments on commit 975b22e

Please sign in to comment.