80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"encoding/base64"
|
|
|
|
speech "cloud.google.com/go/speech/apiv1"
|
|
"github.com/hamcha/clessy/tg"
|
|
"google.golang.org/api/option"
|
|
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
|
|
)
|
|
|
|
var sttClient *speech.Client
|
|
var sttCtx context.Context
|
|
|
|
func initstt() {
|
|
sttCtx = context.Background()
|
|
var err error
|
|
sttClient, err = speech.NewClient(sttCtx, option.WithServiceAccountFile(*gapifile))
|
|
if err != nil {
|
|
panic(fmt.Errorf("Could not initialize Google Speech API client: %s", err.Error()))
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
if update.Voice.Duration > 20 {
|
|
broker.SendTextMessage(update.Chat, "L'audio dura un bel po' (>20s), al momento non posso farci nulla :S", &update.MessageID)
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
resp, err := sttClient.Recognize(sttCtx, &speechpb.RecognizeRequest{
|
|
Config: &speechpb.RecognitionConfig{
|
|
Encoding: speechpb.RecognitionConfig_OGG_OPUS,
|
|
SampleRateHertz: 16000,
|
|
LanguageCode: "it-IT",
|
|
},
|
|
Audio: &speechpb.RecognitionAudio{
|
|
AudioSource: &speechpb.RecognitionAudio_Content{Content: bytes},
|
|
},
|
|
})
|
|
if err != nil {
|
|
log.Printf("[stt] STT request error: %s\n", err.Error())
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
|
return
|
|
}
|
|
|
|
out := "Questo è quello che ho capito:\n"
|
|
for _, result := range resp.Results {
|
|
for _, alt := range result.Alternatives {
|
|
out += fmt.Sprintf("\"%v\"", alt.Transcript, alt.Confidence)
|
|
}
|
|
}
|
|
|
|
broker.SendTextMessage(update.Chat, out, &update.MessageID)
|
|
})
|
|
}
|
|
}
|