Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support Bot API 7.2 #8

Merged
merged 16 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions examples/axum_and_echo_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = TelersRouter::new("main");
router.message.register(echo_handler);
Expand Down
7 changes: 2 additions & 5 deletions examples/bot_http_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::with_client(bot_token, CustomClient::default());
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::with_client(token, CustomClient::default());

let mut router = Router::new("main");
router.message.register(echo_handler);
Expand Down
11 changes: 11 additions & 0 deletions examples/business_connection/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "business_connection"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
telers = { path = "../../telers", features = ["default"] }
tokio = { version = "1.36", features = ["macros"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
82 changes: 82 additions & 0 deletions examples/business_connection/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! This example shows how to receive updates from business connections.
//!
//! You can run this example by setting `BOT_TOKEN` and optional `RUST_LOG` environment variable and running:
//! ```bash
//! RUST_LOG={log_level} BOT_TOKEN={your_bot_token} cargo run --package business_connection
//! ```

use telers::{
event::{telegram::HandlerResult, EventReturn, ToServiceProvider as _},
methods::SendMessage,
types::{BusinessConnection, BusinessMessagesDeleted, Message},
Bot, Dispatcher, Router,
};
use tracing::{event, Level};
use tracing_subscriber::{fmt, layer::SubscriberExt as _, util::SubscriberInitExt as _, EnvFilter};

async fn connection(business_connection: BusinessConnection) -> HandlerResult {
event!(
Level::DEBUG,
?business_connection,
"Received business connection",
);

Ok(EventReturn::Finish)
}

async fn message(bot: Bot, message: Message) -> HandlerResult {
event!(Level::DEBUG, ?message, "Received message");

bot.send(
SendMessage::new(message.chat().id(), "Hello world!")
.business_connection_id(message.business_connection_id().unwrap()),
)
.await?;

Ok(EventReturn::Finish)
}

async fn message_edited(message: Message) -> HandlerResult {
event!(Level::DEBUG, ?message, "Received edited message");

Ok(EventReturn::Finish)
}

async fn messages_deleted(messages_deleted: BusinessMessagesDeleted) -> HandlerResult {
event!(Level::DEBUG, ?messages_deleted, "Received deleted messages");

Ok(EventReturn::Finish)
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");
router.business_connection.register(connection);
router.business_message.register(message);
router.edited_business_message.register(message_edited);
router.deleted_business_messages.register(messages_deleted);

let dispatcher = Dispatcher::builder()
.allowed_updates(router.resolve_used_update_types())
.main_router(router)
.bot(bot)
.build();

match dispatcher
.to_service_provider_default()
.unwrap()
.run_polling()
.await
{
Ok(()) => event!(Level::INFO, "Bot stopped"),
Err(err) => event!(Level::ERROR, error = %err, "Bot stopped"),
}
}
7 changes: 2 additions & 5 deletions examples/echo_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");
router.message.register(echo_handler);
Expand Down
7 changes: 2 additions & 5 deletions examples/finite_state_machine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

// You can use any storage, which implements `Storage` trait
let storage = MemoryStorage::new();
Expand Down
7 changes: 2 additions & 5 deletions examples/from_event_and_context/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");

Expand Down
7 changes: 2 additions & 5 deletions examples/input_file/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");
router.message.register(input_file_handler);
Expand Down
7 changes: 2 additions & 5 deletions examples/router_tree/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut main_router = Router::new("main");

Expand Down
7 changes: 2 additions & 5 deletions examples/stats_incoming_updates_middleware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");
// Register outer middleware for update
Expand Down
7 changes: 2 additions & 5 deletions examples/text_case_filters/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");
router
Expand Down
7 changes: 2 additions & 5 deletions examples/text_formatting/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ async fn main() {
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let Ok(bot_token) = std::env::var("BOT_TOKEN") else {
panic!("BOT_TOKEN env variable is not set!");
};

let bot = Bot::new(bot_token);
let token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN env variable is not set!");
let bot = Bot::new(token);

let mut router = Router::new("main");
router.message.register(handler);
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ help:

# Run the tests with cargo
test:
cargo test --lib --tests --all --all-features --verbose -- --nocapture
cargo test --lib --tests --all --all-features -- --nocapture

# Run the clippy linter
clippy:
Expand Down
2 changes: 1 addition & 1 deletion telers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "telers"
version = "1.0.0-alpha.16"
version = "1.0.0-alpha.17"
edition = "2021"
description = "An asynchronous framework for Telegram Bot API written in Rust"
rust-version = "1.65"
Expand Down
2 changes: 1 addition & 1 deletion telers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<img src="https://img.shields.io/crates/v/telers?style=flat-square"/>
</a>
<a href="https://core.telegram.org/bots/api">
<img src="https://img.shields.io/badge/Telegram%20Bot%20API-7.1-blue?style=flat-square&logo=telegram&label=Telegram%20Bot%20API"/>
<img src="https://img.shields.io/badge/Telegram%20Bot%20API-7.2-blue?style=flat-square&logo=telegram&label=Telegram%20Bot%20API"/>
</a>

<h3>
Expand Down
8 changes: 4 additions & 4 deletions telers/src/enums/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub enum ContentType {
Invoice,
#[strum(serialize = "successful_payment")]
SuccessfulPayment,
#[strum(serialize = "user_shared")]
UserShared,
#[strum(serialize = "users_shared")]
UsersShared,
#[strum(serialize = "chat_shared")]
ChatShared,
#[strum(serialize = "connected_website")]
Expand Down Expand Up @@ -147,7 +147,7 @@ impl ContentType {
ContentType::PinnedMessage,
ContentType::Invoice,
ContentType::SuccessfulPayment,
ContentType::UserShared,
ContentType::UsersShared,
ContentType::ChatShared,
ContentType::ConnectedWebsite,
ContentType::WriteAccessAllowed,
Expand Down Expand Up @@ -224,7 +224,7 @@ impl From<&Message> for ContentType {
Message::Pinned(_) => ContentType::PinnedMessage,
Message::Invoice(_) => ContentType::Invoice,
Message::SuccessfulPayment(_) => ContentType::SuccessfulPayment,
Message::UsersShared(_) => ContentType::UserShared,
Message::UsersShared(_) => ContentType::UsersShared,
Message::ChatShared(_) => ContentType::ChatShared,
Message::ConnectedWebsite(_) => ContentType::ConnectedWebsite,
Message::WriteAccessAllowed(_) => ContentType::WriteAccessAllowed,
Expand Down
22 changes: 21 additions & 1 deletion telers/src/enums/observer_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub enum Telegram {
EditedMessage,
#[strum(serialize = "edited_channel_post")]
EditedChannelPost,
#[strum(serialize = "business_connection")]
BusinessConnection,
#[strum(serialize = "business_message")]
BusinessMessage,
#[strum(serialize = "edited_business_message")]
EditedBusinessMessage,
#[strum(serialize = "deleted_business_messages")]
DeletedBusinessMessages,
#[strum(serialize = "message_reaction")]
MessageReaction,
#[strum(serialize = "message_reaction_count")]
Expand Down Expand Up @@ -47,7 +55,7 @@ pub enum Telegram {

impl Telegram {
#[must_use]
pub const fn all() -> [Telegram; 19] {
pub const fn all() -> [Telegram; 23] {
[
Telegram::Message,
Telegram::InlineQuery,
Expand All @@ -56,6 +64,10 @@ impl Telegram {
Telegram::ChannelPost,
Telegram::EditedMessage,
Telegram::EditedChannelPost,
Telegram::BusinessConnection,
Telegram::BusinessMessage,
Telegram::EditedBusinessMessage,
Telegram::DeletedBusinessMessages,
Telegram::MessageReaction,
Telegram::MessageReactionCount,
Telegram::ShippingQuery,
Expand All @@ -82,6 +94,10 @@ impl From<Telegram> for Option<UpdateType> {
Telegram::ChannelPost => Some(UpdateType::ChannelPost),
Telegram::EditedMessage => Some(UpdateType::EditedMessage),
Telegram::EditedChannelPost => Some(UpdateType::EditedChannelPost),
Telegram::BusinessConnection => Some(UpdateType::BusinessConnection),
Telegram::BusinessMessage => Some(UpdateType::BusinessMessage),
Telegram::EditedBusinessMessage => Some(UpdateType::EditedBusinessMessage),
Telegram::DeletedBusinessMessages => Some(UpdateType::DeletedBusinessMessages),
Telegram::MessageReaction => Some(UpdateType::MessageReaction),
Telegram::MessageReactionCount => Some(UpdateType::MessageReactionCount),
Telegram::ShippingQuery => Some(UpdateType::ShippingQuery),
Expand All @@ -108,6 +124,10 @@ impl PartialEq<UpdateType> for Telegram {
Telegram::ChannelPost => *other == UpdateType::ChannelPost,
Telegram::EditedMessage => *other == UpdateType::EditedMessage,
Telegram::EditedChannelPost => *other == UpdateType::EditedChannelPost,
Telegram::BusinessConnection => *other == UpdateType::BusinessConnection,
Telegram::BusinessMessage => *other == UpdateType::BusinessMessage,
Telegram::EditedBusinessMessage => *other == UpdateType::EditedBusinessMessage,
Telegram::DeletedBusinessMessages => *other == UpdateType::DeletedBusinessMessages,
Telegram::MessageReaction => *other == UpdateType::MessageReaction,
Telegram::MessageReactionCount => *other == UpdateType::MessageReactionCount,
Telegram::ShippingQuery => *other == UpdateType::ShippingQuery,
Expand Down
18 changes: 17 additions & 1 deletion telers/src/enums/update_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub enum UpdateType {
EditedMessage,
#[strum(serialize = "edited_channel_post")]
EditedChannelPost,
#[strum(serialize = "business_connection")]
BusinessConnection,
#[strum(serialize = "business_message")]
BusinessMessage,
#[strum(serialize = "edited_business_message")]
EditedBusinessMessage,
#[strum(serialize = "deleted_business_messages")]
DeletedBusinessMessages,
#[strum(serialize = "message_reaction")]
MessageReaction,
#[strum(serialize = "message_reaction_count")]
Expand All @@ -47,7 +55,7 @@ pub enum UpdateType {

impl UpdateType {
#[must_use]
pub const fn all() -> [Self; 18] {
pub const fn all() -> [Self; 22] {
[
UpdateType::Message,
UpdateType::InlineQuery,
Expand All @@ -56,6 +64,10 @@ impl UpdateType {
UpdateType::ChannelPost,
UpdateType::EditedMessage,
UpdateType::EditedChannelPost,
UpdateType::BusinessConnection,
UpdateType::BusinessMessage,
UpdateType::EditedBusinessMessage,
UpdateType::DeletedBusinessMessages,
UpdateType::MessageReaction,
UpdateType::MessageReactionCount,
UpdateType::ShippingQuery,
Expand All @@ -78,6 +90,10 @@ impl<'a> From<&'a UpdateKind> for UpdateType {
UpdateKind::EditedMessage(_) => UpdateType::EditedMessage,
UpdateKind::ChannelPost(_) => UpdateType::ChannelPost,
UpdateKind::EditedChannelPost(_) => UpdateType::EditedChannelPost,
UpdateKind::BusinessConnection(_) => UpdateType::BusinessConnection,
UpdateKind::BusinessMessage(_) => UpdateType::BusinessMessage,
UpdateKind::EditedBusinessMessage(_) => UpdateType::EditedBusinessMessage,
UpdateKind::DeletedBusinessMessages(_) => UpdateType::DeletedBusinessMessages,
UpdateKind::MessageReaction(_) => UpdateType::MessageReaction,
UpdateKind::MessageReactionCount(_) => UpdateType::MessageReactionCount,
UpdateKind::InlineQuery(_) => UpdateType::InlineQuery,
Expand Down
Loading