Skip to content

Commit

Permalink
Add additional sender helpers (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars authored Feb 18, 2024
1 parent b30ff7d commit 85aad40
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
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

0 comments on commit 85aad40

Please sign in to comment.