From 9b7184fa7951958c72a6a0fcc1e456065bc829fa Mon Sep 17 00:00:00 2001 From: scnace Date: Mon, 8 Jan 2018 02:10:50 +0800 Subject: [PATCH 1/9] check telegram webhook tls handshake --- README.md | 8 +++++++- bot_test.go | 24 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 266f4ed..d9a6873 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,13 @@ func main() { if err != nil { log.Fatal(err) } - + info, err := bot.GetWebhookInfo() + if err != nil { + log.Fatal(err) + } + if info.LastErrorDate != 0 { + log.Printf("[Telegram callback failed]%s", info.LastErrorMessage) + } updates := bot.ListenForWebhook("/" + bot.Token) go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil) diff --git a/bot_test.go b/bot_test.go index 0fb0855..5a3057d 100644 --- a/bot_test.go +++ b/bot_test.go @@ -467,7 +467,13 @@ func TestSetWebhookWithCert(t *testing.T) { t.Error(err) t.Fail() } - + info, err := bot.GetWebhookInfo() + if err != nil { + t.Error(err) + } + if info.LastErrorDate != 0 { + t.Errorf("[Telegram callback failed]%s", info.LastErrorMessage) + } bot.RemoveWebhook() } @@ -484,7 +490,13 @@ func TestSetWebhookWithoutCert(t *testing.T) { t.Error(err) t.Fail() } - + info, err := bot.GetWebhookInfo() + if err != nil { + t.Error(err) + } + if info.LastErrorDate != 0 { + t.Errorf("[Telegram callback failed]%s", info.LastErrorMessage) + } bot.RemoveWebhook() } @@ -549,7 +561,13 @@ func ExampleNewWebhook() { if err != nil { log.Fatal(err) } - + info, err := bot.GetWebhookInfo() + if err != nil { + log.Fatal(err) + } + if info.LastErrorDate != 0 { + log.Printf("[Telegram callback failed]%s", info.LastErrorMessage) + } updates := bot.ListenForWebhook("/" + bot.Token) go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil) From 3b0c0317300ecb967352a7ca2aa7a42a8822ab50 Mon Sep 17 00:00:00 2001 From: Lim Ming Wei Date: Tue, 9 Jan 2018 11:51:17 +0800 Subject: [PATCH 2/9] minor typo in helpers.go --- helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.go b/helpers.go index 132d957..f5800f4 100644 --- a/helpers.go +++ b/helpers.go @@ -641,7 +641,7 @@ func NewCallbackWithAlert(id, text string) CallbackConfig { } } -// NewInvoice created a new Invoice request to the user. +// NewInvoice creates a new Invoice request to the user. func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices *[]LabeledPrice) InvoiceConfig { return InvoiceConfig{ BaseChat: BaseChat{ChatID: chatID}, From f8145e3a68e7bf0bc5ddb308709ddbb839606422 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Thu, 11 Jan 2018 22:41:45 +0100 Subject: [PATCH 3/9] reply_markup is optional for InlineQueryResultGame --- types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types.go b/types.go index bef68b8..4a58287 100644 --- a/types.go +++ b/types.go @@ -657,7 +657,7 @@ type InlineQueryResultGame struct { Type string `json:"type"` ID string `json:"id"` GameShortName string `json:"game_short_name"` - ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup"` + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` } // ChosenInlineResult is an inline query result chosen by a User From 9e8d16e1a8527e9fbb76b2f40a221d3a244055c4 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Fri, 12 Jan 2018 12:12:32 +0100 Subject: [PATCH 4/9] make ChatID int64 for SetGameScoreConfig It's int64 everywhere else. --- configs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configs.go b/configs.go index c0293ce..6c12d64 100644 --- a/configs.go +++ b/configs.go @@ -676,7 +676,7 @@ type SetGameScoreConfig struct { Score int Force bool DisableEditMessage bool - ChatID int + ChatID int64 ChannelUsername string MessageID int InlineMessageID string @@ -689,7 +689,7 @@ func (config SetGameScoreConfig) values() (url.Values, error) { v.Add("score", strconv.Itoa(config.Score)) if config.InlineMessageID == "" { if config.ChannelUsername == "" { - v.Add("chat_id", strconv.Itoa(config.ChatID)) + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) } else { v.Add("chat_id", config.ChannelUsername) } From 6e69f99d113a3ac6537961e7ea3e1b1160f688eb Mon Sep 17 00:00:00 2001 From: Oleksandr Savchuk Date: Sat, 3 Mar 2018 20:20:03 +0200 Subject: [PATCH 5/9] add setChatTitle and setChatDescription methods --- bot.go | 24 ++++++++++++++++++++++++ configs.go | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/bot.go b/bot.go index e201944..48d729b 100644 --- a/bot.go +++ b/bot.go @@ -898,3 +898,27 @@ func (bot *BotAPI) UnpinChatMessage(config UnpinChatMessageConfig) (APIResponse, return bot.MakeRequest(config.method(), v) } + +// SetChatTitle change title of chat. +func (bot *BotAPI) SetChatTitle(config SetChatTitleConfig) (APIResponse, error) { + v, err := config.values() + if err != nil { + return APIResponse{}, err + } + + bot.debugLog(config.method(), v, nil) + + return bot.MakeRequest(config.method(), v) +} + +// SetChatDescription change description of chat. +func (bot *BotAPI) SetChatDescription(config SetChatDescriptionConfig) (APIResponse, error) { + v, err := config.values() + if err != nil { + return APIResponse{}, err + } + + bot.debugLog(config.method(), v, nil) + + return bot.MakeRequest(config.method(), v) +} diff --git a/configs.go b/configs.go index 6c12d64..e16cbbb 100644 --- a/configs.go +++ b/configs.go @@ -1038,8 +1038,8 @@ func (config DeleteMessageConfig) values() (url.Values, error) { // PinChatMessageConfig contains information of a message in a chat to pin. type PinChatMessageConfig struct { - ChatID int64 - MessageID int + ChatID int64 + MessageID int DisableNotification bool } @@ -1072,4 +1072,42 @@ func (config UnpinChatMessageConfig) values() (url.Values, error) { v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) return v, nil -} \ No newline at end of file +} + +// SetChatTitleConfig contains information for change chat title. +type SetChatTitleConfig struct { + ChatID int64 + Title string +} + +func (config SetChatTitleConfig) method() string { + return "setChatTitle" +} + +func (config SetChatTitleConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + v.Add("title", config.Title) + + return v, nil +} + +// SetChatDescriptionConfig contains information for change chat description. +type SetChatDescriptionConfig struct { + ChatID int64 + Description string +} + +func (config SetChatDescriptionConfig) method() string { + return "setChatDescription" +} + +func (config SetChatDescriptionConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + v.Add("description", config.Description) + + return v, nil +} From 04f51c32513f07bbb8f8392715d5524040e2671a Mon Sep 17 00:00:00 2001 From: Behrang Noruzi Niya Date: Sun, 4 Mar 2018 14:40:17 +0330 Subject: [PATCH 6/9] Add response parameters to error messages --- bot.go | 6 +++++- types.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bot.go b/bot.go index e201944..a0e1f81 100644 --- a/bot.go +++ b/bot.go @@ -79,7 +79,11 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, } if !apiResp.Ok { - return apiResp, errors.New(apiResp.Description) + parameters := ResponseParameters{} + if apiResp.Parameters != nil { + parameters = *apiResp.Parameters + } + return apiResp, Error{apiResp.Description, parameters} } return apiResp, nil diff --git a/types.go b/types.go index 4a58287..38bc0f6 100644 --- a/types.go +++ b/types.go @@ -410,7 +410,7 @@ type InlineKeyboardButton struct { SwitchInlineQuery *string `json:"switch_inline_query,omitempty"` // optional SwitchInlineQueryCurrentChat *string `json:"switch_inline_query_current_chat,omitempty"` // optional CallbackGame *CallbackGame `json:"callback_game,omitempty"` // optional - Pay bool `json:"pay,omitempty"` // optional + Pay bool `json:"pay,omitempty"` // optional } // CallbackQuery is data sent when a keyboard button with callback data @@ -771,3 +771,12 @@ type PreCheckoutQuery struct { ShippingOptionID string `json:"shipping_option_id,omitempty"` OrderInfo *OrderInfo `json:"order_info,omitempty"` } + +type Error struct { + Message string + ResponseParameters +} + +func (e Error) Error() string { + return e.Message +} From 57be98801107f19622d22a718cdc4a5f52cc0712 Mon Sep 17 00:00:00 2001 From: Oleksandr Savchuk Date: Mon, 5 Mar 2018 17:04:37 +0200 Subject: [PATCH 7/9] add setChatPhoto method --- bot.go | 12 ++++++++++++ configs.go | 15 +++++++++++++++ helpers.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/bot.go b/bot.go index 48d729b..165d3b6 100644 --- a/bot.go +++ b/bot.go @@ -922,3 +922,15 @@ func (bot *BotAPI) SetChatDescription(config SetChatDescriptionConfig) (APIRespo return bot.MakeRequest(config.method(), v) } + +// SetChatPhoto change photo of chat. +func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) { + params, err := config.params() + if err != nil { + return APIResponse{}, err + } + + file := config.getFile() + + return bot.UploadFile(config.method(), params, config.name(), file) +} diff --git a/configs.go b/configs.go index e16cbbb..1bbaff4 100644 --- a/configs.go +++ b/configs.go @@ -1111,3 +1111,18 @@ func (config SetChatDescriptionConfig) values() (url.Values, error) { return v, nil } + +// SetChatPhotoConfig contains information for change chat photo +type SetChatPhotoConfig struct { + BaseFile +} + +// name returns the field name for the Photo. +func (config SetChatPhotoConfig) name() string { + return "photo" +} + +// method returns Telegram API method name for sending Photo. +func (config SetChatPhotoConfig) method() string { + return "setChatPhoto" +} diff --git a/helpers.go b/helpers.go index f5800f4..c23a3bf 100644 --- a/helpers.go +++ b/helpers.go @@ -653,3 +653,34 @@ func NewInvoice(chatID int64, title, description, payload, providerToken, startP Currency: currency, Prices: prices} } + +// NewSetChatPhotoUpload creates a new chat photo uploader. +// +// chatID is where to send it, file is a string path to the file, +// FileReader, or FileBytes. +// +// Note that you must send animated GIFs as a document. +func NewSetChatPhotoUpload(chatID int64, file interface{}) SetChatPhotoConfig { + return SetChatPhotoConfig{ + BaseFile: BaseFile{ + BaseChat: BaseChat{ChatID: chatID}, + File: file, + UseExisting: false, + }, + } +} + +// NewSetChatPhotoShare shares an existing photo. +// You may use this to reshare an existing photo without reuploading it. +// +// chatID is where to send it, fileID is the ID of the file +// already uploaded. +func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig { + return SetChatPhotoConfig{ + BaseFile: BaseFile{ + BaseChat: BaseChat{ChatID: chatID}, + FileID: fileID, + UseExisting: true, + }, + } +} From a36af7a672af72fb2202924ab5c9001e17b76464 Mon Sep 17 00:00:00 2001 From: Oleksandr Savchuk Date: Mon, 5 Mar 2018 17:12:56 +0200 Subject: [PATCH 8/9] add deleteChatPhoto method --- bot.go | 12 ++++++++++++ configs.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/bot.go b/bot.go index 165d3b6..ee1ddd0 100644 --- a/bot.go +++ b/bot.go @@ -934,3 +934,15 @@ func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) return bot.UploadFile(config.method(), params, config.name(), file) } + +// DeleteChatPhoto delete photo of chat. +func (bot *BotAPI) DeleteChatPhoto(config DeleteChatPhotoConfig) (APIResponse, error) { + v, err := config.values() + if err != nil { + return APIResponse{}, err + } + + bot.debugLog(config.method(), v, nil) + + return bot.MakeRequest(config.method(), v) +} diff --git a/configs.go b/configs.go index 1bbaff4..574b3dd 100644 --- a/configs.go +++ b/configs.go @@ -1126,3 +1126,20 @@ func (config SetChatPhotoConfig) name() string { func (config SetChatPhotoConfig) method() string { return "setChatPhoto" } + +// DeleteChatPhotoConfig contains information for delete chat photo. +type DeleteChatPhotoConfig struct { + ChatID int64 +} + +func (config DeleteChatPhotoConfig) method() string { + return "deleteChatPhoto" +} + +func (config DeleteChatPhotoConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + + return v, nil +} From 309f2dd87fd1a17c552d9ae11a983ff28fc4afdf Mon Sep 17 00:00:00 2001 From: Syfaro Date: Mon, 26 Mar 2018 11:54:02 -0500 Subject: [PATCH 9/9] Minor code quality improvements. --- bot_test.go | 12 +++++++++--- types.go | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/bot_test.go b/bot_test.go index 5a3057d..e7aa7ac 100644 --- a/bot_test.go +++ b/bot_test.go @@ -373,7 +373,10 @@ func TestSendWithNewStickerAndKeyboardHide(t *testing.T) { bot, _ := getBot(t) msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg") - msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, false} + msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{ + RemoveKeyboard: true, + Selective: false, + } _, err := bot.Send(msg) if err != nil { @@ -386,7 +389,10 @@ func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) { bot, _ := getBot(t) msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID) - msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, false} + msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{ + RemoveKeyboard: true, + Selective: false, + } _, err := bot.Send(msg) @@ -399,7 +405,7 @@ func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) { func TestGetFile(t *testing.T) { bot, _ := getBot(t) - file := tgbotapi.FileConfig{ExistingPhotoFileID} + file := tgbotapi.FileConfig{FileID: ExistingPhotoFileID} _, err := bot.GetFile(file) diff --git a/types.go b/types.go index 38bc0f6..d3d30dc 100644 --- a/types.go +++ b/types.go @@ -232,9 +232,9 @@ func (m *Message) CommandArguments() string { entity := (*m.Entities)[0] if len(m.Text) == entity.Length { return "" // The command makes up the whole message - } else { - return m.Text[entity.Length+1:] } + + return m.Text[entity.Length+1:] } // MessageEntity contains information about data in a Message. @@ -772,6 +772,7 @@ type PreCheckoutQuery struct { OrderInfo *OrderInfo `json:"order_info,omitempty"` } +// Error is an error containing extra information returned by the Telegram API. type Error struct { Message string ResponseParameters