73 releases (38 breaking)
new 0.39.0 | Feb 4, 2025 |
---|---|
0.37.0 | Dec 7, 2024 |
0.35.0 | Nov 27, 2024 |
0.32.1 | Jul 16, 2024 |
0.1.0 | Mar 13, 2021 |
#15 in HTTP client
1,395 downloads per month
Used in 3 crates
300KB
6K
SLoC
Frankenstein
Telegram bot API client for Rust.
It's a complete wrapper for Telegram bot API, and it's up-to-date with version 8.2 of the API.
Frankenstein's data structures (rust structs and enums) are mapped one-to-one from Telegram bot API objects and method parameters.
Installation
Run cargo add frankenstein
or add the following to your Cargo.toml
.
[dependencies]
frankenstein = { version = "0.39", features = [] }
You likely want to use either a blocking or an async client. Enable it via the Features.
Features
Without enabling any additional features this crate will only ship with Telegram types.
- blocking (synchronous)
client-ureq
- a blocking HTTP API client based onureq
trait-sync
- a blocking API trait, it's included in theclient-ureq
feature. It may be useful for people who want to create a custom blocking client (for example, replacing an HTTP client)
- async
client-reqwest
- an async HTTP API client based onreqwest
. This client partially supports wasm32, but file uploads are currently not supported there.trait-async
- an async API trait, it's used in theclient-reqwest
. It may be useful for people who want to create a custom async client
For example for the async client add the following line to your Cargo.toml
file:
frankenstein = { version = "0.39", features = ["client-reqwest"] }
Usage
Examples in this section use the blocking client (frankenstein::Api
), but async examples would look the same (just replace frankenstein::Api
with frankenstein::AsyncApi
)
Data structures
All objects described in the API docs have direct counterparts in the Frankenstein. For example, in the docs there is the user type:
id Integer Unique identifier for this user or bot. This number may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. But it has at most 52 significant bits, so a 64-bit integer or double-precision float type are safe for storing this identifier.
is_bot Boolean True, if this user is a bot
first_name String User's or bot's first name
last_name String Optional. User's or bot's last name
username String Optional. User's or bot's username
language_code String Optional. IETF language tag of the user's language
can_join_groups Boolean Optional. True, if the bot can be invited to groups. Returned only in getMe.
can_read_all_group_messages Boolean Optional. True, if privacy mode is disabled for the bot. Returned only in getMe.
supports_inline_queries Boolean Optional. True, if the bot supports inline queries. Returned only in getMe.
In Frankenstein, it's described like this:
pub struct User {
pub id: u64,
pub is_bot: bool,
pub first_name: String,
pub last_name: Option<String>,
pub username: Option<String>,
pub language_code: Option<String>,
pub can_join_groups: Option<bool>,
pub can_read_all_group_messages: Option<bool>,
pub supports_inline_queries: Option<bool>,
}
Optional fields are described as Option
.
Every struct can be created with the associated builder. Only required fields are required to set, optional fields are set to None
when not provided:
use frankenstein::api_params::SendMessageParams;
let send_message_params = SendMessageParams::builder()
.chat_id(1337)
.text("hello")
.build();
Making requests
#![cfg(feature = "client-ureq")]
use frankenstein::TelegramApi;
use frankenstein::api_params::{GetUpdatesParams, SendMessageParams};
use frankenstein::client_ureq::Bot;
use frankenstein::objects::AllowedUpdate;
let token = "123:ABC";
let bot = Bot::new(token);
// Send a message
let send_message_params = SendMessageParams::builder()
.chat_id(1337)
.text("hello")
.build();
let result = bot.send_message(&send_message_params);
// or get the updates (= interactions with the bot)
let update_params = GetUpdatesParams::builder()
.allowed_updates(vec![AllowedUpdate::Message])
.build();
let result = bot.get_updates(&update_params);
Every function returns a Result
with a successful response or failed response.
See more examples in the examples
directory.
Uploading files
Some methods in the API allow uploading files. In the Frankenstein for this FileUpload
enum is used:
pub enum FileUpload {
InputFile(InputFile),
String(String),
}
pub struct InputFile {
path: std::path::PathBuf
}
It has two variants:
FileUpload::String
is used to pass the ID of the already uploaded fileFileUpload::InputFile
is used to upload a new file using multipart upload.
Documentation
Frankenstein implements all Telegram bot API methods. To see which parameters you should pass, check the official Telegram Bot API documentation or docs.rs/frankenstein
You can check out real-world bots created using this library:
- El Monitorro - RSS/Atom/JSON feed reader.
- subvt-telegram-bot - A Telegram bot for the validators of the Polkadot and Kusama.
- wdr-maus-downloader - checks for a new episode of the WDR Maus and downloads it.
- weather_bot_rust - A Telegram bot that provides weather info around the world.
Contributing
- Fork it!
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Author
Ayrat Badykov (@ayrat555)
Dependencies
~2–14MB
~202K SLoC