From c568b85dd182ac42c46c91aa36a6301b6caf47c3 Mon Sep 17 00:00:00 2001 From: Ti Bone Date: Thu, 15 Feb 2024 22:53:34 +0100 Subject: [PATCH] Add reactions support. --- ext/handlers/filters/reaction/reaction.go | 60 +++++++++++++++++++++++ ext/handlers/filters/types.go | 1 + ext/handlers/reaction.go | 36 ++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 ext/handlers/filters/reaction/reaction.go create mode 100644 ext/handlers/reaction.go diff --git a/ext/handlers/filters/reaction/reaction.go b/ext/handlers/filters/reaction/reaction.go new file mode 100644 index 00000000..020ca8c0 --- /dev/null +++ b/ext/handlers/filters/reaction/reaction.go @@ -0,0 +1,60 @@ +package reaction + +import ( + "github.com/PaulSonOfLars/gotgbot/v2" + "github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters" +) + +func All(_ *gotgbot.MessageReactionUpdated) bool { + return true +} + +func FromUserID(id int64) filters.Reaction { + return func(mru *gotgbot.MessageReactionUpdated) bool { + if mru.User != nil { + return mru.User.Id == id + } + + return false + } +} + +func FromAnonymousChatID(id int64) filters.Reaction { + return func(mru *gotgbot.MessageReactionUpdated) bool { + if mru.ActorChat != nil { + return mru.ActorChat.Id == id + } + + return false + } +} + +func FromChatID(id int64) filters.Reaction { + return func(mru *gotgbot.MessageReactionUpdated) bool { + return mru.Chat.Id == id + } +} + +func NewReactionIn(reaction string) filters.Reaction { + return func(mru *gotgbot.MessageReactionUpdated) bool { + for _, r := range mru.NewReaction { + if r.MergeReactionType().Emoji == reaction { + return true + } + } + + return false + } +} + +func OldReactionIn(reaction string) filters.Reaction { + return func(mru *gotgbot.MessageReactionUpdated) bool { + for _, r := range mru.OldReaction { + if r.MergeReactionType().Emoji == reaction { + return true + } + } + + return false + } +} diff --git a/ext/handlers/filters/types.go b/ext/handlers/filters/types.go index 775c2996..14c10352 100644 --- a/ext/handlers/filters/types.go +++ b/ext/handlers/filters/types.go @@ -13,4 +13,5 @@ type ( PollAnswer func(pa *gotgbot.PollAnswer) bool PreCheckoutQuery func(pcq *gotgbot.PreCheckoutQuery) bool ShippingQuery func(sq *gotgbot.ShippingQuery) bool + Reaction func(mra *gotgbot.MessageReactionUpdated) bool ) diff --git a/ext/handlers/reaction.go b/ext/handlers/reaction.go new file mode 100644 index 00000000..7cbb489a --- /dev/null +++ b/ext/handlers/reaction.go @@ -0,0 +1,36 @@ +package handlers + +import ( + "fmt" + + "github.com/PaulSonOfLars/gotgbot/v2" + "github.com/PaulSonOfLars/gotgbot/v2/ext" + "github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters" +) + +type Reaction struct { + Filter filters.Reaction + Response Response +} + +func NewReaction(f filters.Reaction, r Response) Reaction { + return Reaction{ + Filter: f, + Response: r, + } +} + +func (r Reaction) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool { + if ctx.MessageReaction == nil { + return false + } + return r.Filter == nil || r.Filter(ctx.MessageReaction) +} + +func (r Reaction) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error { + return r.Response(b, ctx) +} + +func (r Reaction) Name() string { + return fmt.Sprintf("reaction_%p", r.Response) +}