telegram

package
v0.0.0-...-b6da682 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 36 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMessageToCache

func AddMessageToCache(escarbot *EscarBot, message *tgbotapi.Message)

AddMessageToCache adds a message to the recent messages cache

func BotPoll

func BotPoll(escarbot *EscarBot)

func HandleCaptchaCallback

func HandleCaptchaCallback(escarbot *EscarBot, callback *tgbotapi.CallbackQuery)

func HandleStatsMessage

func HandleStatsMessage(bot *EscarBot, msg *tgbotapi.Message)

func SendCaptcha

func SendCaptcha(escarbot *EscarBot, chatID int64, user tgbotapi.User, joinMsgID int, attempts int)

func UpdateMessageInCache

func UpdateMessageInCache(escarbot *EscarBot, message *tgbotapi.Message)

UpdateMessageInCache updates a message in the recent messages cache

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache handles all bot caching, backed by Valkey/Redis when an address is provided, or falling back to in-memory storage when addr is empty.

func NewCache

func NewCache(addr string) *Cache

NewCache creates a Cache backed by Valkey at addr, or in-memory if addr is empty.

func (*Cache) AddMessage

func (c *Cache) AddMessage(chatID int64, msg CachedMessage, maxSize int) bool

AddMessage prepends msg to the chat's message list (capped at maxSize). Returns true if the message was actually added (not a duplicate).

func (*Cache) CleanupJoinEntries

func (c *Cache) CleanupJoinEntries()

CleanupJoinEntries removes stale entries in in-memory mode (Redis handles TTL expiry automatically).

func (*Cache) DeleteCaptcha

func (c *Cache) DeleteCaptcha(userID int64)

DeleteCaptcha removes a pending captcha and its associated timer.

func (*Cache) GetAllChats

func (c *Cache) GetAllChats() []ChatInfo

GetAllChats returns all cached chat metadata.

func (*Cache) GetAllMessages

func (c *Cache) GetAllMessages() map[int64][]CachedMessage

GetAllMessages returns all cached messages keyed by chat ID.

func (*Cache) GetCaptcha

func (c *Cache) GetCaptcha(userID int64) (*PendingCaptcha, bool)

GetCaptcha returns the pending captcha state for a user.

func (*Cache) GetChatInfo

func (c *Cache) GetChatInfo(chatID int64) (ChatInfo, bool)

GetChatInfo returns cached chat metadata.

func (*Cache) GetJoinEntry

func (c *Cache) GetJoinEntry(userID int64) (*JoinProcessedEntry, bool)

GetJoinEntry returns the join deduplication record for a user.

func (*Cache) GetMessages

func (c *Cache) GetMessages(chatID int64) []CachedMessage

GetMessages returns all cached messages for the given chat.

func (*Cache) GetReactions

func (c *Cache) GetReactions(chatID int64) ([]string, bool)

GetReactions returns the allowed emoji reactions for a chat.

func (*Cache) HasCaptcha

func (c *Cache) HasCaptcha(userID int64) bool

HasCaptcha reports whether a captcha is pending for the given user.

func (*Cache) SetBotReaction

func (c *Cache) SetBotReaction(chatID int64, msgID int, emoji string) (CachedMessage, bool)

SetBotReaction records the bot's own reaction on a cached message.

func (*Cache) SetCaptcha

func (c *Cache) SetCaptcha(userID int64, captcha *PendingCaptcha, ttl time.Duration)

SetCaptcha stores a pending captcha. The ExpirationTimer is kept in-memory; when using Valkey the record is stored with the given TTL.

func (*Cache) SetChatInfo

func (c *Cache) SetChatInfo(chatID int64, info ChatInfo)

SetChatInfo stores chat metadata and records the chat ID in the index set.

func (*Cache) SetJoinEntry

func (c *Cache) SetJoinEntry(userID int64, entry *JoinProcessedEntry)

SetJoinEntry stores a join deduplication record with a 1-minute TTL.

func (*Cache) SetReactions

func (c *Cache) SetReactions(chatID int64, reactions []string)

SetReactions stores the allowed emoji reactions for a chat.

func (*Cache) UpdateCaptchaJoinMsgID

func (c *Cache) UpdateCaptchaJoinMsgID(userID int64, joinMsgID int)

UpdateCaptchaJoinMsgID sets JoinMsgID on a pending captcha if it was zero.

func (*Cache) UpdateIndividualReaction

func (c *Cache) UpdateIndividualReaction(chatID int64, msgID int, userName string, newReactions []tgbotapi.ReactionType) (CachedMessage, bool)

UpdateIndividualReaction updates a single user's emoji reaction on a message.

func (*Cache) UpdateJoinEntryBanned

func (c *Cache) UpdateJoinEntryBanned(userID int64)

UpdateJoinEntryBanned marks a join record as banned.

func (*Cache) UpdateJoinEntryMsgID

func (c *Cache) UpdateJoinEntryMsgID(userID int64, joinMsgID int) (*JoinProcessedEntry, bool)

UpdateJoinEntryMsgID sets JoinMsgID on an existing join record if it was zero.

func (*Cache) UpdateMessage

func (c *Cache) UpdateMessage(chatID int64, updated *tgbotapi.Message) (CachedMessage, bool)

UpdateMessage edits an existing cached message's text/caption and saves a history entry. Returns the updated message and true when found.

func (*Cache) UpdateReactions

func (c *Cache) UpdateReactions(chatID int64, msgID int, reactions []tgbotapi.ReactionCount) (CachedMessage, bool)

UpdateReactions updates the aggregate reaction counts for a cached message.

