-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (109 loc) · 2.74 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
token := os.Getenv("TG_BOT_API_TOKEN")
userID, err := strconv.ParseInt(os.Getenv("USER_ID"), 10, 64)
if err != nil {
userID = 0
}
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
updateConfig := tgbotapi.NewUpdate(0)
updateConfig.Timeout = 60
updates := bot.GetUpdatesChan(updateConfig)
for update := range updates {
if update.Message == nil {
continue
}
if userID != 0 && update.Message.From.ID != userID {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Sorry, this is a private bot. Visit [github.com/jheuel/whisper_bot](https://github.com/jheuel/whisper_bot) to find out how to host your own instance.")
msg.ReplyToMessageID = update.Message.MessageID
msg.ParseMode = "markdown"
_, err = bot.Send(msg)
if err != nil {
log.Println("error handling message:", err)
continue
}
}
if update.Message.Document != nil || update.Message.Voice != nil || update.Message.Audio != nil {
err := handleFile(update, bot)
if err != nil {
log.Println("error handling message:", err)
continue
}
}
}
}
func handleFile(update tgbotapi.Update, bot *tgbotapi.BotAPI) error {
fileID := ""
if update.Message.Document != nil {
fileID = update.Message.Document.FileID
}
if update.Message.Voice != nil {
fileID = update.Message.Voice.FileID
}
if update.Message.Audio != nil {
fileID = update.Message.Audio.FileID
}
fileURL, err := bot.GetFileDirectURL(fileID)
if err != nil {
return err
}
log.Println("fileURL:", fileURL)
response, err := http.Get(fileURL)
if err != nil {
return err
}
defer response.Body.Close()
voiceDir := "/app/data/voice/"
err = os.MkdirAll(voiceDir, 0777)
if err != nil {
return err
}
fileName := filepath.Base(fileURL)
fileName = strings.ReplaceAll(fileName, "%20", " ")
savedFile := voiceDir + fileName
outFile, err := os.Create(savedFile)
if err != nil {
return err
}
defer outFile.Close()
_, err = io.Copy(outFile, response.Body)
if err != nil {
return err
}
python := "/app/venv/bin/python"
args := []string{"transcribe.py", savedFile}
out, err := exec.Command(python, args...).Output()
if err != nil {
return err
}
output := string(out)
log.Println("Transcription:", output)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, output)
msg.ReplyToMessageID = update.Message.MessageID
_, err = bot.Send(msg)
if err != nil {
return err
}
return nil
}