use std::collections::HashMap;
use reqwest::blocking::Client;
use serde_json::Value;
pub struct TextMessage<'a> {
pub content: &'a str,
pub to: &'a str,
}
pub fn send(message: TextMessage, base_url: &String) -> Result<(), &'static str> {
let reqwest = Client::new();
let mut body = HashMap::new();
body.insert("chat_id", &message.to);
body.insert("text", &message.content);
match reqwest
.post(format!("{}/sendMessage", base_url))
.json(&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(())
}