-
Notifications
You must be signed in to change notification settings - Fork 91
/
bot.go
208 lines (180 loc) · 5.8 KB
/
bot.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"mvdan.cc/xurls"
"os/exec"
"os"
"regexp"
log "github.com/sirupsen/logrus"
"strconv"
"strings"
)
var Bot *tgbotapi.BotAPI
var ChatActionHandler ChatActionManager
func init() {
var err error
Bot, err = tgbotapi.NewBotAPI(os.Getenv("BOT_API_TOKEN"))
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", Bot.Self.UserName)
BotAgentChatID, _ = strconv.ParseInt(os.Getenv("BOT_AGENT_CHAT_ID"), 10, 64)
}
// Chat id with agent(client that used for bypass limit in 50MB)
var BotAgentChatID int64
func BotMainLoop() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := Bot.GetUpdatesChan(u)
if err != nil {
log.Panic(err)
}
//var lastInfoMsg tgbotapi.Message
rxRelaxed := xurls.Relaxed()
go ChatActionHandler.ActionLoop()
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
if update.Message.IsCommand() {
if update.Message.Command() == "ping" {
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "pong"))
continue
}
if update.Message.Command() == "start" {
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Send me a video links"))
continue
}
}
// client sended to us video
if update.Message.Chat.ID == BotAgentChatID {
if update.Message.Video == nil && update.Message.Text != "" {
//No video then error occured
// check is message contain error string
splitedText := strings.Split(update.Message.Text, " ")
var chatID int64
var messageID int
if strings.Contains(splitedText[0], ":") {
captionWithID := strings.Split(splitedText[0], ":")
chatID, _ = strconv.ParseInt(captionWithID[0], 10, 64)
messageID, _ = strconv.Atoi(captionWithID[1])
} else {
chatID, err = strconv.ParseInt(splitedText[0], 10, 64)
}
if err == nil {
errMsg := tgbotapi.NewMessage(chatID, strings.Join(splitedText[1:], " "))
if messageID != 0 {
errMsg.ReplyToMessageID = messageID
}
Bot.Send(errMsg)
continue
}
} else {
// All good video was received
caption := strings.Split(update.Message.Caption, ":")
chatID, _ := strconv.ParseInt(caption[0], 10, 64)
messageID, _ := strconv.Atoi(caption[1])
if update.Message.Video != nil {
ShareVideoFile(update.Message.Video, chatID, messageID)
} else if update.Message.Audio != nil {
ShareAudioFile(update.Message.Audio, chatID, messageID)
} else if update.Message.Document != nil {
ShareDocumentFile(update.Message.Document, chatID, messageID)
}
continue
}
}
urls := rxRelaxed.FindAllString(update.Message.Text, 10)
if len(urls) == 0 {
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "There aren't any urls in your message.\nPlease send at least one."))
continue
}
command := update.Message.Command()
if command == "p" || command == "pa" || command == "pw" {
if len(urls) > 1{
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Only one playlist url allowed at time"))
continue
}
playlist_range_re := regexp.MustCompile(`([0-9]+)-([0-9]+)`)
submatches := playlist_range_re.FindStringSubmatch(update.Message.Text)
var start uint64 = 0
var end uint64 = 0
if len(submatches) > 0 {
start, _ = strconv.ParseUint(submatches[1], 10, 64)
end, _ = strconv.ParseUint(submatches[2], 10, 64)
if start >= end {
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Not correct format, start number must be less then end"))
continue
}
command += ":" + submatches[1] + "-" + submatches[2]
} else {
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong message format, correct example: /" + command + " 4-9 " + urls[0]))
continue
}
}
log.Info("Message is ", update.Message.Text)
go func(urls []string, chatID int64, messageID int) {
ChatActionHandler.AddAction(tgbotapi.NewChatAction(chatID, "upload_video"))
err = RequestHanlder(urls, strconv.FormatInt(chatID,10) + ":" + strconv.Itoa(messageID), command)
if err != nil {
log.Error("Request failed: ", err)
Bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Failed download video"))
}
ChatActionHandler.DelAction(chatID)
}(Deduplicate(urls), update.Message.Chat.ID, update.Message.MessageID)
}
}
func RequestHanlder(urls []string, chatNmessageID string, mode string) error {
urlsArg := strings.Join(urls," ")
_cmd := fmt.Sprintf(`python3 ./main.py %s '%s' %s 2>&1`, chatNmessageID, urlsArg, mode)
uploadCmd := exec.Command("bash", "-c", _cmd)
out, err := uploadCmd.Output()
if err != nil {
fmt.Println(err)
}
fmt.Println(_cmd, " ", string(out))
return nil
}
func ShareVideoFile(video *tgbotapi.Video, chatID int64, replyToMessageID int) {
log.Info("share video")
vidShare := tgbotapi.NewVideoShare(chatID, video.FileID)
vidShare.ReplyToMessageID = replyToMessageID
_, err := Bot.Send(vidShare)
if err != nil {
log.Error(err)
}
}
func ShareDocumentFile(doc *tgbotapi.Document, chatID int64, replyToMessageID int) {
log.Info("share doc")
docShare := tgbotapi.NewDocumentShare(chatID, doc.FileID)
docShare.ReplyToMessageID = replyToMessageID
_, err := Bot.Send(docShare)
if err != nil {
log.Error(err)
}
}
func ShareAudioFile(aud *tgbotapi.Audio, chatID int64, replyToMessageID int) {
log.Info("share audio")
docShare := tgbotapi.NewAudioShare(chatID, aud.FileID)
docShare.ReplyToMessageID = replyToMessageID
_, err := Bot.Send(docShare)
if err != nil {
log.Error(err)
}
}
// Deduplicate returns a new slice with duplicates values removed.
func Deduplicate(s []string) []string {
if len(s) <= 1 {
return s
}
result := []string{}
seen := make(map[string]struct{})
for _, val := range s {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = struct{}{}
}
}
return result
}