Skip to content

Commit 4064ced

Browse files
committed
Add some tests, fix copyMessage.
1 parent 24e02f7 commit 4064ced

File tree

6 files changed

+96
-5
lines changed

6 files changed

+96
-5
lines changed

bot.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,23 @@ func (bot *BotAPI) GetMyCommands() ([]BotCommand, error) {
668668

669669
return commands, err
670670
}
671+
672+
// CopyMessage copy messages of any kind. The method is analogous to the method
673+
// forwardMessage, but the copied message doesn't have a link to the original
674+
// message. Returns the MessageID of the sent message on success.
675+
func (bot *BotAPI) CopyMessage(config CopyMessageConfig) (MessageID, error) {
676+
params, err := config.params()
677+
if err != nil {
678+
return MessageID{}, err
679+
}
680+
681+
resp, err := bot.MakeRequest(config.method(), params)
682+
if err != nil {
683+
return MessageID{}, err
684+
}
685+
686+
var messageID MessageID
687+
err = json.Unmarshal(resp.Result, &messageID)
688+
689+
return messageID, err
690+
}

bot_test.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestSendWithMessage(t *testing.T) {
7373
bot, _ := getBot(t)
7474

7575
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
76-
msg.ParseMode = "markdown"
76+
msg.ParseMode = ModeMarkdown
7777
_, err := bot.Send(msg)
7878

7979
if err != nil {
@@ -104,6 +104,26 @@ func TestSendWithMessageForward(t *testing.T) {
104104
}
105105
}
106106

107+
func TestCopyMessage(t *testing.T) {
108+
bot, _ := getBot(t)
109+
110+
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
111+
message, err := bot.Send(msg)
112+
if err != nil {
113+
t.Error(err)
114+
}
115+
116+
copyMessageConfig := NewCopyMessage(SupergroupChatID, message.Chat.ID, message.MessageID)
117+
messageID, err := bot.CopyMessage(copyMessageConfig)
118+
if err != nil {
119+
t.Error(err)
120+
}
121+
122+
if messageID.MessageID == message.MessageID {
123+
t.Error("copied message ID was the same as original message")
124+
}
125+
}
126+
107127
func TestSendWithNewPhoto(t *testing.T) {
108128
bot, _ := getBot(t)
109129

@@ -724,7 +744,7 @@ func TestDeleteMessage(t *testing.T) {
724744
bot, _ := getBot(t)
725745

726746
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
727-
msg.ParseMode = "markdown"
747+
msg.ParseMode = ModeMarkdown
728748
message, _ := bot.Send(msg)
729749

730750
deleteMessageConfig := DeleteMessageConfig{
@@ -742,7 +762,7 @@ func TestPinChatMessage(t *testing.T) {
742762
bot, _ := getBot(t)
743763

744764
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
745-
msg.ParseMode = "markdown"
765+
msg.ParseMode = ModeMarkdown
746766
message, _ := bot.Send(msg)
747767

748768
pinChatMessageConfig := PinChatMessageConfig{
@@ -761,7 +781,7 @@ func TestUnpinChatMessage(t *testing.T) {
761781
bot, _ := getBot(t)
762782

763783
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
764-
msg.ParseMode = "markdown"
784+
msg.ParseMode = ModeMarkdown
765785
message, _ := bot.Send(msg)
766786

767787
// We need pin message to unpin something
@@ -776,14 +796,41 @@ func TestUnpinChatMessage(t *testing.T) {
776796
}
777797

778798
unpinChatMessageConfig := UnpinChatMessageConfig{
779-
ChatID: message.Chat.ID,
799+
ChatID: message.Chat.ID,
800+
MessageID: message.MessageID,
780801
}
781802

782803
if _, err := bot.Request(unpinChatMessageConfig); err != nil {
783804
t.Error(err)
784805
}
785806
}
786807

808+
func TestUnpinAllChatMessages(t *testing.T) {
809+
bot, _ := getBot(t)
810+
811+
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
812+
msg.ParseMode = ModeMarkdown
813+
message, _ := bot.Send(msg)
814+
815+
pinChatMessageConfig := PinChatMessageConfig{
816+
ChatID: message.Chat.ID,
817+
MessageID: message.MessageID,
818+
DisableNotification: true,
819+
}
820+
821+
if _, err := bot.Request(pinChatMessageConfig); err != nil {
822+
t.Error(err)
823+
}
824+
825+
unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
826+
ChatID: message.Chat.ID,
827+
}
828+
829+
if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
830+
t.Error(err)
831+
}
832+
}
833+
787834
func TestPolls(t *testing.T) {
788835
bot, _ := getBot(t)
789836

configs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ func (config CopyMessageConfig) params() (Params, error) {
241241
return params, err
242242
}
243243

244+
func (config CopyMessageConfig) method() string {
245+
return "copyMessage"
246+
}
247+
244248
// PhotoConfig contains information about a SendPhoto request.
245249
type PhotoConfig struct {
246250
BaseFile

helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ func NewForward(chatID int64, fromChatID int64, messageID int) ForwardConfig {
5252
}
5353
}
5454

55+
// NewCopyMessage creates a new copy message.
56+
//
57+
// chatID is where to send it, fromChatID is the source chat,
58+
// and messageID is the ID of the original message.
59+
func NewCopyMessage(chatID int64, fromChatID int64, messageID int) CopyMessageConfig {
60+
return CopyMessageConfig{
61+
BaseChat: BaseChat{ChatID: chatID},
62+
FromChatID: fromChatID,
63+
MessageID: messageID,
64+
}
65+
}
66+
5567
// NewPhotoUpload creates a new photo uploader.
5668
//
5769
// chatID is where to send it, file is a string path to the file,

types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,11 @@ func (m *Message) CommandArguments() string {
584584
return m.Text[entity.Length+1:]
585585
}
586586

587+
// MessageID represents a unique message identifier.
588+
type MessageID struct {
589+
MessageID int `json:"message_id"`
590+
}
591+
587592
// MessageEntity represents one special entity in a text message.
588593
type MessageEntity struct {
589594
// Type of the entity.

types_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ var (
286286
_ Chattable = ChatActionConfig{}
287287
_ Chattable = ChatInfoConfig{}
288288
_ Chattable = ChatInviteLinkConfig{}
289+
_ Chattable = CloseConfig{}
290+
_ Chattable = CopyMessageConfig{}
289291
_ Chattable = ContactConfig{}
290292
_ Chattable = DeleteChatPhotoConfig{}
291293
_ Chattable = DeleteChatStickerSetConfig{}
@@ -306,6 +308,7 @@ var (
306308
_ Chattable = KickChatMemberConfig{}
307309
_ Chattable = LeaveChatConfig{}
308310
_ Chattable = LocationConfig{}
311+
_ Chattable = LogOutConfig{}
309312
_ Chattable = MediaGroupConfig{}
310313
_ Chattable = MessageConfig{}
311314
_ Chattable = PhotoConfig{}

0 commit comments

Comments
 (0)