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 additional GetSender() helpers for new types #150

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 2 additions & 6 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ func NewContext(update *gotgbot.Update, data map[string]interface{}) *Context {
case update.MessageReaction != nil:
user = update.MessageReaction.User
chat = &update.MessageReaction.Chat
sender = &gotgbot.Sender{
User: update.MessageReaction.User,
Chat: update.MessageReaction.ActorChat,
ChatId: update.MessageReaction.Chat.Id,
}
sender = update.MessageReaction.GetSender()

case update.MessageReactionCount != nil:
chat = &update.MessageReactionCount.Chat
Expand Down Expand Up @@ -110,7 +106,7 @@ func NewContext(update *gotgbot.Update, data map[string]interface{}) *Context {

case update.PollAnswer != nil:
user = update.PollAnswer.User
sender = &gotgbot.Sender{User: update.PollAnswer.User, Chat: update.PollAnswer.VoterChat}
sender = update.PollAnswer.GetSender()

case update.MyChatMember != nil:
user = &update.MyChatMember.From
Expand Down
2 changes: 1 addition & 1 deletion ext/handlers/filters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +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
Reaction func(mru *gotgbot.MessageReactionUpdated) bool
)
17 changes: 17 additions & 0 deletions sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ func (m Message) GetSender() *Sender {
}
}

// GetSender populates the relevant fields of a Sender struct given a reaction.
func (mru MessageReactionUpdated) GetSender() *Sender {
return &Sender{
User: mru.User,
Chat: mru.ActorChat,
ChatId: mru.Chat.Id,
}
}

// GetSender populates the relevant fields of a Sender struct given a poll answer.
func (pa PollAnswer) GetSender() *Sender {
return &Sender{
User: pa.User,
Chat: pa.VoterChat,
}
}

// Id determines the sender ID.
// When a message is being sent by a chat/channel, telegram usually populates the User field with dummy values.
// For this reason, we prefer to return the Chat.Id if it is available, rather than a dummy User.Id.
Expand Down
Loading