tg-api 0.4.3

Unofficial Telegram API Library
Documentation
use reqwest::blocking::{multipart, Client};
use serde_json::Value;

pub struct ImageMessage<'a> {
    pub image_path: &'a str,
    pub to: &'a str,
}

pub fn send(message: ImageMessage, base_url: &String) -> Result<(), &'static str> {
    let reqwest = Client::new();

    let body = multipart::Form::new()
        .text("chat_id", String::from(message.to))
        .file("photo", message.image_path);
    let body = match body {
        Ok(body) => body,
        Err(_) => return Err("Failed to load the image"),
    };

    match reqwest
        .post(format!("{}/sendPhoto", base_url))
        .multipart(body)
        .send()
    {
        Ok(result) => {
            if result.status() == 401 || result.status() == 404 {
                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(())
}