all repos — telegram-bot-api @ 9d3974d340f237d4b935ce3e1b512ce343868959

Golang bindings for the Telegram Bot API

Merge pull request #261 from zergon321/add-photo-upload-channel

Uploading a new photo to a channel functionality added.
Syfaro syfaro@huefox.com
Wed, 22 Jul 2020 05:00:28 -0500
commit

9d3974d340f237d4b935ce3e1b512ce343868959

parent

5598dbcb902dd7aa90aa99fbe395888a9c8e9848

2 files changed, 64 insertions(+), 0 deletions(-)

jump to
M bot_test.gobot_test.go

@@ -11,6 +11,7 @@

const ( TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I" ChatID = 76918703 + Channel = "@tgbotapitest" SupergroupChatID = -1001120141283 ReplyToMessageID = 35 ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"

@@ -145,6 +146,51 @@

msg := NewPhotoUpload(ChatID, "tests/image.jpg") msg.ReplyToMessageID = ReplyToMessageID + _, err := bot.Send(msg) + + if err != nil { + t.Error(err) + t.Fail() + } +} + +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 {
M helpers.gohelpers.go

@@ -67,6 +67,24 @@ },

} } +// 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. //