use std::time::Duration;
use teloxide::types::ChatId;
fn api_base(api_url: &str) -> &str {
api_url.trim_end_matches('/')
}
async fn post_api(
api_url: &str,
token: &str,
method: &str,
body: serde_json::Value,
) -> Option<serde_json::Value> {
let url = format!("{}/bot{token}/{method}", api_base(api_url));
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.ok()?;
let resp = client.post(url).json(&body).send().await.ok()?;
let parsed: serde_json::Value = resp.json().await.ok()?;
if parsed.get("ok").and_then(serde_json::Value::as_bool) == Some(true) {
parsed.get("result").cloned()
} else {
None
}
}
pub(crate) async fn chat_title(api_url: &str, token: &str, chat_id: ChatId) -> Option<String> {
let result = post_api(
api_url,
token,
"getChat",
serde_json::json!({ "chat_id": chat_id.0 }),
)
.await?;
result
.get("title")
.and_then(serde_json::Value::as_str)
.map(str::to_owned)
.or_else(|| {
result
.get("username")
.and_then(serde_json::Value::as_str)
.map(str::to_owned)
})
}