From 4064ced03f921894c1c8ee0b40476903f7d10b40 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Fri, 6 Nov 2020 12:36:00 -0500 Subject: [PATCH] Add some tests, fix copyMessage. --- bot.go | 20 ++++++++++++++++++ bot_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++----- configs.go | 4 ++++ helpers.go | 12 +++++++++++ types.go | 5 +++++ types_test.go | 3 +++ 6 files changed, 96 insertions(+), 5 deletions(-) diff --git a/bot.go b/bot.go index a78e635..d7b5b88 100644 --- a/bot.go +++ b/bot.go @@ -668,3 +668,23 @@ func (bot *BotAPI) GetMyCommands() ([]BotCommand, error) { return commands, err } + +// CopyMessage copy messages of any kind. The method is analogous to the method +// forwardMessage, but the copied message doesn't have a link to the original +// message. Returns the MessageID of the sent message on success. +func (bot *BotAPI) CopyMessage(config CopyMessageConfig) (MessageID, error) { + params, err := config.params() + if err != nil { + return MessageID{}, err + } + + resp, err := bot.MakeRequest(config.method(), params) + if err != nil { + return MessageID{}, err + } + + var messageID MessageID + err = json.Unmarshal(resp.Result, &messageID) + + return messageID, err +} diff --git a/bot_test.go b/bot_test.go index ddb8fb2..c465909 100644 --- a/bot_test.go +++ b/bot_test.go @@ -73,7 +73,7 @@ func TestSendWithMessage(t *testing.T) { bot, _ := getBot(t) msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api") - msg.ParseMode = "markdown" + msg.ParseMode = ModeMarkdown _, err := bot.Send(msg) if err != nil { @@ -104,6 +104,26 @@ func TestSendWithMessageForward(t *testing.T) { } } +func TestCopyMessage(t *testing.T) { + bot, _ := getBot(t) + + msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api") + message, err := bot.Send(msg) + if err != nil { + t.Error(err) + } + + copyMessageConfig := NewCopyMessage(SupergroupChatID, message.Chat.ID, message.MessageID) + messageID, err := bot.CopyMessage(copyMessageConfig) + if err != nil { + t.Error(err) + } + + if messageID.MessageID == message.MessageID { + t.Error("copied message ID was the same as original message") + } +} + func TestSendWithNewPhoto(t *testing.T) { bot, _ := getBot(t) @@ -724,7 +744,7 @@ func TestDeleteMessage(t *testing.T) { bot, _ := getBot(t) msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api") - msg.ParseMode = "markdown" + msg.ParseMode = ModeMarkdown message, _ := bot.Send(msg) deleteMessageConfig := DeleteMessageConfig{ @@ -742,7 +762,7 @@ func TestPinChatMessage(t *testing.T) { bot, _ := getBot(t) msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api") - msg.ParseMode = "markdown" + msg.ParseMode = ModeMarkdown message, _ := bot.Send(msg) pinChatMessageConfig := PinChatMessageConfig{ @@ -761,7 +781,7 @@ func TestUnpinChatMessage(t *testing.T) { bot, _ := getBot(t) msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api") - msg.ParseMode = "markdown" + msg.ParseMode = ModeMarkdown message, _ := bot.Send(msg) // We need pin message to unpin something @@ -776,7 +796,8 @@ func TestUnpinChatMessage(t *testing.T) { } unpinChatMessageConfig := UnpinChatMessageConfig{ - ChatID: message.Chat.ID, + ChatID: message.Chat.ID, + MessageID: message.MessageID, } if _, err := bot.Request(unpinChatMessageConfig); err != nil { @@ -784,6 +805,32 @@ func TestUnpinChatMessage(t *testing.T) { } } +func TestUnpinAllChatMessages(t *testing.T) { + bot, _ := getBot(t) + + msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api") + msg.ParseMode = ModeMarkdown + message, _ := bot.Send(msg) + + pinChatMessageConfig := PinChatMessageConfig{ + ChatID: message.Chat.ID, + MessageID: message.MessageID, + DisableNotification: true, + } + + if _, err := bot.Request(pinChatMessageConfig); err != nil { + t.Error(err) + } + + unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{ + ChatID: message.Chat.ID, + } + + if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil { + t.Error(err) + } +} + func TestPolls(t *testing.T) { bot, _ := getBot(t) diff --git a/configs.go b/configs.go index c1321cd..cf192a4 100644 --- a/configs.go +++ b/configs.go @@ -241,6 +241,10 @@ func (config CopyMessageConfig) params() (Params, error) { return params, err } +func (config CopyMessageConfig) method() string { + return "copyMessage" +} + // PhotoConfig contains information about a SendPhoto request. type PhotoConfig struct { BaseFile diff --git a/helpers.go b/helpers.go index 4338c0a..e98ae06 100644 --- a/helpers.go +++ b/helpers.go @@ -52,6 +52,18 @@ func NewForward(chatID int64, fromChatID int64, messageID int) ForwardConfig { } } +// NewCopyMessage creates a new copy message. +// +// chatID is where to send it, fromChatID is the source chat, +// and messageID is the ID of the original message. +func NewCopyMessage(chatID int64, fromChatID int64, messageID int) CopyMessageConfig { + return CopyMessageConfig{ + BaseChat: BaseChat{ChatID: chatID}, + FromChatID: fromChatID, + MessageID: messageID, + } +} + // NewPhotoUpload creates a new photo uploader. // // chatID is where to send it, file is a string path to the file, diff --git a/types.go b/types.go index 63bcb13..22ae9f1 100644 --- a/types.go +++ b/types.go @@ -584,6 +584,11 @@ func (m *Message) CommandArguments() string { return m.Text[entity.Length+1:] } +// MessageID represents a unique message identifier. +type MessageID struct { + MessageID int `json:"message_id"` +} + // MessageEntity represents one special entity in a text message. type MessageEntity struct { // Type of the entity. diff --git a/types_test.go b/types_test.go index 0b1be9d..e9e2026 100644 --- a/types_test.go +++ b/types_test.go @@ -286,6 +286,8 @@ var ( _ Chattable = ChatActionConfig{} _ Chattable = ChatInfoConfig{} _ Chattable = ChatInviteLinkConfig{} + _ Chattable = CloseConfig{} + _ Chattable = CopyMessageConfig{} _ Chattable = ContactConfig{} _ Chattable = DeleteChatPhotoConfig{} _ Chattable = DeleteChatStickerSetConfig{} @@ -306,6 +308,7 @@ var ( _ Chattable = KickChatMemberConfig{} _ Chattable = LeaveChatConfig{} _ Chattable = LocationConfig{} + _ Chattable = LogOutConfig{} _ Chattable = MediaGroupConfig{} _ Chattable = MessageConfig{} _ Chattable = PhotoConfig{}