use crate::types::Update;
use crate::client::MaxClient;
use crate::error::Result;
use serde_json::Value;
pub struct GetUpdatesParams {
pub marker: Option<i64>,
pub limit: Option<u32>,
pub timeout: Option<u32>,
pub types: Vec<String>,
}
impl MaxClient {
pub async fn get_updates(&self, params: GetUpdatesParams) -> Result<(Vec<Update>, Option<i64>)> {
let mut query = Vec::new();
if let Some(m) = params.marker {
query.push(("marker", m.to_string()));
}
if let Some(l) = params.limit {
query.push(("limit", l.to_string()));
}
if let Some(t) = params.timeout {
query.push(("timeout", t.to_string()));
}
if !params.types.is_empty() {
query.push(("types", params.types.join(",")));
}
let resp: Value = self.request(reqwest::Method::GET, "/updates", &query, None).await?;
let updates = serde_json::from_value(resp["updates"].clone())?;
let marker = resp.get("marker").and_then(|v| v.as_i64());
Ok((updates, marker))
}
pub async fn answer_callback_query(
&self,
callback_id: &str,
message: Option<Value>,
notification: Option<&str>,
) -> Result<bool> {
let mut body = serde_json::Map::new();
if let Some(msg) = message {
body.insert("message".into(), msg);
}
if let Some(notif) = notification {
body.insert("notification".into(), notif.into());
}
let resp: Value = self.request(
reqwest::Method::POST,
"/answers",
&[("callback_id", callback_id.to_string())],
Some(body.into()),
).await?;
Ok(resp.get("success").and_then(|v| v.as_bool()).unwrap_or(false))
}
}