From b24a37443a9122cc263f3f82eaeb6b687d65db76 Mon Sep 17 00:00:00 2001 From: Lord-Protector Date: Sat, 5 Aug 2017 12:29:06 +0300 Subject: [PATCH 1/3] Add PinChatMessage and UnpinChatMessage methods --- bot.go | 44 ++++++++++++++++++++++++++++++++++---------- configs.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/bot.go b/bot.go index 9d2adab..2d95f9e 100644 --- a/bot.go +++ b/bot.go @@ -837,18 +837,42 @@ func (bot *BotAPI) DeleteMessage(config DeleteMessageConfig) (APIResponse, error // GetInviteLink get InviteLink for a chat func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) { - v := url.Values{} + v := url.Values{} - if config.SuperGroupUsername == "" { - v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) - } else { - v.Add("chat_id", config.SuperGroupUsername) - } + if config.SuperGroupUsername == "" { + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + } else { + v.Add("chat_id", config.SuperGroupUsername) + } - resp, err := bot.MakeRequest("exportChatInviteLink", v) + resp, err := bot.MakeRequest("exportChatInviteLink", v) - var inviteLink string - err = json.Unmarshal(resp.Result, &inviteLink) + var inviteLink string + err = json.Unmarshal(resp.Result, &inviteLink) - return inviteLink, err + return inviteLink, err } + +// Pin message in supergroup +func (bot *BotAPI) PinChatMessage(config PinChatMessageConfig) (APIResponse, error) { + v, err := config.values() + if err != nil { + return APIResponse{}, err + } + + bot.debugLog(config.method(), v, nil) + + return bot.MakeRequest(config.method(), v) +} + +// Unpin message in supergroup +func (bot *BotAPI) UnpinChatMessage(config UnpinChatMessageConfig) (APIResponse, error) { + v, err := config.values() + if err != nil { + return APIResponse{}, err + } + + bot.debugLog(config.method(), v, nil) + + return bot.MakeRequest(config.method(), v) +} \ No newline at end of file diff --git a/configs.go b/configs.go index d7358ee..c0293ce 100644 --- a/configs.go +++ b/configs.go @@ -1035,3 +1035,41 @@ func (config DeleteMessageConfig) values() (url.Values, error) { return v, nil } + +// PinChatMessageConfig contains information of a message in a chat to pin. +type PinChatMessageConfig struct { + ChatID int64 + MessageID int + DisableNotification bool +} + +func (config PinChatMessageConfig) method() string { + return "pinChatMessage" +} + +func (config PinChatMessageConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + v.Add("message_id", strconv.Itoa(config.MessageID)) + v.Add("disable_notification", strconv.FormatBool(config.DisableNotification)) + + return v, nil +} + +// UnpinChatMessageConfig contains information of chat to unpin. +type UnpinChatMessageConfig struct { + ChatID int64 +} + +func (config UnpinChatMessageConfig) method() string { + return "unpinChatMessage" +} + +func (config UnpinChatMessageConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + + return v, nil +} \ No newline at end of file From 3a60d28d73265c48475c2cf9f99b0842a88f1090 Mon Sep 17 00:00:00 2001 From: Lord-Protector Date: Sat, 5 Aug 2017 12:37:43 +0300 Subject: [PATCH 2/3] Tests for PinChatMessage and UnpinChatMessage methods --- bot_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/bot_test.go b/bot_test.go index d7cec08..0e45a38 100644 --- a/bot_test.go +++ b/bot_test.go @@ -609,3 +609,49 @@ func TestDeleteMessage(t *testing.T) { t.Fail() } } + +func TestPinChatMessage(t *testing.T) { + bot, _ := getBot(t) + + msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api") + msg.ParseMode = "markdown" + message, _ := bot.Send(msg) + + pinChatMessageConfig := tgbotapi.PinChatMessageConfig{ + ChatID: message.Chat.ID, + MessageID: message.MessageID, + DisableNotification: false, + } + _, err := bot.PinChatMessage(pinChatMessageConfig) + + if err != nil { + t.Error(err) + t.Fail() + } +} + +func TestUnpinChatMessage(t *testing.T) { + bot, _ := getBot(t) + + msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api") + msg.ParseMode = "markdown" + message, _ := bot.Send(msg) + + // We need pin message to unpin something + pinChatMessageConfig := tgbotapi.PinChatMessageConfig{ + ChatID: message.Chat.ID, + MessageID: message.MessageID, + DisableNotification: false, + } + _, err := bot.PinChatMessage(pinChatMessageConfig) + + unpinChatMessageConfig := tgbotapi.UnpinChatMessageConfig{ + ChatID: message.Chat.ID, + } + _, err = bot.UnpinChatMessage(unpinChatMessageConfig) + + if err != nil { + t.Error(err) + t.Fail() + } +} \ No newline at end of file From cd9a9741d0cfab359de3a9363b697721f41cc3d6 Mon Sep 17 00:00:00 2001 From: Lord-Protector Date: Sat, 5 Aug 2017 13:50:44 +0300 Subject: [PATCH 3/3] Create own supergroup with test already created bot and give it admin rights --- bot_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot_test.go b/bot_test.go index 0e45a38..a811a27 100644 --- a/bot_test.go +++ b/bot_test.go @@ -14,6 +14,7 @@ import ( const ( TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I" ChatID = 76918703 + SupergroupChatID = -1001120141283 ReplyToMessageID = 35 ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC" ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag" @@ -613,7 +614,7 @@ func TestDeleteMessage(t *testing.T) { func TestPinChatMessage(t *testing.T) { bot, _ := getBot(t) - msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api") + msg := tgbotapi.NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api") msg.ParseMode = "markdown" message, _ := bot.Send(msg) @@ -633,7 +634,7 @@ func TestPinChatMessage(t *testing.T) { func TestUnpinChatMessage(t *testing.T) { bot, _ := getBot(t) - msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api") + msg := tgbotapi.NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api") msg.ParseMode = "markdown" message, _ := bot.Send(msg)