Stickers
Methods for fetching, installing, and managing sticker sets, as well as resolving custom emoji.
See also: Sticker type in the Media reference for how to receive and download sticker files from messages.
Sticker sets
async
client.get_sticker_set(stickerset: tl::enums::InputStickerSet) → Result<tl::types::messages::StickerSet, InvocationError>
Fetch a sticker set and all its stickers. Pass an
InputStickerSet - the most common variants are:
InputStickerSet::InputStickerSetShortName- by@short_nameInputStickerSet::InputStickerSetID- by numeric ID + access hash
#![allow(unused)]
fn main() {
let set = client.get_sticker_set(
tl::enums::InputStickerSet::InputStickerSetShortName(
tl::types::InputStickerSetShortName { short_name: "Animals".into() }
)
).await?;
println!("Set: {} - {} stickers", set.set.title, set.set.count);
for doc in &set.documents {
if let tl::enums::Document::Document(d) = doc {
println!(" doc_id={}", d.id);
}
}
}
async
client.toggle_stickers(stickerset: tl::enums::InputStickerSet, archived: bool) → Result<tl::enums::messages::StickerSetInstallResult, InvocationError>
Install a sticker set for the current account. Pass
archived: true to add it to the archive instead of the active set list.
The return value is StickerSetInstallResult::Success on a clean install, or StickerSetInstallResult::Archive when older sets were moved to the archive to make room.
#![allow(unused)]
fn main() {
let result = client.toggle_stickers(
tl::enums::InputStickerSet::InputStickerSetShortName(
tl::types::InputStickerSetShortName { short_name: "Animals".into() }
),
false,
).await?;
}
async
client.untoggle_stickers(stickerset: tl::enums::InputStickerSet) → Result<(), InvocationError>
Remove a sticker set from the account's installed sets.
async
client.get_all_stickers(hash: i64) → Result<Option<Vec<tl::types::StickerSet>>, InvocationError>
List all sticker sets installed for the current account. Pass
hash = 0 to always get the full list. Returns None when the server confirms the list is unchanged (hash match - used for caching).
#![allow(unused)]
fn main() {
if let Some(sets) = client.get_all_stickers(0).await? {
for s in &sets {
println!("{} ({})", s.title, s.short_name);
}
}
}
Custom emoji
async
client.get_custom_emoji_documents(document_ids: Vec<i64>) → Result<Vec<tl::enums::Document>, InvocationError>
Fetch the
Document objects for a list of custom emoji IDs. Custom emoji IDs come from MessageEntity::CustomEmoji { document_id } when parsing formatted message text.
#![allow(unused)]
fn main() {
use ferogram::media::Document;
// Grab custom emoji IDs from a message's entities
let ids: Vec<i64> = msg.raw.entities.iter().flatten()
.filter_map(|e| match e {
tl::enums::MessageEntity::CustomEmoji(ce) => Some(ce.document_id),
_ => None,
})
.collect();
let docs = client.get_custom_emoji_documents(ids).await?;
for doc in &docs {
let d = Document::from_raw(match doc.clone() {
tl::enums::Document::Document(d) => d,
_ => continue,
});
println!("emoji doc {} mime={}", d.id(), d.mime_type());
}
}
Sending a sticker
Stickers are sent as document media. Get the sticker’s Document, build an InputDocument, and use send_file:
#![allow(unused)]
fn main() {
use ferogram::media::Sticker;
// From a received sticker message
if let Some(sticker) = Sticker::from_media(&incoming_msg.raw) {
let input_media = tl::enums::InputMedia::InputMediaDocument(
tl::types::InputMediaDocument {
spoiler: false,
optional_attributes: false,
id: tl::enums::InputDocument::InputDocument(tl::types::InputDocument {
id: sticker.id(),
access_hash: sticker.access_hash(), // from raw document
file_reference: vec![], // get from raw Document
}),
ttl_seconds: None,
query: None,
}
);
client.send_file(peer, input_media, "").await?;
}
}