use reqwest::blocking::{multipart, Client};
use serde_json::Value;
pub struct FileMessage<'a> {
pub file_path: &'a str,
pub to: &'a str,
}
pub fn send(message: FileMessage, base_url: &String) -> Result<(), &'static str> {
let reqwest = Client::new();
let body = multipart::Form::new()
.text("chat_id", String::from(message.to))
.file("document", message.file_path);
let body = match body {
Ok(body) => body,
Err(_) => return Err("Failed to load the file"),
};
match reqwest
.post(format!("{}/sendDocument", base_url))
.multipart(body)
.send()
{
Ok(result) => {
if result.status() == 401 {
return Err("Invalid token");
}
let value: Value = result.json().unwrap();
if value["description"].to_string().contains("chat not found") {
return Err("No chat found with the given ID");
}
}
Err(_) => return Err("Failed to send the request to telegram."),
};
Ok(())
}