Changed []byte structs to Base64 encoding
This commit is contained in:
parent
32612670a3
commit
1f113c54bd
3 changed files with 49 additions and 4 deletions
|
@ -2,11 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
|
@ -39,7 +41,7 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
|
|||
if strings.HasPrefix(caption, "/meme ") && len(caption) > 6 {
|
||||
idx := strings.Index(caption, ";")
|
||||
if idx < 0 {
|
||||
broker.SendTextMessage(update.Chat, "<b>Formato</b>: /meme TESTO IN ALTO<b>;</b>TESTO IN BASSO", &(update.MessageID))
|
||||
broker.SendTextMessage(update.Chat, "<b>Formato</b>: /meme TESTO IN ALTO<b>;</b>TESTO IN BASSO", &update.MessageID)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -55,9 +57,17 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
|
|||
}
|
||||
}
|
||||
broker.GetFile(photo.FileID, func(broker *tg.Broker, data tg.BrokerUpdate) {
|
||||
img, _, err := image.Decode(bytes.NewReader(data.Bytes))
|
||||
pbytes, err := base64.StdEncoding.DecodeString(*data.Bytes)
|
||||
if err != nil {
|
||||
broker.SendTextMessage(update.Chat, "<b>ERROR</b>: Non riesco a leggere l'immagine", &(update.MessageID))
|
||||
log.Println("[memegen] Base64 decode error: %s\n", err.Error())
|
||||
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
||||
return
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(bytes.NewReader(pbytes))
|
||||
if err != nil {
|
||||
log.Println("[memegen] Image decode error: %s\n", err.Error())
|
||||
broker.SendTextMessage(update.Chat, "<b>ERROR</b>: Non riesco a leggere l'immagine", &update.MessageID)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -145,6 +155,11 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
|
|||
|
||||
buf := new(bytes.Buffer)
|
||||
err = jpeg.Encode(buf, timg, &(jpeg.Options{Quality: 80}))
|
||||
if err != nil {
|
||||
log.Println("[memegen] Image encode error: %s\n", err.Error())
|
||||
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
21
tg/broker.go
21
tg/broker.go
|
@ -1,6 +1,7 @@
|
|||
package tg
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -53,6 +54,26 @@ func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
|
|||
fmt.Fprintln(b.Socket, string(data))
|
||||
}
|
||||
|
||||
// SendTextMessage sends a HTML-styles text message to a specific chat.
|
||||
// A reply_to message ID can be specified as optional parameter.
|
||||
func (b *Broker) SendPhoto(chat *APIChat, data []byte, caption *string, original *int) {
|
||||
cmd := ClientCommand{
|
||||
Type: CmdSendPhoto,
|
||||
PhotoData: &ClientPhotoData{
|
||||
ChatID: chat.ChatID,
|
||||
Bytes: base64.StdEncoding.EncodeToString(data),
|
||||
Caption: caption,
|
||||
ReplyID: original,
|
||||
},
|
||||
}
|
||||
// Encode command and send to broker
|
||||
data, err := json.Marshal(cmd)
|
||||
if err != nil {
|
||||
log.Printf("[SendPhoto] JSON Encode error: %s\n", err.Error())
|
||||
}
|
||||
fmt.Fprintln(b.Socket, string(data))
|
||||
}
|
||||
|
||||
// GetFile sends a file retrieval request to the Broker.
|
||||
// This function is asynchronous as data will be delivered to the given callback.
|
||||
func (b *Broker) GetFile(fileID string, fn BrokerCallback) int {
|
||||
|
|
|
@ -16,7 +16,7 @@ type BrokerUpdate struct {
|
|||
Type BrokerUpdateType
|
||||
Callback *int
|
||||
Message *APIMessage
|
||||
Bytes []byte
|
||||
Bytes *string
|
||||
}
|
||||
|
||||
// ClientCommandType distinguishes requests sent by clients to the broker
|
||||
|
@ -40,6 +40,14 @@ type ClientTextMessageData struct {
|
|||
ReplyID *int
|
||||
}
|
||||
|
||||
// ClientPhotoData is the required data for a CmdSendPhoto request
|
||||
type ClientPhotoData struct {
|
||||
ChatID int
|
||||
Bytes string
|
||||
Caption *string
|
||||
ReplyID *int
|
||||
}
|
||||
|
||||
// FileRequestData is the required data for a CmdGetFile request
|
||||
type FileRequestData struct {
|
||||
FileID int
|
||||
|
@ -49,6 +57,7 @@ type FileRequestData struct {
|
|||
type ClientCommand struct {
|
||||
Type ClientCommandType
|
||||
TextMessageData *ClientTextMessageData
|
||||
PhotoData *ClientPhotoData
|
||||
FileRequestData *FileRequestData
|
||||
Callback *int
|
||||
}
|
||||
|
|
Reference in a new issue