MOBOT
is a Telegram chat framework written in Rust. It comes with a fully native implementation of the
Telegram Bot API.
FakeBot
), to simplify writing unit tests for your bots.Example Bot that replies with "Hello world!" to every message. Working example in src/bin/hello.rs
.
```rust use mobot::*;
async fn main() { let client = Client::new(std::env::var("TELEGRAM_TOKEN").unwrap().into()); let mut router = Router::new(client);
router.add_chat_route(Route::Default, |_, _: chat::State<()>| async move {
Ok(chat::Action::ReplyText("Hello world!".into()))
});
router.start().await;
} ```
See full API documentation at https://docs.rs/mobot.
See examples in src/bin.
Adding support for additional APIs is straightforward. It involves creating struct
s for the request
and response, and adding a method to API
. For example, to add support for the sendSticker Telegram API:
SendStickerRequest
```rust
pub struct Sticker { /// Unique identifier for this file pub file_id: String,
/// Sticker width
pub width: i64,
/// Sticker height
pub height: i64,
/// True, if the sticker is animated
pub is_animated: bool,
/// Emoji associated with the sticker
pub emoji: Option<String>,
/// Name of the sticker set to which the sticker belongs
pub set_name: Option<String>,
/// File size
pub file_size: Option<i64>,
}
pub struct SendStickerRequest { /// Unique identifier for the target chat or username of the target pub chat_id: i64,
/// Sticker to send. Pass a file_id as String to send a file that
pub sticker: String,
/// Sends the message silently. Users will receive a notification with
#[serde(skip_serializing_if = "Option::is_none")]
pub disable_notification: Option<bool>,
/// If the message is a reply, ID of the original message
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to_message_id: Option<i64>,
} ```
Request
traitrust
impl Request for SendStickerRequest {}
send_sticker
method call to API
```rust
impl API {
pub async fn send_sticker(&self, req: &SendStickerRequest) -> anyhow::Result
tests/
. If necessary update lib/fake.rs
for client testing.src/bin/
.This crate requires OpenSSL and pkg-config
:
sudo apt-get install pkg-config libssl-dev
MIT License Copyright 2023 Mohit Muthanna Cheppudira
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.