type CachedMessage

type CachedMessage struct {
	MessageID          int                      `json:"message_id"`
	ChatID             int64                    `json:"chat_id,string"`
	ChatTitle          string                   `json:"chat_title,omitempty"`
	ChatPhotoURL       string                   `json:"chat_photo_url,omitempty"`
	FromUsername       string                   `json:"from_username"`
	FromFirstName      string                   `json:"from_first_name"`
	Text               string                   `json:"text"`
	Caption            string                   `json:"caption,omitempty"`
	MediaURL           string                   `json:"media_url,omitempty"`
	MediaType          string                   `json:"media_type,omitempty"`
	MediaDuration      int                      `json:"media_duration,omitempty"`
	MediaTitle         string                   `json:"media_title,omitempty"`
	MediaFilename      string                   `json:"media_filename,omitempty"`
	MediaMimeType      string                   `json:"media_mime_type,omitempty"`
	MediaFileSize      int64                    `json:"media_file_size,omitempty"`
	Poll               *CachedPoll              `json:"poll,omitempty"`
	Entities           []tgbotapi.MessageEntity `json:"entities,omitempty"`
	ThreadID           int                      `json:"thread_id,omitempty"`
	IsTopicMessage     bool                     `json:"is_topic_message"`
	AvailableReactions []string                 `json:"available_reactions,omitempty"`
	Reactions          []tgbotapi.ReactionCount `json:"reactions,omitempty"`
	RecentReactions    []ReactionDetail         `json:"recent_reactions,omitempty"`
	BotReaction        string                   `json:"bot_reaction,omitempty"`
	History            []MessageHistory         `json:"history,omitempty"`
}

CachedMessage represents a message stored in cache

type CachedPoll

type CachedPoll struct {
	Question              string             `json:"question"`
	Options               []CachedPollOption `json:"options"`
	TotalVoterCount       int                `json:"total_voter_count"`
	IsClosed              bool               `json:"is_closed"`
	IsAnonymous           bool               `json:"is_anonymous"`
	Type                  string             `json:"type"`
	AllowsMultipleAnswers bool               `json:"allows_multiple_answers"`
}

CachedPoll represents a Telegram poll

type CachedPollOption

type CachedPollOption struct {
	Text       string `json:"text"`
	VoterCount int    `json:"voter_count"`
}

CachedPollOption represents a single option in a poll

type ChatInfo

type ChatInfo struct {
	ID       int64  `json:"id,string"`
	Title    string `json:"title"`
	PhotoURL string `json:"photo_url,omitempty"`
}

ChatInfo represents information about a Telegram chat

type EscarBot

type EscarBot struct {
	Bot                   *tgbotapi.BotAPI
	Power                 bool
	LinkDetection         bool
	ChannelForward        bool
	AdminForward          bool
	AutoBan               bool
	Captcha               bool
	CaptchaTimeout        int
	CaptchaMaxRetries     int
	WelcomeMessage        bool
	StatsFeature          bool
	StatsShowNotes        bool
	ChannelID             int64
	GroupID               int64
	AdminID               int64
	LogChannelID          int64
	StatsChatID           int64
	StatsPolynomialDegree int
	BannedWords           []string
	StateMutex            sync.RWMutex
	MaxCacheSize          int
	OnMessageCached       func(CachedMessage) // Callback for when a message is cached
	WelcomeText           string
	WelcomeLinks          string
	WelcomePhoto          string
	CaptchaText           string
	ChatBlacklist         []int64
	EnabledReplacers      map[string]bool
	Cache                 *Cache
}

func NewBot

func NewBot(botToken string, channelId string, groupId string, adminId, logChannelId string) *EscarBot

type FallbackHandler

type FallbackHandler struct {
	// contains filtered or unexported fields
}

func (FallbackHandler) Box

func (h FallbackHandler) Box(txt string, fnt plotfont.Font) (vg.Length, vg.Length, vg.Length)

func (FallbackHandler) Cache

func (h FallbackHandler) Cache() *plotfont.Cache

func (FallbackHandler) Draw

func (h FallbackHandler) Draw(c vg.Canvas, txt string, sty text.Style, pt vg.Point)

func (FallbackHandler) Extents

func (h FallbackHandler) Extents(fnt plotfont.Font) plotfont.Extents

func (FallbackHandler) Lines

func (h FallbackHandler) Lines(s string) []string

type JoinProcessedEntry

type JoinProcessedEntry struct {
	Time      time.Time `json:"time"`
	JoinMsgID int       `json:"join_msg_id"`
	IsBanned  bool      `json:"is_banned"`
}

JoinProcessedEntry represents a join event that was already processed

type MessageHistory

type MessageHistory struct {
	Text     string `json:"text"`
	Caption  string `json:"caption,omitempty"`
	EditDate int64  `json:"edit_date"`
}

MessageHistory represents a previous version of a message

type PendingCaptcha

type PendingCaptcha struct {
	UserID          int64
	UserFirstName   string
	ChatID          int64
	CorrectAnswer   string
	CaptchaMsgID    int
	JoinMsgID       int
	Attempts        int
	ExpirationTimer *time.Timer
}

type ReactionDetail

type ReactionDetail struct {
	User  string `json:"user"`
	Emoji string `json:"emoji"`
}

ReactionDetail represents an individual reaction by a user

type Replacer

type Replacer struct {
	Name   string
	Regex  *regexp.Regexp
	Format string
}

func GetReplacers

func GetReplacers() []Replacer

type StatsRow

type StatsRow struct {
	Date        time.Time
	Subscribers float64
	Note        string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL