pub struct Bot { /* private fields */ }Expand description
The main entry point for the Max Bot API.
Holds an HTTP client and your bot token. All API methods are async and
return Result<T, MaxError>.
§Example
use maxoxide::Bot;
#[tokio::main]
async fn main() {
let bot = Bot::from_env();
let me = bot.get_me().await.unwrap();
println!("Running as @{}", me.username.unwrap_or_default());
}Implementations§
Source§impl Bot
impl Bot
Sourcepub fn with_client(token: impl Into<String>, client: Client) -> Self
pub fn with_client(token: impl Into<String>, client: Client) -> Self
Create a new bot with a custom HTTP client.
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Create a bot reading the token from the MAX_BOT_TOKEN environment variable.
§Panics
Panics if the environment variable is not set.
Sourcepub async fn send_message_to_chat(
&self,
chat_id: i64,
body: NewMessageBody,
) -> Result<Message>
pub async fn send_message_to_chat( &self, chat_id: i64, body: NewMessageBody, ) -> Result<Message>
POST /messages — Send a message to a chat/dialog by chat_id.
chat_id identifies a concrete dialog, group, or channel.
It is not the same as a user’s global MAX user_id.
Sourcepub async fn send_message_to_chat_with_options(
&self,
chat_id: i64,
body: NewMessageBody,
options: SendMessageOptions,
) -> Result<Message>
pub async fn send_message_to_chat_with_options( &self, chat_id: i64, body: NewMessageBody, options: SendMessageOptions, ) -> Result<Message>
POST /messages — Send a message to a chat/dialog by chat_id with query options.
Sourcepub async fn send_message_to_user(
&self,
user_id: i64,
body: NewMessageBody,
) -> Result<Message>
pub async fn send_message_to_user( &self, user_id: i64, body: NewMessageBody, ) -> Result<Message>
POST /messages — Send a message to a user by global MAX user_id.
Use this when you know the user’s stable MAX identifier, but do not want
to address a specific dialog chat_id.
Sourcepub async fn send_message_to_user_with_options(
&self,
user_id: i64,
body: NewMessageBody,
options: SendMessageOptions,
) -> Result<Message>
pub async fn send_message_to_user_with_options( &self, user_id: i64, body: NewMessageBody, options: SendMessageOptions, ) -> Result<Message>
POST /messages — Send a message to a user by global MAX user_id with query options.
Sourcepub async fn send_text_to_chat(
&self,
chat_id: i64,
text: impl Into<String>,
) -> Result<Message>
pub async fn send_text_to_chat( &self, chat_id: i64, text: impl Into<String>, ) -> Result<Message>
Convenience: send a plain-text message to a chat/dialog by chat_id.
Sourcepub async fn send_text_to_user(
&self,
user_id: i64,
text: impl Into<String>,
) -> Result<Message>
pub async fn send_text_to_user( &self, user_id: i64, text: impl Into<String>, ) -> Result<Message>
Convenience: send a plain-text message to a user by global MAX user_id.
Sourcepub async fn send_markdown_to_chat(
&self,
chat_id: i64,
text: impl Into<String>,
) -> Result<Message>
pub async fn send_markdown_to_chat( &self, chat_id: i64, text: impl Into<String>, ) -> Result<Message>
Convenience: send a Markdown-formatted message to a chat/dialog by chat_id.
Sourcepub async fn send_markdown_to_user(
&self,
user_id: i64,
text: impl Into<String>,
) -> Result<Message>
pub async fn send_markdown_to_user( &self, user_id: i64, text: impl Into<String>, ) -> Result<Message>
Convenience: send a Markdown-formatted message to a user by global MAX user_id.
Sourcepub async fn edit_message(
&self,
message_id: &str,
body: NewMessageBody,
) -> Result<SimpleResult>
pub async fn edit_message( &self, message_id: &str, body: NewMessageBody, ) -> Result<SimpleResult>
PUT /messages — Edit an existing message.
Sourcepub async fn delete_message(&self, message_id: &str) -> Result<SimpleResult>
pub async fn delete_message(&self, message_id: &str) -> Result<SimpleResult>
DELETE /messages — Delete a message.
Sourcepub async fn get_message(&self, message_id: &str) -> Result<Message>
pub async fn get_message(&self, message_id: &str) -> Result<Message>
GET /messages/{messageId} — Get a single message by ID.
Sourcepub async fn get_messages(
&self,
chat_id: i64,
count: Option<u32>,
from: Option<i64>,
to: Option<i64>,
) -> Result<MessageList>
pub async fn get_messages( &self, chat_id: i64, count: Option<u32>, from: Option<i64>, to: Option<i64>, ) -> Result<MessageList>
GET /messages — Get messages from a chat.
Sourcepub async fn get_messages_by_ids(
&self,
message_ids: impl IntoIterator<Item = impl Into<String>>,
count: Option<u32>,
from: Option<i64>,
to: Option<i64>,
) -> Result<MessageList>
pub async fn get_messages_by_ids( &self, message_ids: impl IntoIterator<Item = impl Into<String>>, count: Option<u32>, from: Option<i64>, to: Option<i64>, ) -> Result<MessageList>
GET /messages — Get one or more messages by IDs.
Sourcepub async fn get_video(&self, video_token: &str) -> Result<VideoInfo>
pub async fn get_video(&self, video_token: &str) -> Result<VideoInfo>
GET /videos/{videoToken} — Get video metadata and playback URLs.
Sourcepub async fn answer_callback(
&self,
body: AnswerCallbackBody,
) -> Result<SimpleResult>
pub async fn answer_callback( &self, body: AnswerCallbackBody, ) -> Result<SimpleResult>
POST /answers — Respond to an inline button callback.
Sourcepub async fn get_chats(
&self,
count: Option<u32>,
marker: Option<i64>,
) -> Result<ChatList>
pub async fn get_chats( &self, count: Option<u32>, marker: Option<i64>, ) -> Result<ChatList>
GET /chats — Get all group chats the bot is a member of.
Sourcepub async fn get_chat(&self, chat_id: i64) -> Result<Chat>
pub async fn get_chat(&self, chat_id: i64) -> Result<Chat>
GET /chats/{chatId} — Get info about a specific chat.
Sourcepub async fn edit_chat(&self, chat_id: i64, body: EditChatBody) -> Result<Chat>
pub async fn edit_chat(&self, chat_id: i64, body: EditChatBody) -> Result<Chat>
PATCH /chats/{chatId} — Edit chat title/description.
Sourcepub async fn delete_chat(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn delete_chat(&self, chat_id: i64) -> Result<SimpleResult>
DELETE /chats/{chatId} — Delete a chat.
Sourcepub async fn send_action(
&self,
chat_id: i64,
action: &str,
) -> Result<SimpleResult>
pub async fn send_action( &self, chat_id: i64, action: &str, ) -> Result<SimpleResult>
POST /chats/{chatId}/actions — Send a bot action to a group chat.
The Max API expects values such as "typing_on", "sending_photo",
"sending_video", "sending_audio", "sending_file" or "mark_seen".
Note: live MAX tests currently show successful API responses for
"typing_on", but the client-side typing indicator is not reliably
visible. Treat the visual effect as a current MAX platform gap.
Sourcepub async fn send_sender_action(
&self,
chat_id: i64,
action: SenderAction,
) -> Result<SimpleResult>
pub async fn send_sender_action( &self, chat_id: i64, action: SenderAction, ) -> Result<SimpleResult>
POST /chats/{chatId}/actions — Send a typed bot action to a group chat.
Sourcepub async fn send_typing_on(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn send_typing_on(&self, chat_id: i64) -> Result<SimpleResult>
Convenience: request a typing indicator.
Sourcepub async fn send_sending_image(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn send_sending_image(&self, chat_id: i64) -> Result<SimpleResult>
Convenience: request an image upload indicator.
Sourcepub async fn send_sending_video(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn send_sending_video(&self, chat_id: i64) -> Result<SimpleResult>
Convenience: request a video upload indicator.
Sourcepub async fn send_sending_audio(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn send_sending_audio(&self, chat_id: i64) -> Result<SimpleResult>
Convenience: request an audio upload indicator.
Sourcepub async fn send_sending_file(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn send_sending_file(&self, chat_id: i64) -> Result<SimpleResult>
Convenience: request a file upload indicator.
Sourcepub async fn mark_seen(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn mark_seen(&self, chat_id: i64) -> Result<SimpleResult>
Convenience: mark a group chat as seen.
Sourcepub async fn get_pinned_message(&self, chat_id: i64) -> Result<PinnedMessage>
pub async fn get_pinned_message(&self, chat_id: i64) -> Result<PinnedMessage>
GET /chats/{chatId}/pin — Get the pinned message in a chat.
Sourcepub async fn pin_message(
&self,
chat_id: i64,
body: PinMessageBody,
) -> Result<SimpleResult>
pub async fn pin_message( &self, chat_id: i64, body: PinMessageBody, ) -> Result<SimpleResult>
PUT /chats/{chatId}/pin — Pin a message.
Sourcepub async fn unpin_message(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn unpin_message(&self, chat_id: i64) -> Result<SimpleResult>
DELETE /chats/{chatId}/pin — Unpin the pinned message.
Sourcepub async fn get_members(
&self,
chat_id: i64,
count: Option<u32>,
marker: Option<i64>,
) -> Result<ChatMembersList>
pub async fn get_members( &self, chat_id: i64, count: Option<u32>, marker: Option<i64>, ) -> Result<ChatMembersList>
GET /chats/{chatId}/members — Get members of a chat.
Sourcepub async fn get_members_by_ids(
&self,
chat_id: i64,
user_ids: impl IntoIterator<Item = i64>,
) -> Result<ChatMembersList>
pub async fn get_members_by_ids( &self, chat_id: i64, user_ids: impl IntoIterator<Item = i64>, ) -> Result<ChatMembersList>
GET /chats/{chatId}/members — Get selected chat members by user IDs.
Sourcepub async fn add_members(
&self,
chat_id: i64,
user_ids: Vec<i64>,
) -> Result<SimpleResult>
pub async fn add_members( &self, chat_id: i64, user_ids: Vec<i64>, ) -> Result<SimpleResult>
POST /chats/{chatId}/members — Add members to a chat.
Sourcepub async fn remove_member(
&self,
chat_id: i64,
user_id: i64,
) -> Result<SimpleResult>
pub async fn remove_member( &self, chat_id: i64, user_id: i64, ) -> Result<SimpleResult>
DELETE /chats/{chatId}/members — Remove a member from a chat.
Sourcepub async fn get_admins(&self, chat_id: i64) -> Result<ChatMembersList>
pub async fn get_admins(&self, chat_id: i64) -> Result<ChatMembersList>
GET /chats/{chatId}/members/admins — Get administrators of a chat.
Sourcepub async fn add_admins(
&self,
chat_id: i64,
admins: Vec<ChatAdmin>,
) -> Result<SimpleResult>
pub async fn add_admins( &self, chat_id: i64, admins: Vec<ChatAdmin>, ) -> Result<SimpleResult>
POST /chats/{chatId}/members/admins — Grant administrator rights.
Sourcepub async fn remove_admin(
&self,
chat_id: i64,
user_id: i64,
) -> Result<SimpleResult>
pub async fn remove_admin( &self, chat_id: i64, user_id: i64, ) -> Result<SimpleResult>
DELETE /chats/{chatId}/members/admins/{userId} — Revoke administrator rights.
Sourcepub async fn get_my_membership(&self, chat_id: i64) -> Result<ChatMember>
pub async fn get_my_membership(&self, chat_id: i64) -> Result<ChatMember>
GET /chats/{chatId}/members/me — Get the bot’s membership info in a chat.
Sourcepub async fn leave_chat(&self, chat_id: i64) -> Result<SimpleResult>
pub async fn leave_chat(&self, chat_id: i64) -> Result<SimpleResult>
DELETE /chats/{chatId}/members/me — Leave a chat.
Sourcepub async fn get_subscriptions(&self) -> Result<SubscriptionList>
pub async fn get_subscriptions(&self) -> Result<SubscriptionList>
GET /subscriptions — List current webhook subscriptions.
Sourcepub async fn subscribe(&self, body: SubscribeBody) -> Result<SimpleResult>
pub async fn subscribe(&self, body: SubscribeBody) -> Result<SimpleResult>
POST /subscriptions — Subscribe to updates via webhook.
Sourcepub async fn unsubscribe(&self, url: &str) -> Result<SimpleResult>
pub async fn unsubscribe(&self, url: &str) -> Result<SimpleResult>
DELETE /subscriptions — Unsubscribe from a webhook.
Sourcepub async fn get_updates(
&self,
marker: Option<i64>,
timeout: Option<u32>,
limit: Option<u32>,
) -> Result<UpdatesResponse>
pub async fn get_updates( &self, marker: Option<i64>, timeout: Option<u32>, limit: Option<u32>, ) -> Result<UpdatesResponse>
GET /updates — Poll for new updates once.
marker is the offset from the previous call; pass None for the first call.
timeout is the long-poll wait time in seconds (max 90).
Sourcepub async fn get_updates_raw(
&self,
marker: Option<i64>,
timeout: Option<u32>,
limit: Option<u32>,
) -> Result<RawUpdatesResponse>
pub async fn get_updates_raw( &self, marker: Option<i64>, timeout: Option<u32>, limit: Option<u32>, ) -> Result<RawUpdatesResponse>
GET /updates — Poll for raw JSON updates once.
Sourcepub async fn get_upload_url(
&self,
upload_type: UploadType,
) -> Result<UploadEndpoint>
pub async fn get_upload_url( &self, upload_type: UploadType, ) -> Result<UploadEndpoint>
POST /uploads — Get an upload URL for a given file type.
Sourcepub async fn set_my_commands(
&self,
commands: Vec<BotCommand>,
) -> Result<SimpleResult>
pub async fn set_my_commands( &self, commands: Vec<BotCommand>, ) -> Result<SimpleResult>
Attempt to set the list of commands shown to users.
The public MAX REST docs expose bot commands in GET /me, but do not
currently document a write endpoint for updating that menu.
Live requests to POST /me/commands currently return
404 Path /me/commands is not recognized.
This helper is kept for experimentation and future MAX support.
Source§impl Bot
impl Bot
Sourcepub async fn upload_file(
&self,
upload_type: UploadType,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
) -> Result<String>
pub async fn upload_file( &self, upload_type: UploadType, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, ) -> Result<String>
Full two-step upload:
- Gets the upload URL (and pre-issued token for video/audio) from
POST /uploads. - POSTs the file as
multipart/form-datato that URL.
Returns the attachment token to use in NewAttachment.
MAX can return the token from the upload endpoint response, from the
multipart upload response, or for images as a photos token map. This
method accepts all forms and returns the first usable token. The
higher-level send_image_* helpers preserve the full photos payload.
§Arguments
upload_type— one ofImage,Video,Audio,File.path— path to a local file.filename— the filename to send in the multipart form (datafield).mime— MIME type, e.g."image/jpeg","video/mp4","application/pdf".
Sourcepub async fn upload_bytes(
&self,
upload_type: UploadType,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
) -> Result<String>
pub async fn upload_bytes( &self, upload_type: UploadType, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, ) -> Result<String>
Like upload_file, but accepts raw bytes instead of a file path.
Sourcepub async fn send_image_to_chat(
&self,
chat_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_image_to_chat( &self, chat_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload an image from disk and send it to a chat.
Sourcepub async fn send_video_to_chat(
&self,
chat_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_video_to_chat( &self, chat_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload a video from disk and send it to a chat.
Sourcepub async fn send_audio_to_chat(
&self,
chat_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_audio_to_chat( &self, chat_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload an audio file from disk and send it to a chat.
Sourcepub async fn send_file_to_chat(
&self,
chat_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_file_to_chat( &self, chat_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload a generic file from disk and send it to a chat.
Sourcepub async fn send_image_to_user(
&self,
user_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_image_to_user( &self, user_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload an image from disk and send it to a user.
Sourcepub async fn send_video_to_user(
&self,
user_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_video_to_user( &self, user_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload a video from disk and send it to a user.
Sourcepub async fn send_audio_to_user(
&self,
user_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_audio_to_user( &self, user_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload an audio file from disk and send it to a user.
Sourcepub async fn send_file_to_user(
&self,
user_id: i64,
path: impl AsRef<Path>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_file_to_user( &self, user_id: i64, path: impl AsRef<Path>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload a generic file from disk and send it to a user.
Sourcepub async fn send_image_bytes_to_chat(
&self,
chat_id: i64,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_image_bytes_to_chat( &self, chat_id: i64, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload image bytes and send them to a chat.
Sourcepub async fn send_video_bytes_to_chat(
&self,
chat_id: i64,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_video_bytes_to_chat( &self, chat_id: i64, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload video bytes and send them to a chat.
Sourcepub async fn send_audio_bytes_to_chat(
&self,
chat_id: i64,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_audio_bytes_to_chat( &self, chat_id: i64, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload audio bytes and send them to a chat.
Sourcepub async fn send_file_bytes_to_chat(
&self,
chat_id: i64,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_file_bytes_to_chat( &self, chat_id: i64, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload file bytes and send them to a chat.
Sourcepub async fn send_image_bytes_to_user(
&self,
user_id: i64,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_image_bytes_to_user( &self, user_id: i64, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload image bytes and send them to a user.
Sourcepub async fn send_video_bytes_to_user(
&self,
user_id: i64,
bytes: Vec<u8>,
filename: impl Into<String>,
mime: impl Into<String>,
text: Option<String>,
) -> Result<Message>
pub async fn send_video_bytes_to_user( &self, user_id: i64, bytes: Vec<u8>, filename: impl Into<String>, mime: impl Into<String>, text: Option<String>, ) -> Result<Message>
Upload video bytes and send them to a user.