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