Merge pull request #115 from Lord-Protector/master

Add PinChatMessage and UnpinChatMessage methods
bot-api-6.1
Syfaro 2017-08-05 22:56:32 -05:00 committed by GitHub
commit 9dda67c714
3 changed files with 119 additions and 10 deletions

44
bot.go
View File

@ -837,18 +837,42 @@ func (bot *BotAPI) DeleteMessage(config DeleteMessageConfig) (APIResponse, error
// GetInviteLink get InviteLink for a chat // GetInviteLink get InviteLink for a chat
func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) { func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) {
v := url.Values{} v := url.Values{}
if config.SuperGroupUsername == "" { if config.SuperGroupUsername == "" {
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
} else { } else {
v.Add("chat_id", config.SuperGroupUsername) v.Add("chat_id", config.SuperGroupUsername)
} }
resp, err := bot.MakeRequest("exportChatInviteLink", v) resp, err := bot.MakeRequest("exportChatInviteLink", v)
var inviteLink string var inviteLink string
err = json.Unmarshal(resp.Result, &inviteLink) 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)
}

View File

@ -14,6 +14,7 @@ import (
const ( const (
TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I" TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
ChatID = 76918703 ChatID = 76918703
SupergroupChatID = -1001120141283
ReplyToMessageID = 35 ReplyToMessageID = 35
ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC" ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag" ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
@ -609,3 +610,49 @@ func TestDeleteMessage(t *testing.T) {
t.Fail() t.Fail()
} }
} }
func TestPinChatMessage(t *testing.T) {
bot, _ := getBot(t)
msg := tgbotapi.NewMessage(SupergroupChatID, "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(SupergroupChatID, "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()
}
}

View File

@ -1035,3 +1035,41 @@ func (config DeleteMessageConfig) values() (url.Values, error) {
return v, nil 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
}