Skip to content

Commit

Permalink
Add new filters for handling payment-related updates
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Dec 26, 2023
1 parent e73a363 commit d687362
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
10 changes: 7 additions & 3 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func NewContext(update *gotgbot.Update, data map[string]interface{}) *Context {
case update.InlineQuery != nil:
user = &update.InlineQuery.From

case update.ChosenInlineResult != nil:
user = &update.ChosenInlineResult.From

case update.CallbackQuery != nil:
user = &update.CallbackQuery.From

Expand All @@ -74,15 +77,16 @@ func NewContext(update *gotgbot.Update, data map[string]interface{}) *Context {
sender = &gotgbot.Sender{User: user, ChatId: chat.Id}
}

case update.ChosenInlineResult != nil:
user = &update.ChosenInlineResult.From

case update.ShippingQuery != nil:
user = &update.ShippingQuery.From

case update.PreCheckoutQuery != nil:
user = &update.PreCheckoutQuery.From

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

case update.MyChatMember != nil:
user = &update.MyChatMember.From
chat = &update.MyChatMember.Chat
Expand Down
24 changes: 24 additions & 0 deletions ext/handlers/filters/precheckoutquery/precheckoutquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package shippingquery

import (
"strings"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)

func All(_ *gotgbot.PreCheckoutQuery) bool {
return true
}

func FromUserID(id int64) filters.PreCheckoutQuery {
return func(p *gotgbot.PreCheckoutQuery) bool {
return p.From.Id == id
}
}

func HasPayloadPrefix(pre string) filters.PreCheckoutQuery {
return func(p *gotgbot.PreCheckoutQuery) bool {
return strings.HasPrefix(p.InvoicePayload, pre)
}
}
24 changes: 24 additions & 0 deletions ext/handlers/filters/shippingquery/shippingquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package shippingquery

import (
"strings"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)

func All(_ *gotgbot.ShippingQuery) bool {
return true
}

func FromUserID(id int64) filters.ShippingQuery {
return func(p *gotgbot.ShippingQuery) bool {
return p.From.Id == id
}
}

func HasPayloadPrefix(pre string) filters.ShippingQuery {
return func(p *gotgbot.ShippingQuery) bool {
return strings.HasPrefix(p.InvoicePayload, pre)
}
}
4 changes: 3 additions & 1 deletion ext/handlers/filters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import "github.com/PaulSonOfLars/gotgbot/v2"

type (
CallbackQuery func(cq *gotgbot.CallbackQuery) bool
ChatJoinRequest func(cjr *gotgbot.ChatJoinRequest) bool
ChatMember func(u *gotgbot.ChatMemberUpdated) bool
ChosenInlineResult func(cir *gotgbot.ChosenInlineResult) bool
InlineQuery func(iq *gotgbot.InlineQuery) bool
Message func(msg *gotgbot.Message) bool
Poll func(poll *gotgbot.Poll) bool
PollAnswer func(pa *gotgbot.PollAnswer) bool
ChatJoinRequest func(cjr *gotgbot.ChatJoinRequest) bool
PreCheckoutQuery func(pcq *gotgbot.PreCheckoutQuery) bool
ShippingQuery func(sq *gotgbot.ShippingQuery) bool
)
36 changes: 36 additions & 0 deletions ext/handlers/precheckoutquery.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 PreCheckoutQuery struct {
Filter filters.PreCheckoutQuery
Response Response
}

func NewPreCheckoutQuery(f filters.PreCheckoutQuery, r Response) PreCheckoutQuery {
return PreCheckoutQuery{
Filter: f,
Response: r,
}
}

func (r PreCheckoutQuery) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
if ctx.PollAnswer == nil {
return false
}
return r.Filter == nil || r.Filter(ctx.PreCheckoutQuery)
}

func (r PreCheckoutQuery) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
return r.Response(b, ctx)
}

func (r PreCheckoutQuery) Name() string {
return fmt.Sprintf("precheckoutquery_%p", r.Response)
}
36 changes: 36 additions & 0 deletions ext/handlers/shippingquery.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 ShippingQuery struct {
Filter filters.ShippingQuery
Response Response
}

func NewShippingQuery(f filters.ShippingQuery, r Response) ShippingQuery {
return ShippingQuery{
Filter: f,
Response: r,
}
}

func (r ShippingQuery) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
if ctx.PollAnswer == nil {
return false
}
return r.Filter == nil || r.Filter(ctx.ShippingQuery)
}

func (r ShippingQuery) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
return r.Response(b, ctx)
}

func (r ShippingQuery) Name() string {
return fmt.Sprintf("shippingquery_%p", r.Response)
}

0 comments on commit d687362

Please sign in to comment.