Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reactions support. #142

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions ext/handlers/filters/reaction/reaction.go
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random thought: should we be merging this to a single item? How are the other handlers doing it? I'm not sure it makes sense to have both, since only one can trigger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yep, correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you think about merge them and name as FromPeer?
I don't know how Telegram names such things exactly in BotAPI, but in MTProto it's named usually as Peer.

return func(mru *gotgbot.MessageReactionUpdated) bool {
if mru.ActorChat != nil {
return mru.ActorChat.Id == id
}

return false
}
}

func FromChatID(id int64) filters.Reaction {
ti-bone marked this conversation as resolved.
Show resolved Hide resolved
return func(mru *gotgbot.MessageReactionUpdated) bool {
return mru.Chat.Id == id
}
}

func NewReactionIn(reaction string) filters.Reaction {
ti-bone marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
1 change: 1 addition & 0 deletions ext/handlers/filters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
36 changes: 36 additions & 0 deletions ext/handlers/reaction.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading