@@ -5,7 +5,6 @@ package tgbotapi
5
5
import (
6
6
"bytes"
7
7
"context"
8
- "encoding/json"
9
8
"errors"
10
9
"fmt"
11
10
"github.com/pquerna/ffjson/ffjson"
@@ -54,22 +53,26 @@ func NewBotAPIWithClient(token string, client *http.Client) *BotAPI {
54
53
}
55
54
}
56
55
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 {
61
60
return resp , err
62
61
}
63
- return bot .MakeRequest (m . method () , values )
62
+ return bot .MakeRequest (method , values )
64
63
}
65
64
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
+ }
69
69
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 )
71
73
72
74
var hadDeadlineExceeded bool
75
+ var resp * http.Response
73
76
74
77
for i := 1 ; i <= 2 ; i ++ { // TODO: Should this be in bots framework?
75
78
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
103
106
}
104
107
}
105
108
apiResp = APIResponse {
106
- Result : json . RawMessage ( body ) ,
109
+ Result : body ,
107
110
}
108
111
if resp .StatusCode >= 300 {
109
112
apiResp .ErrorCode = resp .StatusCode
@@ -117,36 +120,35 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (apiResp APIR
117
120
}
118
121
119
122
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 )
121
124
}
122
125
123
126
logRequestAndResponse := func () {
124
127
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 ))
127
130
}
128
131
}
129
132
130
- //logRequestAndResponse()
131
-
132
- if err = ffjson .Unmarshal (body , & apiResp ); err != nil {
133
+ if err = ffjson .Unmarshal (apiResp .Result , & apiResp ); err != nil {
133
134
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 ))
135
136
} else if ! apiResp .Ok {
136
137
logRequestAndResponse ()
137
138
if hadDeadlineExceeded && apiResp .ErrorCode == 400 && strings .Contains (apiResp .Description , "message is not modified" ) {
138
139
return apiResp , nil
139
140
}
140
141
return apiResp , apiResp
141
142
}
143
+
142
144
return apiResp , nil
143
145
}
144
146
145
147
func (bot * BotAPI ) DeleteMessage (chatID string , messageID int ) (apiResp APIResponse , err error ) {
146
148
return bot .MakeRequest ("deleteMessage" , url.Values {"chat_id" : {chatID }, "message_id" : {strconv .Itoa (messageID )}})
147
149
}
148
150
149
- // makeMessageRequest makes a request to a method that returns a Message.
151
+ // makeMessageRequest makes a request to a TelegramMethod that returns a Message.
150
152
func (bot * BotAPI ) makeMessageRequest (endpoint string , params url.Values ) (Message , error ) {
151
153
resp , err := bot .MakeRequest (endpoint , params )
152
154
var message Message
@@ -279,7 +281,7 @@ func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) {
279
281
280
282
// GetMe fetches the currently authenticated bot.
281
283
//
282
- // This method is called upon creation to validate the token,
284
+ // This TelegramMethod is called upon creation to validate the token,
283
285
// and so you may get this data from BotAPI.Self without the need for
284
286
// another request.
285
287
func (bot * BotAPI ) GetMe () (User , error ) {
@@ -323,10 +325,10 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool {
323
325
return strings .Contains (message .Text , "@" + bot .Self .UserName )
324
326
}
325
327
326
- // Send will send a Chattable item to Telegram.
328
+ // Send will send a Sendable item to Telegram.
327
329
//
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 ) {
330
332
switch t := c .(type ) {
331
333
case Fileable :
332
334
return bot .sendFile (t )
@@ -390,20 +392,20 @@ func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error
390
392
// a new file, then sends it as needed.
391
393
func (bot * BotAPI ) sendFile (config Fileable ) (Message , error ) {
392
394
if config .useExistingFile () {
393
- return bot .sendExisting (config .method (), config )
395
+ return bot .sendExisting (config .TelegramMethod (), config )
394
396
}
395
397
396
- return bot .uploadAndSend (config .method (), config )
398
+ return bot .uploadAndSend (config .TelegramMethod (), config )
397
399
}
398
400
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 ) {
401
403
v , err := config .Values ()
402
404
if err != nil {
403
405
return Message {}, err
404
406
}
405
407
406
- return bot .makeMessageRequest (config .method (), v )
408
+ return bot .makeMessageRequest (config .TelegramMethod (), v )
407
409
}
408
410
409
411
// GetUserProfilePhotos gets a user's profile photos.
@@ -496,7 +498,7 @@ func (bot *BotAPI) GetUpdates(config *UpdateConfig) ([]Update, error) {
496
498
497
499
// RemoveWebhook unsets the webhook.
498
500
func (bot * BotAPI ) RemoveWebhook () (APIResponse , error ) {
499
- return bot .MakeRequest ("setWebhook " , url.Values {})
501
+ return bot .MakeRequest ("removeWebhook " , url.Values {})
500
502
}
501
503
502
504
// SetWebhook sets a webhook.
@@ -649,3 +651,38 @@ func (bot *BotAPI) UnbanChatMember(config ChatMemberConfig) (APIResponse, error)
649
651
650
652
return bot .MakeRequest ("unbanChatMember" , v )
651
653
}
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
+ }
0 commit comments