|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +// AnswerShippingQuery replies to a shipping query. When ok is true, provide the |
| 11 | +// available shipping options; otherwise pass an error message via |
| 12 | +// WithShippingError describing why the order can't be shipped. |
| 13 | +func (b *Bot) AnswerShippingQuery(ctx context.Context, shippingQueryID string, ok bool, opts ...ShippingAnswerOption) error { |
| 14 | + var cfg shippingAnswerConfig |
| 15 | + for _, o := range opts { |
| 16 | + o(&cfg) |
| 17 | + } |
| 18 | + |
| 19 | + queryID, err := strconv.ParseInt(shippingQueryID, 10, 64) |
| 20 | + if err != nil { |
| 21 | + return &Error{Code: 400, Description: "Bad Request: invalid shipping query id"} |
| 22 | + } |
| 23 | + |
| 24 | + req := &tg.MessagesSetBotShippingResultsRequest{QueryID: queryID} |
| 25 | + if ok { |
| 26 | + req.ShippingOptions = shippingOptionsToTg(cfg.options) |
| 27 | + } else { |
| 28 | + req.Error = cfg.errorMessage |
| 29 | + } |
| 30 | + |
| 31 | + if _, err := b.raw.MessagesSetBotShippingResults(ctx, req); err != nil { |
| 32 | + return asAPIError(err) |
| 33 | + } |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// ShippingAnswerOption configures an AnswerShippingQuery call. |
| 38 | +type ShippingAnswerOption func(*shippingAnswerConfig) |
| 39 | + |
| 40 | +type shippingAnswerConfig struct { |
| 41 | + options []ShippingOption |
| 42 | + errorMessage string |
| 43 | +} |
| 44 | + |
| 45 | +// WithShippingOptions sets the available shipping options (required when ok). |
| 46 | +func WithShippingOptions(options ...ShippingOption) ShippingAnswerOption { |
| 47 | + return func(c *shippingAnswerConfig) { c.options = options } |
| 48 | +} |
| 49 | + |
| 50 | +// WithShippingError sets the human-readable reason shipping is impossible (used |
| 51 | +// when ok is false). |
| 52 | +func WithShippingError(message string) ShippingAnswerOption { |
| 53 | + return func(c *shippingAnswerConfig) { c.errorMessage = message } |
| 54 | +} |
| 55 | + |
| 56 | +// AnswerPreCheckoutQuery replies to a pre-checkout query. When ok is false, |
| 57 | +// provide a human-readable reason via WithPreCheckoutError. |
| 58 | +func (b *Bot) AnswerPreCheckoutQuery(ctx context.Context, preCheckoutQueryID string, ok bool, opts ...PreCheckoutAnswerOption) error { |
| 59 | + var cfg preCheckoutAnswerConfig |
| 60 | + for _, o := range opts { |
| 61 | + o(&cfg) |
| 62 | + } |
| 63 | + |
| 64 | + queryID, err := strconv.ParseInt(preCheckoutQueryID, 10, 64) |
| 65 | + if err != nil { |
| 66 | + return &Error{Code: 400, Description: "Bad Request: invalid pre-checkout query id"} |
| 67 | + } |
| 68 | + |
| 69 | + req := &tg.MessagesSetBotPrecheckoutResultsRequest{ |
| 70 | + Success: ok, |
| 71 | + QueryID: queryID, |
| 72 | + } |
| 73 | + if !ok { |
| 74 | + req.Error = cfg.errorMessage |
| 75 | + } |
| 76 | + |
| 77 | + if _, err := b.raw.MessagesSetBotPrecheckoutResults(ctx, req); err != nil { |
| 78 | + return asAPIError(err) |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// PreCheckoutAnswerOption configures an AnswerPreCheckoutQuery call. |
| 84 | +type PreCheckoutAnswerOption func(*preCheckoutAnswerConfig) |
| 85 | + |
| 86 | +type preCheckoutAnswerConfig struct { |
| 87 | + errorMessage string |
| 88 | +} |
| 89 | + |
| 90 | +// WithPreCheckoutError sets the reason the checkout can't proceed (used when ok |
| 91 | +// is false). |
| 92 | +func WithPreCheckoutError(message string) PreCheckoutAnswerOption { |
| 93 | + return func(c *preCheckoutAnswerConfig) { c.errorMessage = message } |
| 94 | +} |
| 95 | + |
| 96 | +// shippingOptionsToTg converts Bot API shipping options to MTProto. |
| 97 | +func shippingOptionsToTg(options []ShippingOption) []tg.ShippingOption { |
| 98 | + out := make([]tg.ShippingOption, 0, len(options)) |
| 99 | + for _, o := range options { |
| 100 | + prices := make([]tg.LabeledPrice, 0, len(o.Prices)) |
| 101 | + for _, p := range o.Prices { |
| 102 | + prices = append(prices, tg.LabeledPrice{Label: p.Label, Amount: int64(p.Amount)}) |
| 103 | + } |
| 104 | + out = append(out, tg.ShippingOption{ID: o.ID, Title: o.Title, Prices: prices}) |
| 105 | + } |
| 106 | + return out |
| 107 | +} |
0 commit comments