Skip to main content

botkit_telegram/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// Telegram Update object received via webhook
4#[derive(Debug, Clone, Deserialize)]
5pub struct Update {
6    pub update_id: i64,
7    #[serde(flatten)]
8    pub kind: UpdateKind,
9}
10
11/// The kind of update received
12#[derive(Debug, Clone, Deserialize)]
13#[serde(rename_all = "snake_case")]
14#[allow(clippy::large_enum_variant)]
15pub enum UpdateKind {
16    Message(Message),
17    EditedMessage(Message),
18    CallbackQuery(CallbackQuery),
19    #[serde(other)]
20    Unknown,
21}
22
23/// Telegram Message object
24#[derive(Debug, Clone, Deserialize)]
25pub struct Message {
26    pub message_id: i64,
27    pub from: Option<User>,
28    pub chat: Chat,
29    pub date: i64,
30    pub text: Option<String>,
31    pub entities: Option<Vec<MessageEntity>>,
32    pub reply_to_message: Option<Box<Message>>,
33}
34
35/// Telegram User object
36#[derive(Debug, Clone, Deserialize)]
37pub struct User {
38    pub id: i64,
39    pub is_bot: bool,
40    pub first_name: String,
41    pub last_name: Option<String>,
42    pub username: Option<String>,
43    pub language_code: Option<String>,
44}
45
46/// Telegram Chat object
47#[derive(Debug, Clone, Deserialize)]
48pub struct Chat {
49    pub id: i64,
50    #[serde(rename = "type")]
51    pub chat_type: ChatType,
52    pub title: Option<String>,
53    pub username: Option<String>,
54    pub first_name: Option<String>,
55    pub last_name: Option<String>,
56}
57
58/// Chat type
59#[derive(Debug, Clone, Deserialize)]
60#[serde(rename_all = "snake_case")]
61pub enum ChatType {
62    Private,
63    Group,
64    Supergroup,
65    Channel,
66}
67
68/// Message entity (commands, mentions, etc.)
69#[derive(Debug, Clone, Deserialize)]
70pub struct MessageEntity {
71    #[serde(rename = "type")]
72    pub entity_type: EntityType,
73    pub offset: i64,
74    pub length: i64,
75}
76
77/// Entity type
78#[derive(Debug, Clone, Deserialize)]
79#[serde(rename_all = "snake_case")]
80pub enum EntityType {
81    Mention,
82    Hashtag,
83    Cashtag,
84    BotCommand,
85    Url,
86    Email,
87    PhoneNumber,
88    Bold,
89    Italic,
90    Underline,
91    Strikethrough,
92    Spoiler,
93    Code,
94    Pre,
95    TextLink,
96    TextMention,
97    CustomEmoji,
98    #[serde(other)]
99    Unknown,
100}
101
102/// Callback query (button press)
103#[derive(Debug, Clone, Deserialize)]
104pub struct CallbackQuery {
105    pub id: String,
106    pub from: User,
107    pub message: Option<Message>,
108    pub inline_message_id: Option<String>,
109    pub chat_instance: String,
110    pub data: Option<String>,
111}
112
113/// Inline keyboard markup
114#[derive(Debug, Clone, Serialize)]
115pub struct InlineKeyboardMarkup {
116    pub inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
117}
118
119/// Inline keyboard button
120#[derive(Debug, Clone, Serialize)]
121pub struct InlineKeyboardButton {
122    pub text: String,
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub url: Option<String>,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub callback_data: Option<String>,
127}
128
129impl InlineKeyboardButton {
130    pub fn callback(text: impl Into<String>, data: impl Into<String>) -> Self {
131        Self {
132            text: text.into(),
133            url: None,
134            callback_data: Some(data.into()),
135        }
136    }
137
138    pub fn url(text: impl Into<String>, url: impl Into<String>) -> Self {
139        Self {
140            text: text.into(),
141            url: Some(url.into()),
142            callback_data: None,
143        }
144    }
145}
146
147/// Reply markup options
148#[derive(Debug, Clone, Serialize)]
149#[serde(untagged)]
150pub enum ReplyMarkup {
151    InlineKeyboard(InlineKeyboardMarkup),
152}
153
154/// Bot command for setMyCommands API
155#[derive(Debug, Clone, Serialize)]
156pub struct BotCommand {
157    /// Command name without the leading slash
158    pub command: String,
159    /// Description shown in the command menu
160    pub description: String,
161}
162
163impl BotCommand {
164    /// Create a new bot command
165    pub fn new(command: impl Into<String>, description: impl Into<String>) -> Self {
166        Self {
167            command: command.into(),
168            description: description.into(),
169        }
170    }
171}