47 lines
1.5 KiB
Go
47 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"encoding/base64"
|
|
|
|
"io/ioutil"
|
|
|
|
"github.com/hamcha/clessy/tg"
|
|
)
|
|
|
|
func stt(broker *tg.Broker, update tg.APIMessage) {
|
|
if isCommand(update, "stt") {
|
|
// Make replies work
|
|
if update.ReplyTo != nil && update.ReplyTo.Voice != nil {
|
|
update.Voice = update.ReplyTo.Voice
|
|
}
|
|
|
|
broker.SendTextMessage(update.Chat, fmt.Sprintf("Ok! Eccoti intanto delle informazioni sul file: \n<b>Durata: </b> %ds\n<b>Dimensione:</b> %d\n<b>Tipo:</b> %s", update.Voice.Duration, update.Voice.FileSize, *update.Voice.MimeType), &update.MessageID)
|
|
|
|
broker.GetFile(update.Voice.FileID, func(broker *tg.Broker, data tg.BrokerUpdate) {
|
|
if data.Type == tg.BError {
|
|
log.Printf("[stt] Received error from broker: %s\n", *data.Error)
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
|
return
|
|
}
|
|
|
|
bytes, err := base64.StdEncoding.DecodeString(*data.Bytes)
|
|
if err != nil {
|
|
log.Printf("[stt] Base64 decode error: %s\n", err.Error())
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
|
return
|
|
}
|
|
|
|
err = ioutil.WriteFile(update.Voice.FileID, bytes, 0755)
|
|
if err != nil {
|
|
log.Printf("[stt] Save to file error: %s\n", err.Error())
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
|
return
|
|
}
|
|
|
|
broker.SendTextMessage(update.Chat, fmt.Sprintf("Salvata registrazione su disco! (fname %s)", update.Voice.FileID), &update.MessageID)
|
|
})
|
|
}
|
|
}
|