use std::fmt;
use std::net::IpAddr;
use std::time::Duration;
use reqwest::Url;
use serde_json::Value;
use super::TelegramError;
const MAX_RATE_LIMIT_WAIT: u64 = 30;
const RATE_LIMIT_ATTEMPTS: u32 = 3;
pub(crate) struct BotApi {
client: reqwest::Client,
endpoint: String,
files: String,
token: String,
}
impl fmt::Debug for BotApi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BotApi").finish_non_exhaustive()
}
}
impl BotApi {
pub(crate) fn new(base: &str, token: &str) -> Result<Self, String> {
let well_formed = token.contains(':')
&& token.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, ':' | '_' | '-'));
if !well_formed {
return Err(
"the Telegram bot token is empty or malformed: copy it from @BotFather, as \
`123456789:AA…`"
.into(),
);
}
let base = base.trim_end_matches('/');
let url = Url::parse(base)
.map_err(|error| format!("the Telegram API address `{base}` is not a URL: {error}"))?;
match url.scheme() {
"https" => {}
"http" if is_loopback(&url) => {}
"http" => {
return Err(format!(
"the Telegram API address `{base}` must use https: the bot token travels in \
the request path (plain http is accepted on loopback only)"
));
}
other => {
return Err(format!(
"the Telegram API address `{base}` uses `{other}`: use https://api.telegram.org"
));
}
}
let client = reqwest::Client::builder().build().map_err(|error| {
format!("the Telegram HTTP client could not be built: {}", error.without_url())
})?;
Ok(Self {
client,
endpoint: format!("{base}/bot{token}/"),
files: format!("{base}/file/bot{token}/"),
token: token.to_string(),
})
}
pub(crate) async fn call(
&self,
method: &str,
body: &Value,
timeout: Duration,
) -> Result<Value, TelegramError> {
let mut attempt = 1;
loop {
match self.call_once(method, body, timeout).await {
Err(TelegramError::RateLimited { retry_after, .. })
if attempt < RATE_LIMIT_ATTEMPTS && retry_after <= MAX_RATE_LIMIT_WAIT =>
{
tracing::debug!(
telegram.method = method,
retry.after_secs = retry_after,
"telegram rate limit, waiting"
);
tokio::time::sleep(Duration::from_secs(retry_after.max(1))).await;
attempt += 1;
}
other => return other,
}
}
}
pub(crate) async fn call_once(
&self,
method: &str,
body: &Value,
timeout: Duration,
) -> Result<Value, TelegramError> {
let unreachable = |error: reqwest::Error| TelegramError::Unreachable {
method: method.to_string(),
reason: self.redact(&error.without_url().to_string()),
};
let response = self
.client
.post(format!("{}{method}", self.endpoint))
.json(body)
.timeout(timeout)
.send()
.await
.map_err(unreachable)?;
let status = response.status();
let text = response.text().await.map_err(unreachable)?;
self.reply(method, status, &text)
}
pub(crate) async fn send_photo(
&self,
chat_id: &Value,
image: &super::wire::ChatImage,
timeout: Duration,
) -> Result<Value, TelegramError> {
let method = "sendPhoto";
let unreachable = |error: reqwest::Error| TelegramError::Unreachable {
method: method.to_string(),
reason: self.redact(&error.without_url().to_string()),
};
let chat = match chat_id {
Value::String(name) => name.clone(),
other => other.to_string(),
};
let photo = reqwest::multipart::Part::bytes(image.bytes.clone())
.file_name("image.png")
.mime_str(&image.mime_type)
.map_err(unreachable)?;
let form = reqwest::multipart::Form::new()
.text("chat_id", chat)
.text("caption", image.caption.clone())
.part("photo", photo);
let response = self
.client
.post(format!("{}{method}", self.endpoint))
.multipart(form)
.timeout(timeout)
.send()
.await
.map_err(unreachable)?;
let status = response.status();
let text = response.text().await.map_err(unreachable)?;
self.reply(method, status, &text)
}
fn reply(
&self,
method: &str,
status: reqwest::StatusCode,
text: &str,
) -> Result<Value, TelegramError> {
let reply: Value =
serde_json::from_str(text).map_err(|error| TelegramError::InvalidResponse {
method: method.to_string(),
reason: format!("HTTP {status}, {error}"),
})?;
if reply.get("ok").and_then(Value::as_bool) == Some(true) {
return Ok(reply.get("result").cloned().unwrap_or(Value::Null));
}
let code = reply
.get("error_code")
.and_then(Value::as_i64)
.unwrap_or_else(|| i64::from(status.as_u16()));
let description = self
.redact(reply.get("description").and_then(Value::as_str).unwrap_or("no description"));
tracing::debug!(telegram.method = method, telegram.code = code, "telegram refused a call");
Err(match code {
401 | 404 => TelegramError::Unauthorized,
409 => TelegramError::Conflict,
429 => TelegramError::RateLimited {
method: method.to_string(),
retry_after: reply
.pointer("/parameters/retry_after")
.and_then(Value::as_u64)
.unwrap_or(1),
},
_ => TelegramError::Refused { method: method.to_string(), code, description },
})
}
pub(crate) async fn download(
&self,
file_id: &str,
) -> Option<adk_kernel::channels::ChannelAttachment> {
let body = serde_json::json!({ "file_id": file_id });
let file = self.call("getFile", &body, Duration::from_secs(30)).await.ok()?;
let path = file.get("file_path")?.as_str()?;
if path.contains("..") {
return None;
}
let declared = match path.rsplit('.').next()?.to_ascii_lowercase().as_str() {
"jpg" | "jpeg" => "image/jpeg",
"png" => "image/png",
"webp" => "image/webp",
"gif" => "image/gif",
_ => return None,
};
let request = self.client.get(format!("{}{path}", self.files));
let (media_type, bytes) = crate::channels::media::download(request, Some(declared)).await?;
let name = crate::channels::media::name(file_id, &media_type);
Some(adk_kernel::channels::ChannelAttachment::new(media_type, name, bytes))
}
fn redact(&self, text: &str) -> String {
text.replace(&self.token, "[token]")
}
}
fn is_loopback(url: &Url) -> bool {
let host = url.host_str().unwrap_or_default();
let host = host.trim_start_matches('[').trim_end_matches(']');
host == "localhost"
|| host.ends_with(".localhost")
|| host.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback())
}
#[cfg(test)]
mod tests {
use super::*;
const TOKEN: &str = "123456789:AAH-secret_part";
#[test]
fn the_token_and_the_address_are_checked() {
assert!(BotApi::new("https://api.telegram.org", TOKEN).is_ok());
assert!(BotApi::new("http://127.0.0.1:8081/", TOKEN).is_ok());
assert!(BotApi::new("http://[::1]:8081", TOKEN).is_ok());
assert!(BotApi::new("http://localhost:8081", TOKEN).is_ok());
for base in ["http://api.telegram.org", "ftp://api.telegram.org", "not a url"] {
let error = BotApi::new(base, TOKEN).unwrap_err();
assert!(!error.contains(TOKEN), "{error}");
}
for token in ["", "no-colon", "123:abc/../../x", "123:abc?x=1", "123:abc def"] {
assert!(BotApi::new("https://api.telegram.org", token).is_err(), "{token:?}");
}
}
#[tokio::test]
async fn an_unreachable_api_never_shows_the_token() {
let api = BotApi::new("http://127.0.0.1:9", TOKEN).unwrap();
let error =
api.call("getMe", &serde_json::json!({}), Duration::from_secs(2)).await.unwrap_err();
assert!(matches!(error, TelegramError::Unreachable { .. }), "{error:?}");
assert!(!error.to_string().contains("secret_part"), "{error}");
assert!(!format!("{api:?}").contains("secret_part"));
}
}