-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reactions support to handlers (#142)
* Add reactions support. * Rename FromUserID(also merged with FromAnonymousChatID) to FromPeer, rename FromChatID to ChatID, rename New(Old)ReactionIn to New(Old)ReactionEmoji. --------- Co-authored-by: Paul Larsen <[email protected]>
- Loading branch information
1 parent
30e81e5
commit c18b68e
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package reaction | ||
|
||
import ( | ||
"github.com/PaulSonOfLars/gotgbot/v2" | ||
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters" | ||
) | ||
|
||
func All(_ *gotgbot.MessageReactionUpdated) bool { | ||
return true | ||
} | ||
|
||
func FromPeer(id int64) filters.Reaction { | ||
return func(mru *gotgbot.MessageReactionUpdated) bool { | ||
if mru.User != nil { | ||
return mru.User.Id == id | ||
} | ||
|
||
if mru.ActorChat != nil { | ||
return mru.ActorChat.Id == id | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
func ChatID(id int64) filters.Reaction { | ||
return func(mru *gotgbot.MessageReactionUpdated) bool { | ||
return mru.Chat.Id == id | ||
} | ||
} | ||
|
||
func NewReactionEmoji(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 OldReactionEmoji(reaction string) filters.Reaction { | ||
return func(mru *gotgbot.MessageReactionUpdated) bool { | ||
for _, r := range mru.OldReaction { | ||
if r.MergeReactionType().Emoji == reaction { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |