all repos — telegram-bot-api @ 4064ced03f921894c1c8ee0b40476903f7d10b40

Golang bindings for the Telegram Bot API

Add some tests, fix copyMessage.
Syfaro syfaro@huefox.com
Fri, 06 Nov 2020 12:36:00 -0500
commit

4064ced03f921894c1c8ee0b40476903f7d10b40

parent

24e02f7ba6aa2e045e23e8afbebaf7f249f0e368

6 files changed, 96 insertions(+), 5 deletions(-)

jump to
M bot.gobot.go

@@ -668,3 +668,23 @@ err = json.Unmarshal(resp.Result, &commands)

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 +}
M bot_test.gobot_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 {

@@ -101,6 +101,26 @@ _, err := bot.Send(msg)

if err != nil { t.Error(err) + } +} + +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") } }

@@ -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,10 +796,37 @@ t.Error(err)

} unpinChatMessageConfig := UnpinChatMessageConfig{ + 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(unpinChatMessageConfig); err != nil { + if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil { t.Error(err) } }
M configs.goconfigs.go

@@ -241,6 +241,10 @@

return params, err } +func (config CopyMessageConfig) method() string { + return "copyMessage" +} + // PhotoConfig contains information about a SendPhoto request. type PhotoConfig struct { BaseFile
M helpers.gohelpers.go

@@ -52,6 +52,18 @@ MessageID: messageID,

} } +// 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,
M types.gotypes.go

@@ -584,6 +584,11 @@

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.
M types_test.gotypes_test.go

@@ -286,6 +286,8 @@ _ Chattable = ChatAdministratorsConfig{}

_ Chattable = ChatActionConfig{} _ Chattable = ChatInfoConfig{} _ Chattable = ChatInviteLinkConfig{} + _ Chattable = CloseConfig{} + _ Chattable = CopyMessageConfig{} _ Chattable = ContactConfig{} _ Chattable = DeleteChatPhotoConfig{} _ Chattable = DeleteChatStickerSetConfig{}

@@ -306,6 +308,7 @@ _ Chattable = InvoiceConfig{}

_ Chattable = KickChatMemberConfig{} _ Chattable = LeaveChatConfig{} _ Chattable = LocationConfig{} + _ Chattable = LogOutConfig{} _ Chattable = MediaGroupConfig{} _ Chattable = MessageConfig{} _ Chattable = PhotoConfig{}