Add some tests, fix copyMessage.

bot-api-6.1
Syfaro 2020-11-06 12:36:00 -05:00
parent 24e02f7ba6
commit 4064ced03f
6 changed files with 96 additions and 5 deletions

20
bot.go
View File

@ -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
}

View File

@ -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
@ -777,6 +797,7 @@ func TestUnpinChatMessage(t *testing.T) {
unpinChatMessageConfig := UnpinChatMessageConfig{
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)

View File

@ -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

View File

@ -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,

View File

@ -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.

View File

@ -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{}