Skip to content

Commit d10c681

Browse files
authored
Merge pull request #392 from go-telegram-bot-api/bot-api-5.0
Updates for Bot API 5.0 and 5.1
2 parents 366879b + 3b5c8a9 commit d10c681

File tree

6 files changed

+810
-218
lines changed

6 files changed

+810
-218
lines changed

bot.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func (bot *BotAPI) GetFile(config FileConfig) (File, error) {
387387
// GetUpdates fetches updates.
388388
// If a WebHook is set, this will not return any data!
389389
//
390-
// Offset, Limit, and Timeout are optional.
390+
// Offset, Limit, Timeout, and AllowedUpdates are optional.
391391
// To avoid stale items, set Offset to one higher than the previous item.
392392
// Set Timeout to a large number to reduce requests so you can get updates
393393
// instantly instead of having to wait between requests.
@@ -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: 56 additions & 9 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

@@ -518,7 +538,7 @@ func TestSetWebhookWithCert(t *testing.T) {
518538

519539
time.Sleep(time.Second * 2)
520540

521-
bot.Request(RemoveWebhookConfig{})
541+
bot.Request(DeleteWebhookConfig{})
522542

523543
wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
524544
_, err := bot.Request(wh)
@@ -532,15 +552,15 @@ func TestSetWebhookWithCert(t *testing.T) {
532552
t.Error(err)
533553
}
534554

535-
bot.Request(RemoveWebhookConfig{})
555+
bot.Request(DeleteWebhookConfig{})
536556
}
537557

538558
func TestSetWebhookWithoutCert(t *testing.T) {
539559
bot, _ := getBot(t)
540560

541561
time.Sleep(time.Second * 2)
542562

543-
bot.Request(RemoveWebhookConfig{})
563+
bot.Request(DeleteWebhookConfig{})
544564

545565
wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
546566
_, err := bot.Request(wh)
@@ -560,7 +580,7 @@ func TestSetWebhookWithoutCert(t *testing.T) {
560580
t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
561581
}
562582

563-
bot.Request(RemoveWebhookConfig{})
583+
bot.Request(DeleteWebhookConfig{})
564584
}
565585

566586
func TestSendWithMediaGroup(t *testing.T) {
@@ -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

0 commit comments

Comments
 (0)