|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/telegram/message" |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +// InvoiceParams describes an invoice to send with SendInvoice. |
| 11 | +type InvoiceParams struct { |
| 12 | + Title string `json:"title"` |
| 13 | + Description string `json:"description"` |
| 14 | + Payload string `json:"payload"` // bot-defined, not shown to the user |
| 15 | + ProviderToken string `json:"provider_token"` |
| 16 | + Currency string `json:"currency"` |
| 17 | + Prices []LabeledPrice `json:"prices"` |
| 18 | + |
| 19 | + MaxTipAmount int `json:"max_tip_amount,omitempty"` |
| 20 | + SuggestedTipAmounts []int `json:"suggested_tip_amounts,omitempty"` |
| 21 | + StartParameter string `json:"start_parameter,omitempty"` |
| 22 | + ProviderData string `json:"provider_data,omitempty"` // JSON for the payment provider |
| 23 | + |
| 24 | + PhotoURL string `json:"photo_url,omitempty"` |
| 25 | + PhotoSize int `json:"photo_size,omitempty"` |
| 26 | + PhotoWidth int `json:"photo_width,omitempty"` |
| 27 | + PhotoHeight int `json:"photo_height,omitempty"` |
| 28 | + |
| 29 | + NeedName bool `json:"need_name,omitempty"` |
| 30 | + NeedPhoneNumber bool `json:"need_phone_number,omitempty"` |
| 31 | + NeedEmail bool `json:"need_email,omitempty"` |
| 32 | + NeedShippingAddress bool `json:"need_shipping_address,omitempty"` |
| 33 | + |
| 34 | + SendPhoneNumberToProvider bool `json:"send_phone_number_to_provider,omitempty"` |
| 35 | + SendEmailToProvider bool `json:"send_email_to_provider,omitempty"` |
| 36 | + IsFlexible bool `json:"is_flexible,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +// SendInvoice sends an invoice. |
| 40 | +func (b *Bot) SendInvoice(ctx context.Context, chat ChatID, params InvoiceParams, opts ...SendOption) (*Message, error) { |
| 41 | + var cfg sendConfig |
| 42 | + for _, o := range opts { |
| 43 | + o(&cfg) |
| 44 | + } |
| 45 | + |
| 46 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + suggested := make([]int64, len(params.SuggestedTipAmounts)) |
| 52 | + for i, a := range params.SuggestedTipAmounts { |
| 53 | + suggested[i] = int64(a) |
| 54 | + } |
| 55 | + invoice := tg.Invoice{ |
| 56 | + NameRequested: params.NeedName, |
| 57 | + PhoneRequested: params.NeedPhoneNumber, |
| 58 | + EmailRequested: params.NeedEmail, |
| 59 | + ShippingAddressRequested: params.NeedShippingAddress, |
| 60 | + Flexible: params.IsFlexible, |
| 61 | + PhoneToProvider: params.SendPhoneNumberToProvider, |
| 62 | + EmailToProvider: params.SendEmailToProvider, |
| 63 | + Currency: params.Currency, |
| 64 | + Prices: pricesToTg(params.Prices), |
| 65 | + MaxTipAmount: int64(params.MaxTipAmount), |
| 66 | + SuggestedTipAmounts: suggested, |
| 67 | + } |
| 68 | + |
| 69 | + media := &tg.InputMediaInvoice{ |
| 70 | + Title: params.Title, |
| 71 | + Description: params.Description, |
| 72 | + Invoice: invoice, |
| 73 | + Payload: []byte(params.Payload), |
| 74 | + Provider: params.ProviderToken, |
| 75 | + StartParam: params.StartParameter, |
| 76 | + } |
| 77 | + if params.ProviderData != "" { |
| 78 | + media.ProviderData = tg.DataJSON{Data: params.ProviderData} |
| 79 | + } |
| 80 | + if params.PhotoURL != "" { |
| 81 | + media.SetPhoto(tg.InputWebDocument{ |
| 82 | + URL: params.PhotoURL, |
| 83 | + Size: params.PhotoSize, |
| 84 | + MimeType: "image/jpeg", |
| 85 | + Attributes: []tg.DocumentAttributeClass{ |
| 86 | + &tg.DocumentAttributeImageSize{W: params.PhotoWidth, H: params.PhotoHeight}, |
| 87 | + }, |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + builder := &b.sender.To(peer).Builder |
| 92 | + builder, err = b.applySendConfig(builder, cfg) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + resp, err := builder.Media(ctx, message.Media(media)) |
| 98 | + return b.sentMessage(ctx, peer, resp, err) |
| 99 | +} |
| 100 | + |
| 101 | +// pricesToTg converts Bot API labeled prices to MTProto. |
| 102 | +func pricesToTg(prices []LabeledPrice) []tg.LabeledPrice { |
| 103 | + out := make([]tg.LabeledPrice, 0, len(prices)) |
| 104 | + for _, p := range prices { |
| 105 | + out = append(out, tg.LabeledPrice{Label: p.Label, Amount: int64(p.Amount)}) |
| 106 | + } |
| 107 | + return out |
| 108 | +} |
0 commit comments