1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::time::Duration;
/// Base error type for all Telegram Bot API errors.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TelegramError {
/// The bot doesn't have enough rights to perform the requested action.
#[error("{0}")]
Forbidden(String),
/// The provided token is invalid.
#[error("{0}")]
InvalidToken(String),
/// The requested API endpoint was not found.
#[error("{0}")]
EndPointNotFound(String),
/// A network-level error occurred.
#[error("{0}")]
Network(String),
/// Telegram returned a 400 Bad Request response.
#[error("{0}")]
BadRequest(String),
/// The request timed out.
#[error("{0}")]
TimedOut(String),
/// The chat was migrated to a supergroup with a new ID.
#[error("Group migrated to supergroup. New chat id: {new_chat_id}")]
ChatMigrated {
/// The new chat ID of the supergroup.
new_chat_id: i64,
},
/// Flood control -- must wait before retrying.
#[error("Flood control exceeded. Retry in {retry_after:?}")]
RetryAfter {
/// Duration to wait before retrying the request.
retry_after: Duration,
},
/// A long-poll or webhook conflicts with another one.
#[error("{0}")]
Conflict(String),
/// Passport decryption failed.
#[error("PassportDecryptionError: {0}")]
PassportDecryption(String),
/// HTTP-level error from reqwest.
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
/// JSON serialization/deserialization error.
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
/// I/O error.
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
impl TelegramError {
/// Cleans the error prefix from API error messages, matching python-telegram-bot behavior.
pub fn from_api_message(message: &str) -> String {
let msg = message
.strip_prefix("Error: ")
.or_else(|| message.strip_prefix("[Error]: "))
.or_else(|| message.strip_prefix("Bad Request: "));
match msg {
Some(stripped) => {
let mut chars = stripped.chars();
match chars.next() {
Some(c) => c.to_uppercase().to_string() + chars.as_str(),
None => String::new(),
}
}
None => message.to_string(),
}
}
}
/// A type alias for `Result<T, TelegramError>`.
pub type Result<T> = std::result::Result<T, TelegramError>;