use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::file::InputFile;
use crate::{FileMethod, JsonMethod, TelegramMethod};
#[derive(Debug, Deserialize)]
pub struct WebhookInfo {
pub url: String,
pub has_custom_certificate: bool,
pub pending_update_count: u32,
pub ip_address: Option<String>,
pub last_error_date: u64,
pub last_error_message: Option<String>,
pub max_connections: Option<u32>,
pub allowed_updates: Option<Vec<String>>,
}
#[derive(Clone, Serialize)]
pub struct SetWebhook {
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub certificate: Option<InputFile>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ip_address: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_connections: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub allowed_updates: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub drop_pending_updates: Option<bool>,
}
impl SetWebhook {
pub fn new(url: String) -> Self {
Self {
url,
certificate: None,
ip_address: None,
max_connections: None,
allowed_updates: None,
drop_pending_updates: None,
}
}
pub fn remove_previous() -> Self {
Self {
url: "".to_string(),
certificate: None,
ip_address: None,
max_connections: None,
allowed_updates: None,
drop_pending_updates: None,
}
}
pub fn with_certificate(self, cert: InputFile) -> Self {
Self {
certificate: Some(cert),
..self
}
}
pub fn with_ip_address(self, ip_address: impl Into<String>) -> Self {
Self {
ip_address: Some(ip_address.into()),
..self
}
}
pub fn with_max_connections(self, max_connections: u32) -> Self {
Self {
max_connections: Some(max_connections),
..self
}
}
pub fn drop_pending_updates(self) -> Self {
Self {
drop_pending_updates: Some(true),
..self
}
}
}
impl TelegramMethod for SetWebhook {
type Response = bool;
fn name() -> &'static str {
"setWebhook"
}
}
impl FileMethod for SetWebhook {
fn files(&self) -> Option<std::collections::HashMap<&str, &InputFile>> {
self.certificate.as_ref().map(|file| {
let mut map = HashMap::new();
map.insert("certificate", file);
map
})
}
}
#[derive(Clone, Serialize)]
pub struct DeleteWebhook {
pub drop_pending_updates: Option<bool>,
}
impl DeleteWebhook {
pub fn new() -> Self {
Self {
drop_pending_updates: None,
}
}
pub fn drop_pending_updates(self) -> Self {
Self {
drop_pending_updates: Some(true),
..self
}
}
}
impl TelegramMethod for DeleteWebhook {
type Response = bool;
fn name() -> &'static str {
"deleteWebhook"
}
}
impl JsonMethod for DeleteWebhook {}
#[derive(Clone, Serialize)]
pub struct GetWebhookInfo;
impl TelegramMethod for GetWebhookInfo {
type Response = WebhookInfo;
fn name() -> &'static str {
"getWebhookInfo"
}
}
impl JsonMethod for GetWebhookInfo {}