1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::io::Cursor;
use hyper::{body::Buf, client::HttpConnector, Body, Client, Request, Response};
use hyper_multipart_rfc7578::client::multipart::{self, Form};
use hyper_tls::HttpsConnector;
pub use telbot_types as types;
use types::{ApiResponse, FileMethod, JsonMethod, TelegramError, TelegramMethod};
#[derive(Clone)]
pub struct Api {
base_url: String,
client: Client<HttpsConnector<HttpConnector>>,
}
#[derive(Debug)]
pub enum Error {
Telegram(TelegramError),
Hyper(hyper::Error),
Serde(serde_json::Error),
Mime(mime::FromStrError),
}
pub type Result<T> = std::result::Result<T, Error>;
impl From<hyper::Error> for Error {
fn from(e: hyper::Error) -> Self {
Self::Hyper(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Self::Serde(e)
}
}
impl From<mime::FromStrError> for Error {
fn from(e: mime::FromStrError) -> Self {
Self::Mime(e)
}
}
impl Api {
pub fn new(token: impl AsRef<str>) -> Self {
Self {
base_url: format!("https://api.telegram.org/bot{}/", token.as_ref()),
client: Client::builder().build(HttpsConnector::new()),
}
}
pub async fn send_json<Method: JsonMethod>(&self, method: &Method) -> Result<Method::Response> {
let body = serde_json::to_vec(method)?;
let request = Request::builder()
.method(&hyper::Method::POST)
.uri(format!("{}{}", self.base_url, Method::name()))
.header("Content-Type", "application/json")
.body(Body::from(body))
.unwrap();
let response = self.client.request(request).await?;
Self::parse_response::<Method>(response).await
}
pub async fn send_file<Method: FileMethod>(&self, method: &Method) -> Result<Method::Response> {
let url = format!("{}{}", self.base_url, Method::name());
let files = method.files();
let serialized = serde_json::to_value(method).unwrap();
let mut form = Form::default();
for (key, value) in serialized.as_object().unwrap() {
if let Some(file) = files.as_ref().and_then(|map| map.get(key.as_str())) {
form.add_reader_file_with_mime(
key,
Cursor::new(file.data.clone()),
&file.name,
file.mime.parse()?,
);
} else if let Some(value) = value.as_str() {
form.add_text(key, value);
} else {
form.add_text(key, value.to_string());
}
}
let request = Request::builder().method(&hyper::Method::POST).uri(url);
let request = form
.set_body_convert::<hyper::Body, multipart::Body>(request)
.unwrap();
let response = self.client.request(request).await?;
Self::parse_response::<Method>(response).await
}
async fn parse_response<Method: TelegramMethod>(
response: Response<Body>,
) -> Result<Method::Response> {
let body = hyper::body::aggregate(response).await?;
let tg_response: ApiResponse<_> = serde_json::from_reader(body.reader())?;
match tg_response {
ApiResponse::Ok { result } => Ok(result),
ApiResponse::Err(e) => Err(Error::Telegram(e)),
}
}
}