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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"image"
|
"image"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -39,7 +41,7 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
|
||||||
if strings.HasPrefix(caption, "/meme ") && len(caption) > 6 {
|
if strings.HasPrefix(caption, "/meme ") && len(caption) > 6 {
|
||||||
idx := strings.Index(caption, ";")
|
idx := strings.Index(caption, ";")
|
||||||
if idx < 0 {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,9 +57,17 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
broker.GetFile(photo.FileID, func(broker *tg.Broker, data tg.BrokerUpdate) {
|
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 {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,6 +155,11 @@ func memegen(broker *tg.Broker, update tg.APIMessage) {
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
err = jpeg.Encode(buf, timg, &(jpeg.Options{Quality: 80}))
|
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
|
package tg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -53,6 +54,26 @@ func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
|
||||||
fmt.Fprintln(b.Socket, string(data))
|
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.
|
// GetFile sends a file retrieval request to the Broker.
|
||||||
// This function is asynchronous as data will be delivered to the given callback.
|
// This function is asynchronous as data will be delivered to the given callback.
|
||||||
func (b *Broker) GetFile(fileID string, fn BrokerCallback) int {
|
func (b *Broker) GetFile(fileID string, fn BrokerCallback) int {
|
||||||
|
|
|
@ -16,7 +16,7 @@ type BrokerUpdate struct {
|
||||||
Type BrokerUpdateType
|
Type BrokerUpdateType
|
||||||
Callback *int
|
Callback *int
|
||||||
Message *APIMessage
|
Message *APIMessage
|
||||||
Bytes []byte
|
Bytes *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientCommandType distinguishes requests sent by clients to the broker
|
// ClientCommandType distinguishes requests sent by clients to the broker
|
||||||
|
@ -40,6 +40,14 @@ type ClientTextMessageData struct {
|
||||||
ReplyID *int
|
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
|
// FileRequestData is the required data for a CmdGetFile request
|
||||||
type FileRequestData struct {
|
type FileRequestData struct {
|
||||||
FileID int
|
FileID int
|
||||||
|
@ -49,6 +57,7 @@ type FileRequestData struct {
|
||||||
type ClientCommand struct {
|
type ClientCommand struct {
|
||||||
Type ClientCommandType
|
Type ClientCommandType
|
||||||
TextMessageData *ClientTextMessageData
|
TextMessageData *ClientTextMessageData
|
||||||
|
PhotoData *ClientPhotoData
|
||||||
FileRequestData *FileRequestData
|
FileRequestData *FileRequestData
|
||||||
Callback *int
|
Callback *int
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue