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.
// 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),

View file

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

View file

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

View file

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