Skip to main content

Bot

Struct Bot 

Source
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

Source

pub fn new(token: impl Into<String>) -> Self

Create a new bot with the given token.

Source

pub fn with_client(token: impl Into<String>, client: Client) -> Self

Create a new bot with a custom HTTP client.

Source

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.

Source

pub fn client(&self) -> &Client

Returns a reference to the raw HTTP client.

Source

pub fn token(&self) -> &str

Returns the bot token.

Source

pub async fn get_me(&self) -> Result<User>

GET /me — Returns info about the current bot.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn edit_message( &self, message_id: &str, body: NewMessageBody, ) -> Result<SimpleResult>

PUT /messages — Edit an existing message.

Source

pub async fn delete_message(&self, message_id: &str) -> Result<SimpleResult>

DELETE /messages — Delete a message.

Source

pub async fn get_message(&self, message_id: &str) -> Result<Message>

GET /messages/{messageId} — Get a single message by ID.

Source

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.

Source

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.

Source

pub async fn get_video(&self, video_token: &str) -> Result<VideoInfo>

GET /videos/{videoToken} — Get video metadata and playback URLs.

Source

pub async fn answer_callback( &self, body: AnswerCallbackBody, ) -> Result<SimpleResult>

POST /answers — Respond to an inline button callback.

Source

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.

Source

pub async fn get_chat(&self, chat_id: i64) -> Result<Chat>

GET /chats/{chatId} — Get info about a specific chat.

Source

pub async fn edit_chat(&self, chat_id: i64, body: EditChatBody) -> Result<Chat>

PATCH /chats/{chatId} — Edit chat title/description.

Source

pub async fn delete_chat(&self, chat_id: i64) -> Result<SimpleResult>

DELETE /chats/{chatId} — Delete a chat.

Source

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.

Source

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.

Source

pub async fn send_typing_on(&self, chat_id: i64) -> Result<SimpleResult>

Convenience: request a typing indicator.

Source

pub async fn send_sending_image(&self, chat_id: i64) -> Result<SimpleResult>

Convenience: request an image upload indicator.

Source

pub async fn send_sending_video(&self, chat_id: i64) -> Result<SimpleResult>

Convenience: request a video upload indicator.

Source

pub async fn send_sending_audio(&self, chat_id: i64) -> Result<SimpleResult>

Convenience: request an audio upload indicator.

Source

pub async fn send_sending_file(&self, chat_id: i64) -> Result<SimpleResult>

Convenience: request a file upload indicator.

Source

pub async fn mark_seen(&self, chat_id: i64) -> Result<SimpleResult>

Convenience: mark a group chat as seen.

Source

pub async fn get_pinned_message(&self, chat_id: i64) -> Result<PinnedMessage>

GET /chats/{chatId}/pin — Get the pinned message in a chat.

Source

pub async fn pin_message( &self, chat_id: i64, body: PinMessageBody, ) -> Result<SimpleResult>

PUT /chats/{chatId}/pin — Pin a message.

Source

pub async fn unpin_message(&self, chat_id: i64) -> Result<SimpleResult>

DELETE /chats/{chatId}/pin — Unpin the pinned message.

Source

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.

Source

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.

Source

pub async fn add_members( &self, chat_id: i64, user_ids: Vec<i64>, ) -> Result<SimpleResult>

POST /chats/{chatId}/members — Add members to a chat.

Source

pub async fn remove_member( &self, chat_id: i64, user_id: i64, ) -> Result<SimpleResult>

DELETE /chats/{chatId}/members — Remove a member from a chat.

Source

pub async fn get_admins(&self, chat_id: i64) -> Result<ChatMembersList>

GET /chats/{chatId}/members/admins — Get administrators of a chat.

Source

pub async fn add_admins( &self, chat_id: i64, admins: Vec<ChatAdmin>, ) -> Result<SimpleResult>

POST /chats/{chatId}/members/admins — Grant administrator rights.

Source

pub async fn remove_admin( &self, chat_id: i64, user_id: i64, ) -> Result<SimpleResult>

DELETE /chats/{chatId}/members/admins/{userId} — Revoke administrator rights.

Source

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.

Source

pub async fn leave_chat(&self, chat_id: i64) -> Result<SimpleResult>

DELETE /chats/{chatId}/members/me — Leave a chat.

Source

pub async fn get_subscriptions(&self) -> Result<SubscriptionList>

GET /subscriptions — List current webhook subscriptions.

Source

pub async fn subscribe(&self, body: SubscribeBody) -> Result<SimpleResult>

POST /subscriptions — Subscribe to updates via webhook.

Source

pub async fn unsubscribe(&self, url: &str) -> Result<SimpleResult>

DELETE /subscriptions — Unsubscribe from a webhook.

Source

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).

Source

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.

Source

pub async fn get_upload_url( &self, upload_type: UploadType, ) -> Result<UploadEndpoint>

POST /uploads — Get an upload URL for a given file type.

Source

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

Source

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:

  1. Gets the upload URL (and pre-issued token for video/audio) from POST /uploads.
  2. POSTs the file as multipart/form-data to 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 of Image, Video, Audio, File.
  • path — path to a local file.
  • filename — the filename to send in the multipart form (data field).
  • mime — MIME type, e.g. "image/jpeg", "video/mp4", "application/pdf".
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn send_audio_bytes_to_user( &self, user_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 user.

Source

pub async fn send_file_bytes_to_user( &self, user_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 user.

Trait Implementations§

Source§

impl Clone for Bot

Source§

fn clone(&self) -> Bot

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bot

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Bot

§

impl !RefUnwindSafe for Bot

§

impl Send for Bot

§

impl Sync for Bot

§

impl Unpin for Bot

§

impl UnsafeUnpin for Bot

§

impl !UnwindSafe for Bot

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more