maxbot 0.7.2

Автоматизация работы с чат-ботами на платформе MAX (max.ru)
Documentation
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 {
    /// Получение обновлений (long polling)
    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))
    }

    /// Ответ на callback-запрос (нажатие inline-кнопки)
    ///
    /// # Arguments
    /// * `callback_id` – идентификатор callback из Update.callback.callback_id
    /// * `message` – новое сообщение для замены текущего (опционально)
    /// * `notification` – текст уведомления, которое увидит пользователь (опционально)
    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))
    }
}