use crate::{
api::telegram::Operation,
network::{HttpMethod, NetworkTarget, NetworkTask},
system::SystemInfo,
};
use super::{PhotoMessage, TextMessage};
const TELEGRAM_API_BASE: &str = "https://api.telegram.org/bot";
#[derive(Debug, Clone)]
pub struct API {
token: String,
chat_id: String,
operation: Operation,
}
impl API {
pub fn text(
token: impl Into<String>,
chat_id: impl Into<String>,
params: TextMessage,
) -> Self {
API {
token: token.into(),
chat_id: chat_id.into(),
operation: Operation::SendMessage(params),
}
}
pub fn photo(
token: impl Into<String>,
chat_id: impl Into<String>,
params: PhotoMessage,
) -> Self {
API {
token: token.into(),
chat_id: chat_id.into(),
operation: Operation::SendPhoto(params),
}
}
fn get_chat_id(&self) -> String {
self.chat_id.clone()
}
}
impl NetworkTarget for API {
fn base_url(&self) -> String {
format!("{}{}", TELEGRAM_API_BASE, self.token)
}
fn path(&self) -> String {
match &self.operation {
Operation::SendMessage(_) => "sendMessage".to_string(),
Operation::SendPhoto(_) => "sendPhoto".to_string(),
}
}
fn method(&self) -> HttpMethod {
HttpMethod::Post
}
fn task(&self) -> NetworkTask {
match &self.operation {
Operation::SendMessage(params) => {
params.clone().into_task(self.get_chat_id())
}
Operation::SendPhoto(params) => {
params.clone().into_task(self.get_chat_id())
}
}
}
fn headers(&self) -> Vec<(String, String)> {
let sys_info = SystemInfo::new();
vec![
("Content-Type".into(), "application/json".into()),
("Accept".into(), "application/json".into()),
("user-agent".into(), sys_info.get_user_agent()),
]
}
}