Skip to content

Commit ed39afb

Browse files
committed
feat: adds payment-related methods and types
1 parent 8e945a9 commit ed39afb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+61391
-101
lines changed

tgbotapi/bot.go renamed to tgbotapi/bot_api.go

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package tgbotapi
55
import (
66
"bytes"
77
"context"
8-
"encoding/json"
98
"errors"
109
"fmt"
1110
"github.com/pquerna/ffjson/ffjson"
@@ -54,22 +53,26 @@ func NewBotAPIWithClient(token string, client *http.Client) *BotAPI {
5453
}
5554
}
5655

57-
// MakeRequestFromChattable makes request from chattable TODO: Is duplicate of Send()?
58-
func (bot *BotAPI) MakeRequestFromChattable(m Chattable) (resp APIResponse, err error) { //
59-
values, err := m.Values()
60-
if err != nil {
56+
// MakeRequestFromMessageWithValues makes request from WithValues
57+
func (bot *BotAPI) MakeRequestFromMessageWithValues(method string, m WithValues) (resp APIResponse, err error) { //
58+
var values url.Values
59+
if values, err = m.Values(); err != nil {
6160
return resp, err
6261
}
63-
return bot.MakeRequest(m.method(), values)
62+
return bot.MakeRequest(method, values)
6463
}
6564

66-
// MakeRequest makes a request to a specific endpoint with our token.
67-
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (apiResp APIResponse, err error) {
68-
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint)
65+
// MakeRequestFromChattable makes request from chattable TODO: Is duplicate of Send()?
66+
func (bot *BotAPI) MakeRequestFromChattable(m Sendable) (resp APIResponse, err error) { //
67+
return bot.MakeRequestFromMessageWithValues(m.TelegramMethod(), m)
68+
}
6969

70-
var resp *http.Response
70+
// SendRequest sends a request to a specific endpoint with our token and reads response.
71+
func (bot *BotAPI) MakeRequest(telegramMethod string, params url.Values) (apiResp APIResponse, err error) {
72+
method := fmt.Sprintf(APIEndpoint, bot.Token, telegramMethod)
7173

7274
var hadDeadlineExceeded bool
75+
var resp *http.Response
7376

7477
for i := 1; i <= 2; i++ { // TODO: Should this be in bots framework?
7578
if resp, err = bot.Client.PostForm(method, params); err != nil {
@@ -103,7 +106,7 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (apiResp APIR
103106
}
104107
}
105108
apiResp = APIResponse{
106-
Result: json.RawMessage(body),
109+
Result: body,
107110
}
108111
if resp.StatusCode >= 300 {
109112
apiResp.ErrorCode = resp.StatusCode
@@ -117,36 +120,35 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (apiResp APIR
117120
}
118121

119122
if err != nil {
120-
return APIResponse{Ok: false, Result: json.RawMessage(body)}, fmt.Errorf("%v: %s: %w", "POST", method, err)
123+
return APIResponse{Ok: false, Result: body}, fmt.Errorf("%v: %s: %w", "POST", method, err)
121124
}
122125

123126
logRequestAndResponse := func() {
124127
if bot.c != nil {
125-
logus.Debugf(bot.c, "Request to Telegram API: %v => %v", endpoint, params)
126-
logus.Debugf(bot.c, "Telegram API response: %v", string(body))
128+
logus.Debugf(bot.c, "Request to Telegram API: %v => %v", telegramMethod, params)
129+
logus.Debugf(bot.c, "Telegram API response: %v", string(apiResp.Result))
127130
}
128131
}
129132

130-
//logRequestAndResponse()
131-
132-
if err = ffjson.Unmarshal(body, &apiResp); err != nil {
133+
if err = ffjson.Unmarshal(apiResp.Result, &apiResp); err != nil {
133134
logRequestAndResponse()
134-
return apiResp, fmt.Errorf("telegram API returned non JSON response or unknown JSON: %w:\n%s", err, string(body))
135+
return apiResp, fmt.Errorf("telegram API returned non JSON response or unknown JSON: %w:\n%s", err, string(apiResp.Result))
135136
} else if !apiResp.Ok {
136137
logRequestAndResponse()
137138
if hadDeadlineExceeded && apiResp.ErrorCode == 400 && strings.Contains(apiResp.Description, "message is not modified") {
138139
return apiResp, nil
139140
}
140141
return apiResp, apiResp
141142
}
143+
142144
return apiResp, nil
143145
}
144146

145147
func (bot *BotAPI) DeleteMessage(chatID string, messageID int) (apiResp APIResponse, err error) {
146148
return bot.MakeRequest("deleteMessage", url.Values{"chat_id": {chatID}, "message_id": {strconv.Itoa(messageID)}})
147149
}
148150

149-
// makeMessageRequest makes a request to a method that returns a Message.
151+
// makeMessageRequest makes a request to a TelegramMethod that returns a Message.
150152
func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Message, error) {
151153
resp, err := bot.MakeRequest(endpoint, params)
152154
var message Message
@@ -279,7 +281,7 @@ func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) {
279281

280282
// GetMe fetches the currently authenticated bot.
281283
//
282-
// This method is called upon creation to validate the token,
284+
// This TelegramMethod is called upon creation to validate the token,
283285
// and so you may get this data from BotAPI.Self without the need for
284286
// another request.
285287
func (bot *BotAPI) GetMe() (User, error) {
@@ -323,10 +325,10 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool {
323325
return strings.Contains(message.Text, "@"+bot.Self.UserName)
324326
}
325327

326-
// Send will send a Chattable item to Telegram.
328+
// Send will send a Sendable item to Telegram.
327329
//
328-
// It requires the Chattable to send.
329-
func (bot *BotAPI) Send(c Chattable) (Message, error) {
330+
// It requires the Sendable to send.
331+
func (bot *BotAPI) Send(c Sendable) (Message, error) {
330332
switch t := c.(type) {
331333
case Fileable:
332334
return bot.sendFile(t)
@@ -390,20 +392,20 @@ func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error
390392
// a new file, then sends it as needed.
391393
func (bot *BotAPI) sendFile(config Fileable) (Message, error) {
392394
if config.useExistingFile() {
393-
return bot.sendExisting(config.method(), config)
395+
return bot.sendExisting(config.TelegramMethod(), config)
394396
}
395397

396-
return bot.uploadAndSend(config.method(), config)
398+
return bot.uploadAndSend(config.TelegramMethod(), config)
397399
}
398400

399-
// sendChattable sends a Chattable.
400-
func (bot *BotAPI) sendChattable(config Chattable) (Message, error) {
401+
// sendChattable sends a Sendable.
402+
func (bot *BotAPI) sendChattable(config Sendable) (Message, error) {
401403
v, err := config.Values()
402404
if err != nil {
403405
return Message{}, err
404406
}
405407

406-
return bot.makeMessageRequest(config.method(), v)
408+
return bot.makeMessageRequest(config.TelegramMethod(), v)
407409
}
408410

409411
// GetUserProfilePhotos gets a user's profile photos.
@@ -496,7 +498,7 @@ func (bot *BotAPI) GetUpdates(config *UpdateConfig) ([]Update, error) {
496498

497499
// RemoveWebhook unsets the webhook.
498500
func (bot *BotAPI) RemoveWebhook() (APIResponse, error) {
499-
return bot.MakeRequest("setWebhook", url.Values{})
501+
return bot.MakeRequest("removeWebhook", url.Values{})
500502
}
501503

502504
// SetWebhook sets a webhook.
@@ -649,3 +651,38 @@ func (bot *BotAPI) UnbanChatMember(config ChatMemberConfig) (APIResponse, error)
649651

650652
return bot.MakeRequest("unbanChatMember", v)
651653
}
654+
655+
func (bot *BotAPI) SetDescription(config SetMyDescription) (APIResponse, error) {
656+
return bot.MakeRequestFromChattable(config)
657+
}
658+
659+
func (bot *BotAPI) SetShortDescription(config SetMyShortDescription) (APIResponse, error) {
660+
return bot.MakeRequestFromChattable(config)
661+
}
662+
663+
func (bot *BotAPI) SetCommands(config SetMyCommandsConfig) (APIResponse, error) {
664+
return bot.MakeRequestFromChattable(config)
665+
}
666+
667+
func (bot *BotAPI) GetCommands(ctx context.Context, config GetMyCommandsConfig) (commands []TelegramBotCommand, err error) {
668+
err = bot.SendCustomMessage(ctx, config, &commands)
669+
return
670+
}
671+
672+
func (bot *BotAPI) SendCustomMessage(ctx context.Context, config Sendable, result any) (err error) {
673+
var values url.Values
674+
if values, err = config.Values(); err != nil {
675+
return
676+
}
677+
telegramMethod := config.TelegramMethod()
678+
var apiResponse APIResponse
679+
apiResponse, err = bot.MakeRequest(telegramMethod, values)
680+
if err != nil {
681+
return
682+
}
683+
if err = ffjson.Unmarshal(apiResponse.Result, &result); err != nil {
684+
err = fmt.Errorf("failed to unmarshal telegram response to type %T: %s: %w", result, string(apiResponse.Result), err)
685+
return
686+
}
687+
return
688+
}
File renamed without changes.

0 commit comments

Comments
 (0)