Skip to content

Commit

Permalink
implemented sendInvoice (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
roskee committed Nov 22, 2023
1 parent 9a54923 commit d9c068a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ type Bot interface {
// SendDocument is used to send general files.
//
// Note: bots can only send files of any type up to 50 MB in size.

SendDocument(msg entity.MessageEnvelop) (entity.Message, error)
// SendVoice is used to send audio files.
//
Expand Down Expand Up @@ -152,6 +151,9 @@ type Bot interface {
// - If the bot is an administrator of a group, it can delete any message there.
// - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
DeleteMessage(msg envelop.DeleteMessageEnvelop) (bool, error)

// SendInvoice is used to send invoices.
SendInvoice(invoice envelop.SendInvoiceEnvelop) (entity.Message, error)
}

// BotOptions hold the options for the bot
Expand Down
13 changes: 13 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,16 @@ func (b *bot) DeleteMessage(msg envelop.DeleteMessageEnvelop) (bool, error) {

return status, json.Unmarshal(res, &status)
}

func (b *bot) SendInvoice(invoice envelop.SendInvoiceEnvelop) (entity.Message, error) {
res, err := b.SendRawRequest(http.MethodPost, "sendInvoice", func() (io.Reader, BodyOptions, error) {
return GetJSONBody(invoice)
}, SetApplicationJSON)
if err != nil {
return entity.Message{}, err
}

var msg entity.Message

return msg, json.Unmarshal(res, &msg)
}
11 changes: 11 additions & 0 deletions entity/payment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package entity

// LabeledPrice represents a portion of the price for goods or services.
type LabeledPrice struct {
// Label is a label for this portion of the price.
Label string `json:"label,omitempty"`
// Amount is price of the product in the smallest units of the currency
// (integer, not float/double).
// For example, for a price of US$ 1.45 pass amount = 145.
Amount int64 `json:"amount,omitempty"`
}
94 changes: 94 additions & 0 deletions envelop/payment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package envelop

import "github.com/roskee/gotbot/entity"

// SendInvoiceEnvelop is used to send invoices.
type SendInvoiceEnvelop struct {
// ChatID is unique identifier for the target chat.
//
// It is a required field.
ChatID int64 `json:"chat_id,omitempty"`
// MessageThreadID is unique identifier of the target message thread (topic).
MessageThreadID int64 `json:"message_thread_id,omitempty"`
// Title is product name, 1-32 characters.
//
// It is a required field.
Title string `json:"title,omitempty"`
// Description is product description, 1-255 characters.
//
// It is a required field.
Description string `json:"description,omitempty"`
// Payload is bot-defined invoice payload, 1-128 bytes.
//
// It is a required field.
Payload string `json:"payload,omitempty"`
// ProviderToken is payments provider token, obtained via Botfather.
//
// It is a required field.
ProviderToken string `json:"provider_token,omitempty"`
// Currency is three-letter ISO 4217 currency code.
//
// It is a required field.
Currency string `json:"currency,omitempty"`
// Prices is price breakdown, a list of components
// (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.).
//
// It is a required field.
Prices []entity.LabeledPrice `json:"prices,omitempty"`
// MaxTipAmount is the maximum accepted amount for tips
// in the smallest units of the currency (integer, not float/double).
MaxTipAmount int64 `json:"max_tip_amount,omitempty"`
// SuggestedTipAmounts is a JSON-serialized array of suggested amounts of tip
// in the smallest units of the currency (integer, not float/double).
// At most 4 suggested tip amounts can be specified.
// The suggested tip amounts must be positive, passed in a strictly increased order
// and must not exceed max_tip_amount.
SuggestedTipAmounts []int64 `json:"suggested_tip_amounts,omitempty"`
// StartParameter is unique deep-linking parameter.
// If left empty, forwarded copies of the sent message will have a Pay button,
// allowing multiple users to pay directly from the forwarded message,
// using the same invoice.
StartParameter string `json:"start_parameter,omitempty"`
// ProviderData is a JSON-serialized data about the invoice,
// which will be shared with the payment provider.
// A detailed description of required fields should be provided by the payment provider.
// See https://core.telegram.org/bots/api#payments
ProviderData string `json:"provider_data,omitempty"`
// PhotoURL is URL of the product photo for the invoice.
// Can be a photo of the goods or a marketing image for a service.
// People like it better when they see what they are paying for.
PhotoURL string `json:"photo_url,omitempty"`
// PhotoSize is photo size in bytes.
PhotoSize int64 `json:"photo_size,omitempty"`
// PhotoWidth is photo width.
PhotoWidth int64 `json:"photo_width,omitempty"`
// PhotoHeight is photo height.
PhotoHeight int64 `json:"photo_height,omitempty"`
// NeedName is pass True, if you require the user's full name to complete the order.
NeedName bool `json:"need_name,omitempty"`
// NeedPhoneNumber is pass True, if you require the user's phone number to complete the order.
NeedPhoneNumber bool `json:"need_phone_number,omitempty"`
// NeedEmail is pass True, if you require the user's email address to complete the order.
NeedEmail bool `json:"need_email,omitempty"`
// NeedShippingAddress is pass True, if you require the user's shipping address to complete the order.
NeedShippingAddress bool `json:"need_shipping_address,omitempty"`
// SendPhoneNumberToProvider is pass True, if user's phone number should be sent to provider.
SendPhoneNumberToProvider bool `json:"send_phone_number_to_provider,omitempty"`
// SendEmailToProvider is pass True, if user's email address should be sent to provider.
SendEmailToProvider bool `json:"send_email_to_provider,omitempty"`
// IsFlexible is pass True, if the final price depends on the shipping method.
IsFlexible bool `json:"is_flexible,omitempty"`
// DisableNotification is sends the message silently.
// Users will receive a notification with no sound.
DisableNotification bool `json:"disable_notification,omitempty"`
// ProtectContent is pass True, if the content of the message needs to be protected
// from forwarding and saving.
ProtectContent bool `json:"protect_content,omitempty"`
// ReplyToMessageID is if the message is a reply, ID of the original message.
ReplyToMessageID int64 `json:"reply_to_message_id,omitempty"`
// AllowSendingWithoutReply is pass True, if the message should be sent
// even if the specified replied-to message is not found.
AllowSendingWithoutReply bool `json:"allow_sending_without_reply,omitempty"`
// ReplyMarkup is additional interface options.
ReplyMarkup *entity.InlineKeyboardMarkup `json:"reply_markup,omitempty"`
}

0 comments on commit d9c068a

Please sign in to comment.