From 400e756356d7cb4950aea1b7cda7d76ea10c3766 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Sun, 27 Mar 2022 22:22:20 +0200 Subject: [PATCH] Better SendPhoto for non-network --- broker.go | 2 +- cmd/tg-broker/action.go | 2 +- command.go | 11 ++++++++++- telegram.go | 24 ++++++++++++++++-------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/broker.go b/broker.go index bde8cd2..77220c0 100644 --- a/broker.go +++ b/broker.go @@ -61,7 +61,7 @@ func (b *Broker) SendTextMessage(chat *APIChat, text string, extra *MessageOptio // SendPhoto sends a photo with an optional caption to a chat. // A reply_to message ID can be specified as optional parameter. func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, extra *MessageOptions) { - photodata := &ClientPhotoData{ + photodata := &ClientPhotoDataNet{ ChatID: chat.ChatID, Filename: filename, Bytes: base64.StdEncoding.EncodeToString(data), diff --git a/cmd/tg-broker/action.go b/cmd/tg-broker/action.go index 15b43fb..e81ec5a 100644 --- a/cmd/tg-broker/action.go +++ b/cmd/tg-broker/action.go @@ -16,7 +16,7 @@ func executeClientCommand(action tg.ClientCommand, client net.Conn) { api.GetFileNet(data, client, *action.Callback) case tg.CmdSendPhoto: data := *(action.PhotoData) - api.SendPhoto(data) + api.SendPhotoNet(data) case tg.CmdForwardMessage: data := *(action.ForwardMessageData) api.ForwardMessage(data) diff --git a/command.go b/command.go index c12a3d0..8a10681 100644 --- a/command.go +++ b/command.go @@ -68,6 +68,15 @@ type ClientTextMessageData struct { // ClientPhotoData is the required data for a CmdSendPhoto request type ClientPhotoData struct { + ChatID int64 + Bytes []byte + Filename string + Caption string `json:",omitempty"` + ReplyID *int64 `json:",omitempty"` +} + +// ClientPhotoDataNet is the required data for a CmdSendPhoto request over the wire +type ClientPhotoDataNet struct { ChatID int64 Bytes string Filename string @@ -156,7 +165,7 @@ type FileRequestData struct { type ClientCommand struct { Type ClientCommandType TextMessageData *ClientTextMessageData `json:",omitempty"` - PhotoData *ClientPhotoData `json:",omitempty"` + PhotoData *ClientPhotoDataNet `json:",omitempty"` ForwardMessageData *ClientForwardMessageData `json:",omitempty"` ChatActionData *ClientChatActionData `json:",omitempty"` InlineQueryResults *InlineQueryResponse `json:",omitempty"` diff --git a/telegram.go b/telegram.go index 59b9357..1ebf267 100644 --- a/telegram.go +++ b/telegram.go @@ -110,16 +110,24 @@ func (t Telegram) SendTextMessage(data ClientTextMessageData) (APIMessage, error return out.Result, err } -// SendPhoto sends a picture to a chat as a photo -func (t Telegram) SendPhoto(data ClientPhotoData) (APIMessage, error) { - // Decode photo from b64 - photolen := base64.StdEncoding.DecodedLen(len(data.Bytes)) - photobytes := make([]byte, photolen) - decoded, err := base64.StdEncoding.Decode(photobytes, []byte(data.Bytes)) - if checkerr("SendPhoto/base64.Decode", err) { +// SendPhotoNet decodes a network SendPhoto request and executes it +func (t Telegram) SendPhotoNet(data ClientPhotoDataNet) (APIMessage, error) { + byt, err := base64.StdEncoding.DecodeString(data.Bytes) + if checkerr("SendPhotoNet/base64.Decode", err) { return APIMessage{}, err } + return t.SendPhoto(ClientPhotoData{ + ChatID: data.ChatID, + Bytes: byt, + Filename: data.Filename, + Caption: data.Caption, + ReplyID: data.ReplyID, + }) +} + +// SendPhoto sends a picture to a chat as a photo +func (t Telegram) SendPhoto(data ClientPhotoData) (APIMessage, error) { // Write file into multipart buffer body := new(bytes.Buffer) writer := multipart.NewWriter(body) @@ -127,7 +135,7 @@ func (t Telegram) SendPhoto(data ClientPhotoData) (APIMessage, error) { if checkerr("SendPhoto/multipart.CreateFormFile", err) { return APIMessage{}, err } - part.Write(photobytes[0:decoded]) + part.Write(data.Bytes) // Write other fields writer.WriteField("chat_id", strconv.FormatInt(data.ChatID, 10))