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