Skip to content

ayrat555/frankenstein

Repository files navigation

frankenstein

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 on ureq
    • trait-sync - a blocking API trait, it's included in the client-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 on reqwest. This client partially supports wasm32, but file uploads are currently not supported there.
    • trait-async - an async API trait, it's used in the client-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 file
  • FileUpload::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:

Contributing

  1. Fork it!
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

Ayrat Badykov (@ayrat555)