Send videos when needed for mirroring.

This commit is contained in:
Syfaro 2020-01-15 16:30:04 -06:00
parent fda806764d
commit 1d480dd8c4
2 changed files with 100 additions and 24 deletions

View File

@ -1067,36 +1067,63 @@ impl MessageHandler {
if results.len() == 1 {
let result = results.get(0).unwrap();
let photo = SendPhoto {
chat_id: message.chat_id(),
caption: if let Some(source_link) = &result.source_link {
Some(source_link.to_owned())
} else {
None
},
photo: FileType::URL(result.url.clone()),
reply_to_message_id: Some(message.message_id),
..Default::default()
};
if result.file_type == "mp4" {
let video = SendVideo {
chat_id: message.chat_id(),
caption: if let Some(source_link) = &result.source_link {
Some(source_link.to_owned())
} else {
None
},
video: FileType::URL(result.url.clone()),
reply_to_message_id: Some(message.message_id),
..Default::default()
};
completed.store(true, std::sync::atomic::Ordering::SeqCst);
completed.store(true, std::sync::atomic::Ordering::SeqCst);
if let Err(e) = self.bot.make_request(&photo).await {
log::error!("Unable to make request: {:?}", e);
self.report_error(
&message,
Some(vec![("command", "mirror".to_string())]),
|| capture_fail(&e),
)
.await;
return;
if let Err(e) = self.bot.make_request(&video).await {
log::error!("Unable to make request: {:?}", e);
self.report_error(
&message,
Some(vec![("command", "mirror".to_string())]),
|| capture_fail(&e),
)
.await;
return;
}
} else {
let photo = SendPhoto {
chat_id: message.chat_id(),
caption: if let Some(source_link) = &result.source_link {
Some(source_link.to_owned())
} else {
None
},
photo: FileType::URL(result.url.clone()),
reply_to_message_id: Some(message.message_id),
..Default::default()
};
completed.store(true, std::sync::atomic::Ordering::SeqCst);
if let Err(e) = self.bot.make_request(&photo).await {
log::error!("Unable to make request: {:?}", e);
self.report_error(
&message,
Some(vec![("command", "mirror".to_string())]),
|| capture_fail(&e),
)
.await;
return;
}
}
} else {
for chunk in results.chunks(10) {
let media = chunk
.iter()
.map(|result| {
InputMedia::Photo(InputMediaPhoto {
.map(|result| match result.file_type.as_ref() {
"mp4" => InputMedia::Video(InputMediaVideo {
media: FileType::URL(result.url.to_owned()),
caption: if let Some(source_link) = &result.source_link {
Some(source_link.to_owned())
@ -1104,7 +1131,16 @@ impl MessageHandler {
None
},
..Default::default()
})
}),
_ => InputMedia::Photo(InputMediaPhoto {
media: FileType::URL(result.url.to_owned()),
caption: if let Some(source_link) = &result.source_link {
Some(source_link.to_owned())
} else {
None
},
..Default::default()
}),
})
.collect();

View File

@ -140,6 +140,16 @@ pub struct InputMediaVideo {
pub caption: Option<String>,
}
impl Default for InputMediaVideo {
fn default() -> Self {
Self {
media_type: "video".to_string(),
media: Default::default(),
caption: None,
}
}
}
#[derive(Serialize)]
#[serde(untagged)]
pub enum InputMedia {
@ -436,6 +446,36 @@ impl TelegramRequest for SendPhoto {
}
}
#[derive(Serialize, Debug, Default)]
pub struct SendVideo {
pub chat_id: ChatID,
#[serde(skip_serializing_if = "FileType::needs_upload")]
pub video: FileType,
#[serde(skip_serializing_if = "Option::is_none")]
pub caption: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to_message_id: Option<i32>,
}
impl TelegramRequest for SendVideo {
type Response = Message;
fn endpoint(&self) -> &str {
"sendVideo"
}
fn files(&self) -> Option<Vec<(String, reqwest::multipart::Part)>> {
// Check if the photo needs to be uploaded. If the photo does need to
// be uploaded, we specify the field name and get the file. This unwrap
// is safe because `needs_upload` only returns true when it exists.
if self.video.needs_upload() {
Some(vec![("photo".to_owned(), self.video.file().unwrap())])
} else {
None
}
}
}
/// GetFile retrieves information about a file.
///
/// This will not download the file! It only returns a [File] containing