telega/reactions

reactions provides convenient helpers for working with Telegram reactions.

Sending reactions

import telega/reactions

// React to current message with emoji
reactions.react(ctx, reactions.thumbs_up)

// React with custom emoji string
reactions.react_emoji(ctx, "👍")

// React with big animation
reactions.react_big(ctx, reactions.heart)

Handling reaction updates

import telega/router
import telega/reactions

router.new()
|> router.on_reaction(fn(ctx, update) {
  let changes = reactions.get_changes(update)
  // Process added/removed reactions
  Ok(ctx)
})

Types

Information about reaction changes

pub type ReactionChanges {
  ReactionChanges(
    added: List(types.ReactionType),
    removed: List(types.ReactionType),
    kept: List(types.ReactionType),
  )
}

Constructors

Reaction count information

pub type ReactionCountInfo {
  ReactionCountInfo(reaction: types.ReactionType, count: Int)
}

Constructors

Values

pub fn clear_reaction(
  ctx ctx: bot.Context(session, error),
) -> Result(Bool, error.TelegaError)

Clear all bot’s reactions from the current message

Example

reactions.clear_reaction(ctx)
pub fn custom_emoji(
  custom_emoji_id: String,
) -> types.ReactionType

Create a custom emoji reaction

pub fn emoji(emoji_str: String) -> types.ReactionType

Create an emoji reaction

pub fn get_changes(
  update: types.MessageReactionUpdated,
) -> ReactionChanges

Get detailed information about reaction changes

Example

let changes = reactions.get_changes(update)
list.each(changes.added, fn(r) {
  case reactions.get_emoji(r) {
    Some(e) -> io.println("Added: " <> e)
    None -> Nil
  }
})
pub fn get_counts(
  update: types.MessageReactionCountUpdated,
) -> List(ReactionCountInfo)

Get all reaction counts from a MessageReactionCountUpdated

pub fn get_custom_emoji_id(
  reaction: types.ReactionType,
) -> option.Option(String)

Get custom emoji ID from a ReactionType (if it’s a custom emoji reaction)

pub fn get_emoji(
  reaction: types.ReactionType,
) -> option.Option(String)

Get emoji string from a ReactionType (if it’s an emoji reaction)

Example

case reactions.get_emoji(reaction) {
  Some(emoji) -> io.println("Emoji: " <> emoji)
  None -> io.println("Not an emoji reaction")
}
pub fn get_emoji_count(
  update: types.MessageReactionCountUpdated,
  emoji_str: String,
) -> Int

Get count for a specific emoji

pub fn get_top_reactions(
  update: types.MessageReactionCountUpdated,
  limit: Int,
) -> List(ReactionCountInfo)

Get top reactions sorted by count

pub fn get_total_count(
  update: types.MessageReactionCountUpdated,
) -> Int

Get total count across all reactions

pub fn has_added(update: types.MessageReactionUpdated) -> Bool

Check if there are any added reactions

pub fn has_removed(update: types.MessageReactionUpdated) -> Bool

Check if there are any removed reactions

pub fn is_custom_emoji(reaction: types.ReactionType) -> Bool

Check if a reaction is a custom emoji reaction

pub fn is_emoji(reaction: types.ReactionType) -> Bool

Check if a reaction is an emoji reaction

pub fn is_paid(reaction: types.ReactionType) -> Bool

Check if a reaction is a paid reaction (stars)

pub fn matches_emoji(
  reaction: types.ReactionType,
  emoji_str: String,
) -> Bool

Check if a reaction matches a specific emoji

pub fn paid() -> types.ReactionType

Create a paid reaction (stars)

pub fn react(
  ctx ctx: bot.Context(session, error),
  reaction reaction: types.ReactionType,
) -> Result(Bool, error.TelegaError)

React to the current message with a reaction

Example

reactions.react(ctx, reactions.thumbs_up)
pub fn react_big(
  ctx ctx: bot.Context(session, error),
  reaction reaction: types.ReactionType,
) -> Result(Bool, error.TelegaError)

React to the current message with a big animation

Example

reactions.react_big(ctx, reactions.party)
pub fn react_emoji(
  ctx ctx: bot.Context(session, error),
  emoji_str emoji_str: String,
) -> Result(Bool, error.TelegaError)

React to the current message with an emoji string

Example

reactions.react_emoji(ctx, "👍")
pub fn react_many(
  ctx ctx: bot.Context(session, error),
  reactions reactions: List(types.ReactionType),
) -> Result(Bool, error.TelegaError)

React to the current message with multiple reactions

Example

reactions.react_many(ctx, [reactions.thumbs_up, reactions.heart])
pub fn react_to_chat_message(
  ctx ctx: bot.Context(session, error),
  chat_id chat_id: Int,
  message_id message_id: Int,
  reaction reaction: types.ReactionType,
) -> Result(Bool, error.TelegaError)

React to a specific message in a specific chat

Example

reactions.react_to_chat_message(ctx, chat_id: -100123, message_id: 456, reaction: reactions.fire)
pub fn react_to_message(
  ctx ctx: bot.Context(session, error),
  message_id message_id: Int,
  reaction reaction: types.ReactionType,
) -> Result(Bool, error.TelegaError)

React to a specific message by ID

Example

reactions.react_to_message(ctx, message_id: 123, reaction: reactions.heart)
pub fn reaction_equals(
  a: types.ReactionType,
  b: types.ReactionType,
) -> Bool

Check if two reactions are equal

pub fn was_added(
  update: types.MessageReactionUpdated,
  emoji_str: String,
) -> Bool

Check if a specific emoji was added

Example

if reactions.was_added(update, "👍") {
  // Handle like
}
pub fn was_removed(
  update: types.MessageReactionUpdated,
  emoji_str: String,
) -> Bool

Check if a specific emoji was removed

Example

if reactions.was_removed(update, "👍") {
  // Handle unlike
}
Search Document