1use reqwest::blocking::{multipart, Client};
2use serde_json::Value;
3
4pub struct ImageMessage<'a> {
5 pub image_path: &'a str,
6 pub to: &'a str,
7}
8
9pub fn send(message: ImageMessage, base_url: &String) -> Result<(), &'static str> {
10 let reqwest = Client::new();
11
12 let body = multipart::Form::new()
13 .text("chat_id", String::from(message.to))
14 .file("photo", message.image_path);
15 let body = match body {
16 Ok(body) => body,
17 Err(_) => return Err("Failed to load the image"),
18 };
19
20 match reqwest
21 .post(format!("{}/sendPhoto", base_url))
22 .multipart(body)
23 .send()
24 {
25 Ok(result) => {
26 if result.status() == 401 || result.status() == 404 {
27 return Err("Invalid token");
28 }
29 let value: Value = result.json().unwrap();
30 if value["description"].to_string().contains("chat not found") {
31 return Err("No chat found with the given ID");
32 }
33 }
34 Err(_) => return Err("Failed to send the request to telegram."),
35 };
36 Ok(())
37}