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 Serialize for types #24

Merged
merged 3 commits into from
Jul 13, 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
12 changes: 12 additions & 0 deletions examples/serialize/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "serialize"
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"] }
serde_json = "1.0"
73 changes: 73 additions & 0 deletions examples/serialize/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! This example shows how to serialize Telegram types.
//!
//! 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 serialize
//! ```

use telers::{
enums::{ParseMode, UpdateType},
errors::HandlerError,
event::{telegram::HandlerResult, EventReturn, ToServiceProvider as _},
methods::SendMessage,
types::Update,
utils::text::{html_pre_language, html_quote},
Bot, Dispatcher, Router,
};

use tracing::{event, Level};
use tracing_subscriber::{fmt, layer::SubscriberExt as _, util::SubscriberInitExt as _, EnvFilter};

async fn serialize_handler(bot: Bot, update: Update) -> HandlerResult {
if let Some(chat_id) = update.chat_id() {
match serde_json::to_string_pretty(&update) {
Ok(text) => {
bot.send(
SendMessage::new(chat_id, html_pre_language(html_quote(text), "json"))
.parse_mode(ParseMode::HTML),
)
.await?;
}
Err(err) => {
bot.send(SendMessage::new(
chat_id,
format!("Serialize error :(\n\n{err:?}"),
))
.await?;

return Err(HandlerError::new(err));
}
}
}

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 bot = Bot::from_env_by_key("BOT_TOKEN");

let mut router = Router::new("main");
router.update.register(serialize_handler);

let dispatcher = Dispatcher::builder()
.main_router(router)
.bot(bot)
.allowed_updates(UpdateType::all())
.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: 4 additions & 3 deletions telers/src/types/animation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::PhotoSize;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).
/// # Documentation
/// <https://core.telegram.org/bots/api#animation>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[skip_serializing_none]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct Animation {
/// Identifier for this file, which can be used to download or reuse the file
pub file_id: Box<str>,
Expand Down
6 changes: 4 additions & 2 deletions telers/src/types/audio.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::PhotoSize;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// This object represents an audio file to be treated as music by the Telegram clients.
/// # Documentation
/// <https://core.telegram.org/bots/api#audio>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[skip_serializing_none]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct Audio {
/// Identifier for this file, which can be used to download or reuse the file
pub file_id: Box<str>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_fill.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::{BackgroundFillFreeformGradient, BackgroundFillGradient, BackgroundFillSolid};

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// This object describes the way a background is filled based on the selected colors. Currently, it can be one of
/// - [`BackgroundFillSolid`]
/// - [`BackgroundFillGradient`]
/// - [`BackgroundFillFreeformGradient`]
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundfill>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BackgroundFill {
Solid(BackgroundFillSolid),
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_fill_freeform_gradient.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// The background is a freeform gradient that rotates after every message in the chat
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundfillfreeformgradient>
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundFillFreeformGradient {
/// A list of the 3 or 4 base colors that are used to generate the freeform gradient in the RGB24 format
pub colors: Box<[u32]>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_fill_gradient.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// The background is a gradient fill
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundfillgradient>
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundFillGradient {
/// Top color of the gradient in the RGB24 format
pub top_color: u32,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_fill_solid.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// The background is filled using the selected color
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundfillsolid>
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundFillSolid {
/// The color of the background fill in the RGB24 format
pub color: u32,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
BackgroundTypeChatTheme, BackgroundTypeFill, BackgroundTypePattern, BackgroundTypeWallpaper,
};

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// This object describes the type of a background. Currently, it can be one of
/// - [`BackgroundTypeFill`]
Expand All @@ -11,7 +11,7 @@ use serde::Deserialize;
/// - [`BackgroundTypeChatTheme`]
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundtype>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BackgroundType {
Fill(BackgroundTypeFill),
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_type_chat_theme.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// The background is taken directly from a built-in chat theme.
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundtypechattheme>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundTypeChatTheme {
/// Name of the chat theme, which is usually an emoji
#[serde(rename = "theme_name")]
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/background_type_fill.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::BackgroundFill;

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// The background is automatically filled based on the selected colors
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundtypefill>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundTypeFill {
/// The background fill
pub fill: BackgroundFill,
Expand Down
6 changes: 4 additions & 2 deletions telers/src/types/background_type_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::{BackgroundFill, Document};

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// The background is a PNG or TGV (gzipped subset of SVG with MIME type “application/x-tgwallpattern”) pattern to be combined with the background fill chosen by the user
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundtypepattern>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[skip_serializing_none]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundTypePattern {
/// Document with the wallpaper
pub document: Document,
Expand Down
6 changes: 4 additions & 2 deletions telers/src/types/background_type_wallpaper.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::Document;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// The background is a wallpaper in the JPEG format
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundtypewallpaper>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[skip_serializing_none]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BackgroundTypeWallpaper {
/// Document with the wallpaper
pub document: Document,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/birthdate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// # Documentation
/// <https://core.telegram.org/bots/api#birthdate>
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct Birthdate {
/// Day of the user's birth; 1-31
pub day: i8,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/bot_description.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// This object represents the bot's description.
/// # Documentation
/// <https://core.telegram.org/bots/api#botdescription>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BotDescription {
/// The bot's description
pub description: Box<str>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/bot_name.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// This object represents the bot's name.
/// # Documentation
/// <https://core.telegram.org/bots/api#botname>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BotName {
/// The bot's name
pub name: Box<str>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/bot_short_description.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// This object represents the bot's short description.
/// # Documentation
/// <https://core.telegram.org/bots/api#botshortdescription>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BotShortDescription {
/// The bot's short description
pub short_description: Box<str>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/business_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::{Update, UpdateKind, User};

use crate::{errors::ConvertToTypeError, FromEvent};

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// Describes the connection of the bot with a business account.
/// # Documentation
/// <https://core.telegram.org/bots/api#businessconnection>
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize, FromEvent)]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize, Serialize, FromEvent)]
#[event(try_from = Update)]
pub struct BusinessConnection {
/// Unique identifier of the business connection
Expand Down
6 changes: 4 additions & 2 deletions telers/src/types/business_intro.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::Sticker;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// # Documentation
/// <https://core.telegram.org/bots/api#businessintro>
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct BusinessIntro {
/// Title text of the business intro
pub title: Option<Box<str>>,
Expand Down
6 changes: 4 additions & 2 deletions telers/src/types/business_location.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::Location;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// # Documentation
/// <https://core.telegram.org/bots/api#businesslocation>
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct BusinessLocation {
/// Address of the business
pub address: Box<str>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/business_messages_deleted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::{Chat, Update, UpdateKind};

use crate::{errors::ConvertToTypeError, FromEvent};

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// This object is received when messages are deleted from a connected business account.
/// # Documentation
/// <https://core.telegram.org/bots/api#businessmessagesdeleted>
#[derive(Debug, Clone, PartialEq, Deserialize, FromEvent)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, FromEvent)]
#[event(try_from = Update)]
pub struct BusinessMessagesDeleted {
/// Unique identifier of the business connection
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/business_opening_hours.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::BusinessOpeningHoursInterval;

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// # Documentation
/// <https://core.telegram.org/bots/api#businessopeninghours>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BusinessOpeningHours {
/// Unique name of the time zone for which the opening hours are defined
pub time_zone_name: Box<str>,
Expand Down
4 changes: 2 additions & 2 deletions telers/src/types/business_opening_hours_interval.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// # Documentation
/// <https://core.telegram.org/bots/api#businessopeninghoursinterval>
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct BusinessOpeningHoursInterval {
/// The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60
pub opening_minute: i64,
Expand Down
6 changes: 4 additions & 2 deletions telers/src/types/callback_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use super::{InaccessibleMessage, MaybeInaccessibleMessage, Update, UpdateKind, U

use crate::{errors::ConvertToTypeError, FromEvent};

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// This object represents an incoming callback query from a callback button in an [`inline keyboard`](https://core.telegram.org/bots/features#inline-keyboards). If the button that originated the query was attached to a message sent by the bot, the field `message` will be present. If the button was attached to a message sent via the bot (in [`inline mode`](https://core.telegram.org/bots/api#inline-mode)), the field `inline_message_id` will be present. Exactly one of the fields *data* or `game_short_name` will be present.
/// **NOTE:** After the user presses a callback button, Telegram clients will display a progress bar until you call [`AnswerCallbackQuery`](crate::methods::AnswerCallbackQuery). It is, therefore, necessary to react by calling [`AnswerCallbackQuery`](crate::methods::AnswerCallbackQuery) even if no notification to the user is needed (e.g., without specifying any of the optional parameters).
/// # Documentation
/// <https://core.telegram.org/bots/api#callbackquery>
#[derive(Debug, Default, Clone, PartialEq, Deserialize, FromEvent)]
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize, FromEvent)]
#[event(try_from = Update)]
pub struct CallbackQuery {
/// Unique identifier for this query
Expand Down
Loading