Better SendPhoto for non-network

This commit is contained in:
Hamcha 2022-03-27 22:22:20 +02:00
parent b27494aa52
commit 400e756356
Signed by: hamcha
GPG Key ID: 1669C533B8CF6D89
4 changed files with 28 additions and 11 deletions

View File

@ -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. // SendPhoto sends a photo with an optional caption to a chat.
// A reply_to message ID can be specified as optional parameter. // 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) { func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, extra *MessageOptions) {
photodata := &ClientPhotoData{ photodata := &ClientPhotoDataNet{
ChatID: chat.ChatID, ChatID: chat.ChatID,
Filename: filename, Filename: filename,
Bytes: base64.StdEncoding.EncodeToString(data), Bytes: base64.StdEncoding.EncodeToString(data),

View File

@ -16,7 +16,7 @@ func executeClientCommand(action tg.ClientCommand, client net.Conn) {
api.GetFileNet(data, client, *action.Callback) api.GetFileNet(data, client, *action.Callback)
case tg.CmdSendPhoto: case tg.CmdSendPhoto:
data := *(action.PhotoData) data := *(action.PhotoData)
api.SendPhoto(data) api.SendPhotoNet(data)
case tg.CmdForwardMessage: case tg.CmdForwardMessage:
data := *(action.ForwardMessageData) data := *(action.ForwardMessageData)
api.ForwardMessage(data) api.ForwardMessage(data)

View File

@ -68,6 +68,15 @@ type ClientTextMessageData struct {
// ClientPhotoData is the required data for a CmdSendPhoto request // ClientPhotoData is the required data for a CmdSendPhoto request
type ClientPhotoData struct { 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 ChatID int64
Bytes string Bytes string
Filename string Filename string
@ -156,7 +165,7 @@ type FileRequestData struct {
type ClientCommand struct { type ClientCommand struct {
Type ClientCommandType Type ClientCommandType
TextMessageData *ClientTextMessageData `json:",omitempty"` TextMessageData *ClientTextMessageData `json:",omitempty"`
PhotoData *ClientPhotoData `json:",omitempty"` PhotoData *ClientPhotoDataNet `json:",omitempty"`
ForwardMessageData *ClientForwardMessageData `json:",omitempty"` ForwardMessageData *ClientForwardMessageData `json:",omitempty"`
ChatActionData *ClientChatActionData `json:",omitempty"` ChatActionData *ClientChatActionData `json:",omitempty"`
InlineQueryResults *InlineQueryResponse `json:",omitempty"` InlineQueryResults *InlineQueryResponse `json:",omitempty"`

View File

@ -110,16 +110,24 @@ func (t Telegram) SendTextMessage(data ClientTextMessageData) (APIMessage, error
return out.Result, err return out.Result, err
} }
// SendPhoto sends a picture to a chat as a photo // SendPhotoNet decodes a network SendPhoto request and executes it
func (t Telegram) SendPhoto(data ClientPhotoData) (APIMessage, error) { func (t Telegram) SendPhotoNet(data ClientPhotoDataNet) (APIMessage, error) {
// Decode photo from b64 byt, err := base64.StdEncoding.DecodeString(data.Bytes)
photolen := base64.StdEncoding.DecodedLen(len(data.Bytes)) if checkerr("SendPhotoNet/base64.Decode", err) {
photobytes := make([]byte, photolen)
decoded, err := base64.StdEncoding.Decode(photobytes, []byte(data.Bytes))
if checkerr("SendPhoto/base64.Decode", err) {
return APIMessage{}, 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 // Write file into multipart buffer
body := new(bytes.Buffer) body := new(bytes.Buffer)
writer := multipart.NewWriter(body) writer := multipart.NewWriter(body)
@ -127,7 +135,7 @@ func (t Telegram) SendPhoto(data ClientPhotoData) (APIMessage, error) {
if checkerr("SendPhoto/multipart.CreateFormFile", err) { if checkerr("SendPhoto/multipart.CreateFormFile", err) {
return APIMessage{}, err return APIMessage{}, err
} }
part.Write(photobytes[0:decoded]) part.Write(data.Bytes)
// Write other fields // Write other fields
writer.WriteField("chat_id", strconv.FormatInt(data.ChatID, 10)) writer.WriteField("chat_id", strconv.FormatInt(data.ChatID, 10))