Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (bot *BotAPI) GetFile(config FileConfig) (File, error) {
// GetUpdates fetches updates.
// If a WebHook is set, this will not return any data!
//
// Offset, Limit, and Timeout are optional.
// Offset, Limit, Timeout, and AllowedUpdates are optional.
// To avoid stale items, set Offset to one higher than the previous item.
// Set Timeout to a large number to reduce requests so you can get updates
// instantly instead of having to wait between requests.
Expand Down Expand Up @@ -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
}
65 changes: 56 additions & 9 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

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

time.Sleep(time.Second * 2)

bot.Request(RemoveWebhookConfig{})
bot.Request(DeleteWebhookConfig{})

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

bot.Request(RemoveWebhookConfig{})
bot.Request(DeleteWebhookConfig{})
}

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

time.Sleep(time.Second * 2)

bot.Request(RemoveWebhookConfig{})
bot.Request(DeleteWebhookConfig{})

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

bot.Request(RemoveWebhookConfig{})
bot.Request(DeleteWebhookConfig{})
}

func TestSendWithMediaGroup(t *testing.T) {
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -776,14 +796,41 @@ 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 {
t.Error(err)
}
}

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)

Expand Down
Loading