diff --git a/mods/memegen.go b/mods/memegen.go
index 487d269..5f1831c 100644
--- a/mods/memegen.go
+++ b/mods/memegen.go
@@ -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, "Formato: /meme TESTO IN ALTO;TESTO IN BASSO", &(update.MessageID))
+ broker.SendTextMessage(update.Chat, "Formato: /meme TESTO IN ALTO;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, "ERROR: Non riesco a leggere l'immagine", &(update.MessageID))
+ log.Println("[memegen] Base64 decode error: %s\n", err.Error())
+ broker.SendTextMessage(update.Chat, "ERRORE! @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, "ERROR: 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, "ERRORE! @hamcha controlla la console!", &update.MessageID)
+ return
+ }
})
}
}
diff --git a/tg/broker.go b/tg/broker.go
index 90792fb..ddeb86f 100644
--- a/tg/broker.go
+++ b/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 {
diff --git a/tg/command.go b/tg/command.go
index 0c6a12c..7ec9161 100644
--- a/tg/command.go
+++ b/tg/command.go
@@ -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
}