-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (132 loc) · 3.58 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"subaru/utility"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
if botToken == "" {
log.Fatal("TELEGRAM_BOT_TOKEN environment variable not set")
}
bot, err := tgbotapi.NewBotAPI(botToken)
if err != nil {
log.Panic(err)
}
// bot.Debug = true
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
shiftingMode := false
fileName := ""
for update := range updates {
msg := update.Message
if msg != nil {
if msg.IsCommand() {
handleStartCmd(bot, msg)
} else if msg.Document != nil {
err := handleDocument(bot, msg)
if err != nil {
log.Println(err)
continue
}
message := tgbotapi.NewMessage(msg.Chat.ID, "Ok, now send the time to timeshift in seconds.\ne.g.\n1000 -> for shifting 1s\n-1000 -> for shifting 1s back.")
bot.Send(message)
shiftingMode = true
fileName = msg.Document.FileName
} else if msg.IsCommand() == false {
if shiftingMode {
seconds, err := strconv.Atoi(strings.TrimSpace(msg.Text))
if err != nil {
log.Printf("Error converting string to int: %v", err)
message := tgbotapi.NewMessage(msg.Chat.ID, "Invalid number format. Please send an integer.")
bot.Send(message)
continue
}
utility.TimeShift(fileName, int64(seconds)) // error is HERE!!!!!!!!
if err = sendFile(bot, msg, fileName); err != nil {
log.Fatal(err)
}
shiftingMode = false
err = os.Remove(fileName)
if err != nil {
fmt.Println("Error:", err)
continue
}
fmt.Println("File removed successfully")
} else {
message := tgbotapi.NewMessage(msg.Chat.ID, "It is not subtitle file. Try again")
shiftingMode = false
bot.Send(message)
}
}
}
}
}
func handleStartCmd(bot *tgbotapi.BotAPI, message *tgbotapi.Message) {
txt := "Welcome. Send me subtitle file you want to timeshift."
msg := tgbotapi.NewMessage(message.Chat.ID, txt)
bot.Send(msg)
}
func handleDocument(bot *tgbotapi.BotAPI, message *tgbotapi.Message) error {
fileID := message.Document.FileID
fileName := message.Document.FileName
fileSize := message.Document.FileSize
fileMime := message.Document.MimeType
if fileMime == "application/x-subrip" || (fileMime == "text/plain" && strings.HasSuffix(fileName, ".srt")) {
log.Printf("Received file: %s (ID: %s, Size: %v)", fileName, fileID, fileSize)
// Get the file
file, err := bot.GetFile(tgbotapi.FileConfig{FileID: fileID})
if err != nil {
return err
}
fileURL := "https://api.telegram.org/file/bot" + bot.Token + "/" + file.FilePath
err = downloadFile(fileName, fileURL)
if err != nil {
return err
}
log.Println("file downloaded successfully")
} else {
txt := "It is not subtitle file. Try again"
msg := tgbotapi.NewMessage(message.Chat.ID, txt)
bot.Send(msg)
return fmt.Errorf("not srt")
}
return nil
}
func downloadFile(fileName, fileURL string) error {
resp, err := http.Get(fileURL)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(fileName)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func sendFile(bot *tgbotapi.BotAPI, message *tgbotapi.Message, fileName string) error {
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
doc := tgbotapi.NewDocument(message.Chat.ID, tgbotapi.FileReader{
Name: fileName,
Reader: file,
})
_, err = bot.Send(doc)
if err != nil {
return err
}
return nil
}