pub struct Bot { /* private fields */ }Expand description
Bot class with attributes
connection_pool:ConnectionPool- Pool of HTTP connections for API requeststoken:String- Bot API tokenbase_api_url:reqwest::Url- Base API URLbase_api_path:String- Base API pathevent_id:std::sync::Arc<_>- Last event ID
Implementations§
Source§impl Bot
Listen for events and execute callback function
impl Bot
Listen for events and execute callback function
§Parameters
func- callback function withResulttypeResponseEventsGetas argument
Sourcepub async fn event_listener<F, X>(&self, func: F) -> Result<()>
pub async fn event_listener<F, X>(&self, func: F) -> Result<()>
Listen for events and execute callback function
§Parameters
func- callback function withResulttype andResponseEventsGetargument
§Errors
BotError::Api- API error when getting eventsBotError::Network- network error when getting eventsBotError::Serialization- response deserialization errorBotError::System- error when executing callback function
Sourcepub async fn event_listener_parallel<F, X>(&self, func: F) -> Result<()>
pub async fn event_listener_parallel<F, X>(&self, func: F) -> Result<()>
Listen for events with parallel processing Enhanced version that processes events in parallel batches
Source§impl Bot
impl Bot
Sourcepub fn new(version: APIVersionUrl) -> Self
pub fn new(version: APIVersionUrl) -> Self
Creates a new Bot with API version APIVersionUrl
Uses ConnectionPool with optimized settings for HTTP requests.
Get token from variable VKTEAMS_BOT_API_TOKEN in .env file
Get base url from variable VKTEAMS_BOT_API_URL in .env file
Set default path depending on API version
§Errors
BotError::Config- configuration error (invalid token or URL)BotError::Url- URL parsing errorBotError::Network- network client creation error
§Panics
- Unable to find token in .env file
- Unable to find or parse url in .env file
- Unable to create connection pool
Sourcepub fn with_params(
version: &APIVersionUrl,
token: &str,
api_url: &str,
) -> Result<Self>
pub fn with_params( version: &APIVersionUrl, token: &str, api_url: &str, ) -> Result<Self>
Creates a new Bot with direct parameters instead of environment variablesx
This method allows you to create a bot by directly providing the token and API URL, instead of reading them from environment variables. This is particularly useful when:
- You want to manage credentials programmatically
- You’re integrating with a system that doesn’t use environment variables
- You’re testing with different credentials
Uses ConnectionPool with optimized settings for HTTP requests.
§Parameters
version:APIVersionUrl- API versiontoken:String- Bot API tokenapi_url:String- Base API URL
§Errors
BotError::Url- URL parsing errorBotError::Network- network client creation error
§Example
use vkteams_bot::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let bot = Bot::with_params(
&APIVersionUrl::V1,
"your_bot_token",
"https://api.example.com"
)?;
// Now use the bot...
Ok(())
}For most cases, consider using with_default_version
which uses V1 API version and has a simpler signature.
Sourcepub fn get_last_event_id(&self) -> EventId
pub fn get_last_event_id(&self) -> EventId
Get last event id (lock-free)
Sourcepub fn set_last_event_id(&self, id: EventId)
pub fn set_last_event_id(&self, id: EventId)
Sourcepub fn set_path(&self, path: &str) -> String
pub fn set_path(&self, path: &str) -> String
Append method path to base_api_path
path:String- append path tobase_api_path
Sourcepub fn get_parsed_url(&self, path: String, query: String) -> Result<Url>
pub fn get_parsed_url(&self, path: String, query: String) -> Result<Url>
Build full URL with optional query parameters
§Errors
BotError::Url- URL parsing error
Parse with Url::parse
Sourcepub async fn send_api_request<Rq>(
&self,
message: Rq,
) -> Result<Rq::ResponseType>
pub async fn send_api_request<Rq>( &self, message: Rq, ) -> Result<Rq::ResponseType>
Send request, get response
Serialize request generic type Rq with serde_url_params::to_string into query string
Get response body using connection pool
Deserialize response with serde_json::from_str
message: generic typeRq- request type
§Errors
BotError::UrlParams- URL parameters serialization errorBotError::Url- URL parsing errorBotError::Network- network error when sending requestBotError::Serialization- response deserialization errorBotError::Io- file operation errorBotError::Api- API error when processing request
§Panics
- Unable to deserialize response
Source§impl Bot
impl Bot
Sourcepub fn with_default_version(token: &str, api_url: &str) -> Result<Self>
pub fn with_default_version(token: &str, api_url: &str) -> Result<Self>
Creates a new bot with default API version (V1) and direct parameters
This is the recommended method for creating a bot with direct parameters. It uses the default connection pool with optimized settings.
§Parameters
§Errors
BotError::Url- URL parsing errorBotError::Network- network client creation error
§Example
use vkteams_bot::{Bot, error::Result};
#[tokio::main]
async fn main() -> Result<()> {
let bot = Bot::with_default_version(
"your_bot_token",
"https://api.example.com"
)?;
// Now use the bot...
Ok(())
}