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 implementation for getStickerSet #230

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
pub use telegram_bot_raw::CanAnswerCallbackQuery;
pub use telegram_bot_raw::CanAnswerInlineQuery;
pub use telegram_bot_raw::CanExportChatInviteLink;
pub use telegram_bot_raw::CanGetStickerSet;
pub use telegram_bot_raw::CanLeaveChat;
pub use telegram_bot_raw::CanSendChatAction;
pub use telegram_bot_raw::{CanDeleteMessage, CanForwardMessage};
Expand Down
52 changes: 51 additions & 1 deletion lib/src/util/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
//! [`telegram_bot_raw::types::message`]: ../../telegram_bot_raw/types/message/index.html

use crate::prelude::CanGetFile;
use crate::prelude::CanGetStickerSet;
use crate::types::{
requests::get_file::GetFile, ChannelPost, Message, MessageKind, MessageOrChannelPost,
requests::get_file::GetFile, requests::get_sticker_set::GetStickerSet, ChannelPost, Message,
MessageKind, MessageOrChannelPost,
};

/// A trait to obtain text from a message.
Expand Down Expand Up @@ -152,3 +154,51 @@ impl MessageGetFiles for ChannelPost {
self.kind.get_files()
}
}

/// A trait to obtain `GetStickerSet` requests from a message.
///
/// Only available on a sticker message.
pub trait MessageGetStickerSet {
/// Obtain a sticker set from a message if available.
fn get_sticker_set(&self) -> Option<GetStickerSet>;
}

impl MessageGetStickerSet for Message {
fn get_sticker_set<'a>(&'a self) -> Option<GetStickerSet> {
self.kind.get_sticker_set()
}
}

impl MessageGetStickerSet for MessageKind {
fn get_sticker_set<'a>(&'a self) -> Option<GetStickerSet> {
match self {
MessageKind::Text { .. } => None,
MessageKind::Audio { .. } => None,
MessageKind::Document { .. } => None,
MessageKind::Photo { .. } => None,
MessageKind::Sticker { data } => match data.set_name {
Some(_) => Some(data.get_sticker_set()),
None => None,
},
MessageKind::Video { .. } => None,
MessageKind::Voice { .. } => None,
MessageKind::VideoNote { .. } => None,
MessageKind::Contact { .. } => None,
MessageKind::Location { .. } => None,
MessageKind::Poll { .. } => None,
MessageKind::Venue { .. } => None,
MessageKind::NewChatMembers { .. } => None,
MessageKind::LeftChatMember { .. } => None,
MessageKind::NewChatTitle { .. } => None,
MessageKind::NewChatPhoto { .. } => None,
MessageKind::DeleteChatPhoto => None,
MessageKind::GroupChatCreated => None,
MessageKind::SupergroupChatCreated => None,
MessageKind::ChannelChatCreated => None,
MessageKind::MigrateToChatId { .. } => None,
MessageKind::MigrateFromChatId { .. } => None,
MessageKind::PinnedMessage { .. } => None,
MessageKind::Unknown { .. } => None,
}
}
}
43 changes: 43 additions & 0 deletions raw/src/requests/get_sticker_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::requests::*;
use crate::types::*;

/// Use this method to get a sticker set.
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
#[must_use = "requests do nothing unless sent"]
pub struct GetStickerSet {
name: StickerSetRef,
}

impl Request for GetStickerSet {
type Type = JsonRequestType<Self>;
type Response = JsonIdResponse<StickerSet>;

fn serialize(&self) -> Result<HttpRequest, Error> {
Self::Type::serialize(RequestUrl::method("getStickerSet"), self)
}
}

impl GetStickerSet {
pub fn new<S>(set: S) -> Self
where
S: ToStickerSetRef,
{
Self {
name: set.to_sticker_set_ref(),
}
}
}

/// Get basic info about a sticker set and prepare it for downloading.
pub trait CanGetStickerSet {
fn get_sticker_set(&self) -> GetStickerSet;
}

impl<S> CanGetStickerSet for S
where
S: ToStickerSetRef,
{
fn get_sticker_set(&self) -> GetStickerSet {
GetStickerSet::new(self)
}
}
2 changes: 2 additions & 0 deletions raw/src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod get_chat_member;
pub mod get_chat_members_count;
pub mod get_file;
pub mod get_me;
pub mod get_sticker_set;
pub mod get_updates;
pub mod get_user_profile_photos;
pub mod kick_chat_member;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub use self::get_chat_member::*;
pub use self::get_chat_members_count::*;
pub use self::get_file::*;
pub use self::get_me::*;
pub use self::get_sticker_set::*;
pub use self::get_updates::*;
pub use self::get_user_profile_photos::*;
pub use self::kick_chat_member::*;
Expand Down
17 changes: 17 additions & 0 deletions raw/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,23 @@ pub struct Sticker {
pub file_size: Option<Integer>,
}

/// This object represents a sticker set.
#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
pub struct StickerSet {
/// Sticker set name.
pub name: String,
/// Sticket set title.
pub title: String,
/// True, if the sticker set contains animated stickers.
pub is_animated: bool,
/// True, if the sticker set contains masks.
pub contains_masks: bool,
/// List of all set stickers.
pub stickers: Vec<Sticker>,
/// Sticker set thumbnail in the .WEBP or .TGS format.
pub thumb: Option<PhotoSize>,
}

/// This object represents a video file.
#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
pub struct Video {
Expand Down
56 changes: 56 additions & 0 deletions raw/src/types/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ specific_chat_id_impls!(ChannelId, Channel);
pub struct ChatId(Integer);
chat_id_impls!(ChatId);

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Name(String);

/// Get `MessageId` from the type reference.
pub trait ToMessageId {
fn to_message_id(&self) -> MessageId;
Expand Down Expand Up @@ -378,6 +381,59 @@ impl Serialize for FileRef {
}
}

/// Get `StickerSetRef` from the type reference.
pub trait ToStickerSetRef {
fn to_sticker_set_ref(&self) -> StickerSetRef;
}

impl<S> ToStickerSetRef for S
where
S: Deref,
S::Target: ToStickerSetRef,
{
fn to_sticker_set_ref(&self) -> StickerSetRef {
self.deref().to_sticker_set_ref()
}
}

impl ToStickerSetRef for Sticker {
fn to_sticker_set_ref(&self) -> StickerSetRef {
match &self.set_name {
Some(s) => s.to_owned().into(),
None => panic!("Failed to convert sticker set_name to StickerSetRef"),
}
}
}

/// Unique sticker set name identifier reference.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StickerSetRef {
pub(crate) inner: String,
}

impl<'a> From<&'a str> for StickerSetRef {
fn from(s: &'a str) -> Self {
StickerSetRef {
inner: s.to_string(),
}
}
}

impl<'a> From<String> for StickerSetRef {
fn from(s: String) -> Self {
StickerSetRef { inner: s.clone() }
}
}

impl Serialize for StickerSetRef {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.inner)
}
}

/// Get `CallbackQueryId` from the type reference.
pub trait ToCallbackQueryId {
fn to_callback_query_id(&self) -> CallbackQueryId;
Expand Down