From 696aebd64c3d29da9d41a6a0ee24c1f32e278041 Mon Sep 17 00:00:00 2001 From: nightghost Date: Sun, 1 Sep 2019 22:00:09 +0300 Subject: [PATCH 1/2] Uploading photo to a channel functionality added. --- bot_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ helpers.go | 18 ++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/bot_test.go b/bot_test.go index fe1fd55..5947936 100644 --- a/bot_test.go +++ b/bot_test.go @@ -11,6 +11,7 @@ import ( const ( TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I" ChatID = 76918703 + Channel = "@nightghost_test" SupergroupChatID = -1001120141283 ReplyToMessageID = 35 ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC" @@ -153,6 +154,51 @@ func TestSendWithNewPhotoReply(t *testing.T) { } } +func TestSendNewPhotoToChannel(t *testing.T) { + bot, _ := getBot(t) + + msg := NewPhotoUploadToChannel(Channel, "tests/image.jpg") + msg.Caption = "Test" + _, err := bot.Send(msg) + + if err != nil { + t.Error(err) + t.Fail() + } +} + +func TestSendNewPhotoToChannelFileBytes(t *testing.T) { + bot, _ := getBot(t) + + data, _ := ioutil.ReadFile("tests/image.jpg") + b := FileBytes{Name: "image.jpg", Bytes: data} + + msg := NewPhotoUploadToChannel(Channel, b) + msg.Caption = "Test" + _, err := bot.Send(msg) + + if err != nil { + t.Error(err) + t.Fail() + } +} + +func TestSendNewPhotoToChannelFileReader(t *testing.T) { + bot, _ := getBot(t) + + f, _ := os.Open("tests/image.jpg") + reader := FileReader{Name: "image.jpg", Reader: f, Size: -1} + + msg := NewPhotoUploadToChannel(Channel, reader) + msg.Caption = "Test" + _, err := bot.Send(msg) + + if err != nil { + t.Error(err) + t.Fail() + } +} + func TestSendWithExistingPhoto(t *testing.T) { bot, _ := getBot(t) diff --git a/helpers.go b/helpers.go index b97aa2a..015fb73 100644 --- a/helpers.go +++ b/helpers.go @@ -67,6 +67,24 @@ func NewPhotoUpload(chatID int64, file interface{}) PhotoConfig { } } +// NewPhotoUploadToChannel creates a new photo uploader to send a photo to a channel. +// +// username is the username of the channel, file is a string path to the file, +// FileReader, or FileBytes. +// +// Note that you must send animated GIFs as a document. +func NewPhotoUploadToChannel(username string, file interface{}) PhotoConfig { + return PhotoConfig{ + BaseFile: BaseFile{ + BaseChat: BaseChat{ + ChannelUsername: username, + }, + File: file, + UseExisting: false, + }, + } +} + // NewPhotoShare shares an existing photo. // You may use this to reshare an existing photo without reuploading it. // From aaaa278b563e4e344a0e9440591347371801f2f1 Mon Sep 17 00:00:00 2001 From: nightghost Date: Mon, 2 Sep 2019 18:44:35 +0300 Subject: [PATCH 2/2] My own bot is used to test the added functionality. --- bot_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bot_test.go b/bot_test.go index 5947936..98d4f24 100644 --- a/bot_test.go +++ b/bot_test.go @@ -11,6 +11,7 @@ import ( const ( TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I" ChatID = 76918703 + ChannelBot = "903278594:AAHmLoQncKOMKz2A644-cIK1Sb0VwfnOpGQ" Channel = "@nightghost_test" SupergroupChatID = -1001120141283 ReplyToMessageID = 35 @@ -155,7 +156,7 @@ func TestSendWithNewPhotoReply(t *testing.T) { } func TestSendNewPhotoToChannel(t *testing.T) { - bot, _ := getBot(t) + bot, _ := NewBotAPI(ChannelBot) msg := NewPhotoUploadToChannel(Channel, "tests/image.jpg") msg.Caption = "Test" @@ -168,7 +169,7 @@ func TestSendNewPhotoToChannel(t *testing.T) { } func TestSendNewPhotoToChannelFileBytes(t *testing.T) { - bot, _ := getBot(t) + bot, _ := NewBotAPI(ChannelBot) data, _ := ioutil.ReadFile("tests/image.jpg") b := FileBytes{Name: "image.jpg", Bytes: data} @@ -184,7 +185,7 @@ func TestSendNewPhotoToChannelFileBytes(t *testing.T) { } func TestSendNewPhotoToChannelFileReader(t *testing.T) { - bot, _ := getBot(t) + bot, _ := NewBotAPI(ChannelBot) f, _ := os.Open("tests/image.jpg") reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}