Skip to content

Commit

Permalink
Add new types. Change old types and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Desiders committed Jun 10, 2024
1 parent b0b57be commit 1682f05
Show file tree
Hide file tree
Showing 24 changed files with 1,236 additions and 368 deletions.
6 changes: 5 additions & 1 deletion telers/src/enums/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub enum ContentType {
ProximityAlertTriggered,
#[strum(serialize = "chat_boost_added")]
ChatBoostAdded,
#[strum(serialize = "chat_background_set")]
ChatBackgroundSet,
#[strum(serialize = "forum_topic_created")]
ForumTopicCreated,
#[strum(serialize = "forum_topic_edited")]
Expand Down Expand Up @@ -115,7 +117,7 @@ pub enum ContentType {

impl ContentType {
#[must_use]
pub const fn all() -> [ContentType; 52] {
pub const fn all() -> [ContentType; 53] {
[
ContentType::Text,
ContentType::Animation,
Expand Down Expand Up @@ -154,6 +156,7 @@ impl ContentType {
ContentType::PassportData,
ContentType::ProximityAlertTriggered,
ContentType::ChatBoostAdded,
ContentType::ChatBackgroundSet,
ContentType::ForumTopicCreated,
ContentType::ForumTopicEdited,
ContentType::ForumTopicClosed,
Expand Down Expand Up @@ -231,6 +234,7 @@ impl From<&Message> for ContentType {
Message::PassportData(_) => ContentType::PassportData,
Message::ProximityAlertTriggered(_) => ContentType::ProximityAlertTriggered,
Message::ChatBoostAdded(_) => ContentType::ChatBoostAdded,
Message::ChatBackgroundSet(_) => ContentType::ChatBackgroundSet,
Message::ForumTopicCreated(_) => ContentType::ForumTopicCreated,
Message::ForumTopicEdited(_) => ContentType::ForumTopicEdited,
Message::ForumTopicClosed(_) => ContentType::ForumTopicClosed,
Expand Down
19 changes: 19 additions & 0 deletions telers/src/methods/edit_message_live_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub struct EditMessageLiveLocation {
pub longitude: f64,
/// Latitude of new location
pub latitude: f64,
/// New period in seconds during which the location can be updated, starting from the message send date. If `0x7FFFFFFF` is specified, then the location can be updated forever. Otherwise, the new value must not exceed the current `live_period` by more than a day, and the live location expiration date must remain within the next 90 days. If not specified, then `live_period` remains unchanged
pub live_period: Option<i64>,
/// The radius of uncertainty for the location, measured in meters; 0-1500
pub horizontal_accuracy: Option<f64>,
/// For live locations, a direction in which the user is moving, in degrees. Must be between 1 and 360 if specified.
Expand All @@ -46,6 +48,7 @@ impl EditMessageLiveLocation {
inline_message_id: None,
longitude,
latitude,
live_period: None,
horizontal_accuracy: None,
heading: None,
proximity_alert_radius: None,
Expand Down Expand Up @@ -93,6 +96,14 @@ impl EditMessageLiveLocation {
}
}

#[must_use]
pub fn live_period(self, val: i64) -> Self {
Self {
live_period: Some(val),
..self
}
}

#[must_use]
pub fn horizontal_accuracy(self, val: f64) -> Self {
Self {
Expand Down Expand Up @@ -151,6 +162,14 @@ impl EditMessageLiveLocation {
}
}

#[must_use]
pub fn live_period_option(self, val: Option<i64>) -> Self {
Self {
live_period: val,
..self
}
}

#[must_use]
pub fn horizontal_accuracy_option(self, val: Option<f64>) -> Self {
Self {
Expand Down
8 changes: 4 additions & 4 deletions telers/src/methods/get_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use super::base::{Request, TelegramMethod};

use crate::{
client::Bot,
types::{Chat, ChatIdKind},
types::{ChatFullInfo, ChatIdKind},
};

use serde::Serialize;

/// Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
/// Use this method to get up-to-date information about the chat
/// # Documentation
/// <https://core.telegram.org/bots/api#getchat>
/// # Returns
/// Returns a [`Chat`] object on success
/// Returns a [`ChatFullInfo`] object on success
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize)]
pub struct GetChat {
/// Unique identifier for the target chat or username of the target supergroup or channel (in the format `@channelusername`)
Expand All @@ -36,7 +36,7 @@ impl GetChat {

impl TelegramMethod for GetChat {
type Method = Self;
type Return = Chat;
type Return = ChatFullInfo;

fn build_request<Client>(&self, _bot: &Bot<Client>) -> Request<Self::Method> {
Request::new("getChat", self, None)
Expand Down
76 changes: 69 additions & 7 deletions telers/src/methods/send_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::base::{Request, TelegramMethod};

use crate::{
client::Bot,
types::{ChatIdKind, Message, MessageEntity, ReplyMarkup, ReplyParameters},
types::{ChatIdKind, InputPollOption, Message, MessageEntity, ReplyMarkup, ReplyParameters},
};

use serde::Serialize;
Expand All @@ -24,8 +24,12 @@ pub struct SendPoll {
pub message_thread_id: Option<i64>,
/// Poll question, 1-300 characters
pub question: String,
/// A JSON-serialized list of answer options, 2-10 strings 1-100 characters each
pub options: Vec<String>,
/// Mode for parsing entities in the question. See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details. Currently, only custom emoji entities are allowed
pub question_parse_mode: Option<String>,
/// A JSON-serialized list of special entities that appear in the poll question. It can be specified instead of `question_parse_mode`
pub question_entities: Option<Vec<MessageEntity>>,
/// A JSON-serialized list of 2-10 answer options
pub options: Vec<InputPollOption>,
/// `true`, if the poll needs to be anonymous, defaults to `true`
pub is_anonymous: Option<bool>,
/// Poll type, `quiz` or `regular`, defaults to `regular`
Expand Down Expand Up @@ -59,16 +63,22 @@ pub struct SendPoll {

impl SendPoll {
#[must_use]
pub fn new<T, I>(chat_id: impl Into<ChatIdKind>, question: T, options: I) -> Self
pub fn new<T, I>(
chat_id: impl Into<ChatIdKind>,
question: impl Into<String>,
options: I,
) -> Self
where
T: Into<String>,
T: Into<InputPollOption>,
I: IntoIterator<Item = T>,
{
Self {
business_connection_id: None,
chat_id: chat_id.into(),
message_thread_id: None,
question: question.into(),
question_parse_mode: None,
question_entities: None,
options: options.into_iter().map(Into::into).collect(),
is_anonymous: None,
poll_type: None,
Expand Down Expand Up @@ -120,7 +130,43 @@ impl SendPoll {
}

#[must_use]
pub fn option(self, val: impl Into<String>) -> Self {
pub fn question_parse_mode(self, val: impl Into<String>) -> Self {
Self {
question_parse_mode: Some(val.into()),
..self
}
}

#[must_use]
pub fn question_entity(self, val: MessageEntity) -> Self {
Self {
question_entities: Some(
self.question_entities
.unwrap_or_default()
.into_iter()
.chain(Some(val))
.collect(),
),
..self
}
}

#[must_use]
pub fn question_entities(self, val: impl IntoIterator<Item = MessageEntity>) -> Self {
Self {
question_entities: Some(
self.question_entities
.unwrap_or_default()
.into_iter()
.chain(val)
.collect(),
),
..self
}
}

#[must_use]
pub fn option(self, val: impl Into<InputPollOption>) -> Self {
Self {
options: self.options.into_iter().chain(Some(val.into())).collect(),
..self
Expand All @@ -130,7 +176,7 @@ impl SendPoll {
#[must_use]
pub fn options<T, I>(self, val: I) -> Self
where
T: Into<String>,
T: Into<InputPollOption>,
I: IntoIterator<Item = T>,
{
Self {
Expand Down Expand Up @@ -293,6 +339,22 @@ impl SendPoll {
}
}

#[must_use]
pub fn question_option(self, val: Option<impl Into<String>>) -> Self {
Self {
question: val.map(Into::into).unwrap_or_default(),
..self
}
}

#[must_use]
pub fn question_parse_mode_option(self, val: Option<impl Into<String>>) -> Self {
Self {
question_parse_mode: val.map(Into::into),
..self
}
}

#[must_use]
pub fn is_anonymous_option(self, val: Option<bool>) -> Self {
Self {
Expand Down
2 changes: 1 addition & 1 deletion telers/src/methods/send_voice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use serde::Serialize;
use serde_with::skip_serializing_none;

/// Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent as [Audio](crate::types::Audio) or [Document](crate::types::Document)). Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
/// Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS, or in .MP3 format, or in .M4A format (other formats may be sent as [Audio](crate::types::Audio) or [Document](crate::types::Document)). Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
/// # Documentation
/// <https://core.telegram.org/bots/api#sendvoice>
/// # Returns
Expand Down
32 changes: 28 additions & 4 deletions telers/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
pub mod animation;
pub mod audio;
pub mod background_fill;
pub mod background_fill_freeform_gradient;
pub mod background_fill_gradient;
pub mod background_fill_solid;
pub mod background_type;
pub mod background_type_chat_theme;
pub mod background_type_fill;
pub mod background_type_pattern;
pub mod background_type_wallpaper;
pub mod birthdate;
pub mod bot_command;
pub mod bot_command_scope;
Expand All @@ -62,6 +71,7 @@ pub mod callback_game;
pub mod callback_query;
pub mod chat;
pub mod chat_administrator_rights;
pub mod chat_background;
pub mod chat_boost;
pub mod chat_boost_added;
pub mod chat_boost_removed;
Expand All @@ -70,6 +80,7 @@ pub mod chat_boost_source_gift_code;
pub mod chat_boost_source_giveaway;
pub mod chat_boost_source_premium;
pub mod chat_boost_updated;
pub mod chat_full_info;
pub mod chat_id_kind;
pub mod chat_invite_link;
pub mod chat_join_request;
Expand Down Expand Up @@ -144,6 +155,7 @@ pub mod input_media_document;
pub mod input_media_photo;
pub mod input_media_video;
pub mod input_message_content;
pub mod input_poll_option;
pub mod input_sticker;
pub mod input_text_message_content;
pub mod input_venue_message_content;
Expand Down Expand Up @@ -234,6 +246,15 @@ pub mod write_access_allowed;

pub use animation::Animation;
pub use audio::Audio;
pub use background_fill::BackgroundFill;
pub use background_fill_freeform_gradient::BackgroundFillFreeformGradient;
pub use background_fill_gradient::BackgroundFillGradient;
pub use background_fill_solid::BackgroundFillSolid;
pub use background_type::BackgroundType;
pub use background_type_chat_theme::BackgroundTypeChatTheme;
pub use background_type_fill::BackgroundTypeFill;
pub use background_type_pattern::BackgroundTypePattern;
pub use background_type_wallpaper::BackgroundTypeWallpaper;
pub use birthdate::Birthdate;
pub use bot_command::BotCommand;
pub use bot_command_scope::BotCommandScope;
Expand All @@ -255,11 +276,9 @@ pub use business_opening_hours::BusinessOpeningHours;
pub use business_opening_hours_interval::BusinessOpeningHoursInterval;
pub use callback_game::CallbackGame;
pub use callback_query::CallbackQuery;
pub use chat::{
Channel as ChatChannel, Chat, Group as ChatGroup, Private as ChatPrivate,
Supergroup as ChatSupergroup,
};
pub use chat::Chat;
pub use chat_administrator_rights::ChatAdministratorRights;
pub use chat_background::ChatBackground;
pub use chat_boost::ChatBoost;
pub use chat_boost_added::ChatBoostAdded;
pub use chat_boost_removed::ChatBoostRemoved;
Expand All @@ -268,6 +287,10 @@ pub use chat_boost_source_gift_code::ChatBoostSourceGiftCode;
pub use chat_boost_source_giveaway::ChatBoostSourceGiveaway;
pub use chat_boost_source_premium::ChatBoostSourcePremium;
pub use chat_boost_updated::ChatBoostUpdated;
pub use chat_full_info::{
Channel as ChatChannel, ChatFullInfo, Group as ChatGroup, Private as ChatPrivate,
Supergroup as ChatSupergroup,
};
pub use chat_id_kind::ChatIdKind;
pub use chat_invite_link::ChatInviteLink;
pub use chat_join_request::ChatJoinRequest;
Expand Down Expand Up @@ -368,6 +391,7 @@ pub use input_media_document::InputMediaDocument;
pub use input_media_photo::InputMediaPhoto;
pub use input_media_video::InputMediaVideo;
pub use input_message_content::InputMessageContent;
pub use input_poll_option::InputPollOption;
pub use input_sticker::InputSticker;
pub use input_text_message_content::InputTextMessageContent;
pub use input_venue_message_content::InputVenueMessageContent;
Expand Down
35 changes: 35 additions & 0 deletions telers/src/types/background_fill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use super::{BackgroundFillFreeformGradient, BackgroundFillGradient, BackgroundFillSolid};

use serde::Deserialize;

/// 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)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BackgroundFill {
Solid(BackgroundFillSolid),
Gradient(BackgroundFillGradient),
FreeformGradient(BackgroundFillFreeformGradient),
}

impl From<BackgroundFillSolid> for BackgroundFill {
fn from(fill: BackgroundFillSolid) -> Self {
Self::Solid(fill)
}
}

impl From<BackgroundFillGradient> for BackgroundFill {
fn from(fill: BackgroundFillGradient) -> Self {
Self::Gradient(fill)
}
}

impl From<BackgroundFillFreeformGradient> for BackgroundFill {
fn from(fill: BackgroundFillFreeformGradient) -> Self {
Self::FreeformGradient(fill)
}
}
10 changes: 10 additions & 0 deletions telers/src/types/background_fill_freeform_gradient.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::Deserialize;

/// 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)]
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]>,
}
14 changes: 14 additions & 0 deletions telers/src/types/background_fill_gradient.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::Deserialize;

/// The background is a gradient fill
/// # Documentation
/// <https://core.telegram.org/bots/api#backgroundfillgradient>
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Deserialize)]
pub struct BackgroundFillGradient {
/// Top color of the gradient in the RGB24 format
pub top_color: u32,
/// Bottom color of the gradient in the RGB24 format
pub bottom_color: u32,
/// Clockwise rotation angle of the background fill in degrees; 0-359
pub rotation_angle: i32,
}
Loading

0 comments on commit 1682f05

Please sign in to comment.