Skip to content

MTGo Filters ​

Filters narrow which updates trigger a handler. They compose with And, Or, and Not.

Using Filters ​

Pass filters as the second argument to OnMessage and other registration methods:

go
client.OnMessage(handler, tg.Private)
client.OnMessage(handler, tg.Private.And(tg.Text("hello")))

Built-in Filters ​

Chat Type ​

FilterMatches
PrivateDMs (positive ChatID)
GroupGroup messages (negative ChatID)
ChannelChannel posts
DirectIncoming DMs (private + incoming)
ForumMessages from forum-enabled supergroups
BusinessBusiness connection messages

Message Direction ​

FilterMatches
IncomingNon-outgoing messages
OutgoingMessages sent by the current user
MeOutgoing messages (alias)

Content Type ​

FilterMatches
MediaAny media attachment
HasMediaAlias for Media
PhotoPhoto attachments
VideoVideo attachments
AudioAudio attachments
VoiceVoice notes
VideoNoteRound video notes
StickerStickers
DocumentGeneric file documents
AnimationGIFs/animations
ContactContact cards
LocationStatic location pins
LiveLocationLive updating locations
VenueVenue info
WebPageLink previews
PollPoll messages
DiceDice/animated emoji
GameHTML5 game attachments
StoryForwarded stories
PaidMediaPaid media (Stars)
PaidMessagePaid messages (Stars)
InvoicePayment invoices
SuccessfulPaymentCompleted payment confirmations
GiveawayGiveaway announcements
GiveawayWinnersGiveaway winner announcements
MediaGroupAlbum messages
MediaSpoilerSpoiler-hidden media
SelfDestructionSelf-destructing media (TTL)
ReplyKeyboardMessages with reply keyboard markup
InlineKeyboardMessages with inline keyboard markup
CaptionMedia with caption only

Text Matching ​

FilterMatches
Text("hello")Exact text match
Command("start")/start command
Regex("\\d+")Regex pattern match
AllEvery update

Sender & Chat ​

FilterMatches
User(123, 456)Specific user IDs
Chat(-100123)Specific chat IDs
Topic(42)Specific forum topic
SenderChat(-100)Specific sender chat
BotMessages from bots

Message Properties ​

FilterMatches
ReplyMessages that reply to another
ForwardedForwarded messages
MentionedMessages where you're @mentioned
ViaBotMessages sent via inline bot
PinnedPinned messages
LinkedChannelForwarded from linked channel
GuestMessageGuest users in business chats
ServiceService/system messages

Service Message Filters ​

FilterMatches
NewChatMembersMember join events
LeftChatMemberMember leave events
NewChatTitleTitle change events
NewChatPhotoPhoto change events
DeleteChatPhotoPhoto deletion events
GroupChatCreatedGroup creation events
SupergroupChatCreatedSupergroup creation events
ChannelChatCreatedChannel creation events
MigrateToChatIDGroup-to-supergroup migration
MigrateFromChatIDChannel-from-group migration
PinnedMessagePin notification events
VideoChatStartedVideo chat started
VideoChatEndedVideo chat ended
VideoChatMembersInvitedVideo chat invitations
GameHighScoreGame score updates

Callback & Inline ​

FilterMatches
CallbackData("approve")Exact callback data match
CallbackRegex("^page_\\d+$")Regex on callback data
InlineQueryText("search")Exact inline query text

Raw Update Type Filter ​

FilterMatches
UpdateType[*tg.UpdateUserTyping]()Specific raw update type (use with OnRawUpdate)

Composition ​

Filters compose with three operators:

go
// AND — both must match
tg.Private.And(tg.Text("hello"))

// OR — either matches
tg.Photo.Or(tg.Video)

// NOT — negate a filter
tg.Bot.Not() // non-bot messages

Chain multiple:

go
tg.Private.And(tg.Incoming).And(tg.Command("start"))

Custom Filters ​

Use Create for filters that need client access:

go
tg.Create(func(c *tg.Client, ctx *tg.Context) bool {
    return isAdmin(c, ctx.Message.FromID)
})

Custom Command Filter ​

NewCommand creates a command filter with custom prefixes and case sensitivity:

go
tg.NewCommand(
    []string{"start", "help"},  // commands
    []string{"!", "/"},          // prefixes
    false,                       // case-insensitive
)

Released under the Apache-2.0 License.