-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (88 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"github.com/cavaliergopher/grab/v3"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
ffmpeg "github.com/u2takey/ffmpeg-go"
"log"
"os"
)
func main() {
apiToken, ok := os.LookupEnv("TELEGRAM_APITOKEN")
if !ok {
panic("No TELEGRAM_APITOKEN environment variable found.")
}
bot, err := tgbotapi.NewBotAPI(apiToken)
if err != nil {
panic(err)
}
whisperModel := GetModel()
defer whisperModel.Close()
bot.Debug = true
// Create a new UpdateConfig struct with an offset of 0. Offsets are used
// to make sure Telegram knows we've handled previous values and we don't
// need them repeated.
updateConfig := tgbotapi.NewUpdate(0)
// Tell Telegram we should wait up to 30 seconds on each request for an
// update. This way we can get information just as quickly as making many
// frequent requests without having to send nearly as many.
updateConfig.Timeout = 30
// Start polling Telegram for updates.
updates := bot.GetUpdatesChan(updateConfig)
// Let's go through each update that we're getting from Telegram.
for update := range updates {
// Discard if not message.
if update.Message == nil {
continue
}
// Discard if not voice message.
if update.Message.Voice == nil {
continue
}
file, err := bot.GetFileDirectURL(update.Message.Voice.FileID)
if err != nil {
log.Fatal(err)
}
//TODO: Don't save to a temporary file, replace with a pipe to ffmpeg
resp, err := grab.Get("tmp/", file)
if err != nil {
log.Fatal(err)
}
fmt.Println("Download saved to", resp.Filename)
voiceMsgFilename := resp.Filename
err = ffmpeg.Input(voiceMsgFilename).
Output("tmp/tmp.wav",
ffmpeg.KwArgs{"acodec": "pcm_s16le", "ac": "1", "ar": "16000"}).
OverWriteOutput().ErrorToStdOut().Run()
if err != nil {
log.Fatal(err)
}
samples, err := GetSamplesFromFilePath("tmp/tmp.wav")
if err != nil {
log.Printf("Unfortunately this happened: %v", err)
continue
}
recognizedText, err := ProcessSamples(whisperModel, samples)
if err != nil {
log.Printf("Error while processing samples from wave file: %v", err)
continue
}
// Now that we know we've gotten a new message, we can construct a
// reply! We'll take the Chat ID and Text from the incoming message
// and use it to create a new message.
if recognizedText != "" {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, recognizedText)
// We'll also say that this message is a reply to the previous message.
// For any other specifications than Chat ID or Text, you'll need to
// set fields on the `MessageConfig`.
msg.ReplyToMessageID = update.Message.MessageID
// Okay, we're sending our message off! We don't care about the message
// we just sent, so we'll discard it.
if _, err := bot.Send(msg); err != nil {
log.Printf("Error sending message: %v", err) //TODO: Handle and retry?
}
} else {
log.Println("Nothing recognized, nothing to send.")
}
}
}