-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (204 loc) · 7.12 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
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
209
210
211
212
213
214
215
216
217
218
package main
import (
"os"
"io/ioutil"
"encoding/json"
"log"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
type d_transmission struct{
Url string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
}
type config struct {
Token string `json:"token"`
AllowedIDs []int `json:"allowed_ids"`
Transmission d_transmission `json:"transmission"`
}
type cmdConfigs struct {
Yts CmdConfigYts
Transmission CmdConfigTransmission
}
var Commandssl cmdConfigs
func main() {
if len(os.Args) != 2 {
log.Fatalln("Usage: ./ytsbot <CONFIGFILE>")
}
cfg, err := parseConfig(os.Args[1])
if err != nil{
log.Fatalln(err)
}
bot, err := tgbotapi.NewBotAPI(cfg.Token)
if err != nil {
log.Panic(err)
}
NewCmdYts(&Commandssl.Yts)
NewCmdTransmission(&Commandssl.Transmission, cfg.Transmission.Url,cfg.Transmission.Username,cfg.Transmission.Password)
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
go handle_updates(bot, update, cfg)
}
}
func handle_updates(bot *tgbotapi.BotAPI, update tgbotapi.Update, cfg config) {
if update.Message!=nil{
if !allowed_ids(update.Message.From.ID, cfg.AllowedIDs){
log.Println("User " + update.Message.From.FirstName + " blocked! Alias: " + update.Message.From.UserName)
return
}
log.Printf("Message -> [%s] %s (id: %d, alias: %s)", update.Message.From.FirstName, update.Message.Text, update.Message.Chat.ID, update.Message.From.UserName)
var msg tgbotapi.MessageConfig
if Commandssl.Yts.Reg_rand.MatchString(update.Message.Text){
YtsSetPage(update.Message.Text)
msg = YtsNewKeyboard(update.Message.Chat.ID)
}else if Commandssl.Yts.Reg_detail.MatchString(update.Message.Text){
img, m, torrent, err := YtsDetail(update.Message.Text)
if err != nil{
log.Println(err)
return
}
if img != ""{
msg = tgbotapi.NewMessage(update.Message.Chat.ID, img)
msg.DisableWebPagePreview = false
bot.Send(msg)
}
msg = tgbotapi.NewMessage(update.Message.Chat.ID, m)
if torrent != ""{
var DownloadKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Download", "download_torrent_" + torrent),
),
)
msg.ReplyMarkup = DownloadKeyboard
}
}else if Commandssl.Yts.Reg_search.MatchString(update.Message.Text){
m, err := YtsSearch(update.Message.Text)
if err != nil{
log.Println(err)
return
}
msg = tgbotapi.NewMessage(update.Message.Chat.ID, m)
}else if Commandssl.Transmission.St_list == update.Message.Text{
m, err := BtList()
if err != nil{
log.Println(err)
return
}
msg = tgbotapi.NewMessage(update.Message.Chat.ID, m)
}else if Commandssl.Transmission.St_clean == update.Message.Text{
m, err := BtClean()
if err != nil{
log.Println(err)
return
}
msg = tgbotapi.NewMessage(update.Message.Chat.ID, m)
}else if Commandssl.Transmission.Reg_del.MatchString(update.Message.Text){
m, err := BtRemove(update.Message.Text)
if err != nil{
log.Println(err)
return
}
msg = tgbotapi.NewMessage(update.Message.Chat.ID, "Operation result: " + m)
}else{
m := "Command not found!"
msg = tgbotapi.NewMessage(update.Message.Chat.ID, m)
msg.ReplyToMessageID = update.Message.MessageID
}
msg.DisableWebPagePreview = false
bot.Send(msg)
}else if update.CallbackQuery != nil {
if !allowed_ids(update.CallbackQuery.From.ID, cfg.AllowedIDs){
log.Println("User " + update.CallbackQuery.From.FirstName + " blocked! Alias: " + update.CallbackQuery.From.UserName)
return
}
callback := tgbotapi.CallbackConfig{
CallbackQueryID: update.CallbackQuery.ID,
}
log.Printf("Callback -> [%s] %s (id: %d, alias: %s)", update.CallbackQuery.From.FirstName,
update.CallbackQuery.Data, update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.From.UserName)
if ((len(update.CallbackQuery.Data) > 3) && (update.CallbackQuery.Data[:4] == "YTS_")){
m, err := YtsRandom(update.CallbackQuery.Data[4:])
if err != nil{
log.Println(err)
return
}
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, m)
var MoreKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("More...", update.CallbackQuery.Data),
),
)
msg.ReplyMarkup = MoreKeyboard
bot.Send(msg)
}else if ((len(update.CallbackQuery.Data) > 20) && (update.CallbackQuery.Data[:17] == "download_torrent_")){
/*var DoneKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Done!", ""),
),
)
//msg := tgbotapi.NewEditMessageReplyMarkup(update.CallbackQuery.Message.Chat.ID,update.CallbackQuery.Message.MessageID , DoneKeyboard)
msg :=
tgbotapi.NewEditMessageCaption(update.CallbackQuery.Message.Chat.ID,update.CallbackQuery.Message.MessageID,"LEL")
bot.Send(msg)*/
callback.Text = "Downloading movie..." + BtDown(update.CallbackQuery.Data[17:])
callback.ShowAlert = true
}
if _, err := bot.AnswerCallbackQuery(callback); err != nil {
log.Println(err)
}
}else{
log.Println("Unknown query received: ", update)
}
}
func YtsNewKeyboard(id int64) tgbotapi.MessageConfig {
var CategoryKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Action", "YTS_Action"),
tgbotapi.NewInlineKeyboardButtonData("Adventure", "YTS_Adventure"),
tgbotapi.NewInlineKeyboardButtonData("Animation", "YTS_Animation"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Comedy", "YTS_Comedy"),
tgbotapi.NewInlineKeyboardButtonData("Documentary", "YTS_Documentary"),
tgbotapi.NewInlineKeyboardButtonData("Drama", "YTS_Drama"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Fantasy", "YTS_Fantasy"),
tgbotapi.NewInlineKeyboardButtonData("Horror", "YTS_Horror"),
tgbotapi.NewInlineKeyboardButtonData("Mystery", "YTS_Mystery"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Sport", "YTS_Sport"),
tgbotapi.NewInlineKeyboardButtonData("Sci-Fi", "YTS_Sci-fi"),
tgbotapi.NewInlineKeyboardButtonData("Thriller", "YTS_Thriller"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("All", "YTS_"),
),
)
msg := tgbotapi.NewMessage(id, "Select a category:")
msg.ReplyMarkup = CategoryKeyboard
return msg
}
func parseConfig(file string) (config, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
return config{}, err
}
var cfg config
if err := json.Unmarshal(b, &cfg); err != nil {
return config{}, err
}
log.Println("Config: ", cfg)
return cfg, nil
}
func allowed_ids(a int, b []int) bool{
for _, i := range b{
if i == a{return true}
}
return false
}