This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
205 lines (175 loc) · 5.53 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
package main
import (
"context"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rhiskey/spotytg/auths"
"github.com/rhiskey/spotytg/spotifydl"
"github.com/rhiskey/spotytg/structures"
"github.com/rhiskey/spotytg/utils"
"github.com/rollbar/rollbar-go"
"github.com/zmb3/spotify/v2"
"log"
"os"
"strings"
)
var (
ctx context.Context
spotifyClient *spotify.Client
bot *tgbotapi.BotAPI
apiEntity *structures.Api
)
var commandsKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("/status"),
tgbotapi.NewKeyboardButton("/help"),
),
//tgbotapi.NewKeyboardButtonRow(
// tgbotapi.NewKeyboardButton("4"),
// tgbotapi.NewKeyboardButton("5"),
// tgbotapi.NewKeyboardButton("6"),
//),
)
func init() {
rollbar.SetToken(os.Getenv("ROLLBAR_TOKEN"))
rollbar.SetEnvironment("production")
rollbar.SetCodeVersion("v1.1.4") // optional Git hash/branch/tag (required for GitHub integration)
//rollbar.SetServerHost("release.1") // optional override; defaults to hostname
rollbar.SetServerRoot("github.com/rhiskey/spotytg") // path of project (required for GitHub integration and non-project stacktrace collapsing) - where repo is set up for the project, the server.root has to be "/"
if os.Getenv("DEBUG") == "true" {
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer func(f *os.File) {
err = f.Close()
if err != nil {
log.Fatalf("error closing file: %v", err)
}
}(f)
log.SetOutput(f)
}
spotifyClient = auths.AuthSpotifyWithCreds()
ctx = context.Background()
var err error
bot, err = tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil {
rollbar.Critical(err)
panic(err)
}
if os.Getenv("DEBUG") == "true" {
bot.Debug = true
} else {
bot.Debug = false
}
log.Printf("📢 Authorized on account %s", bot.Self.UserName)
apiEntity = structures.NewApi(spotifyClient, bot)
}
func ProcessUrl(i int, playlistURL string, update tgbotapi.Update, msg tgbotapi.MessageConfig) {
spotifyClient = auths.AuthSpotifyWithCreds()
apiEntity.SpotifyClient = spotifyClient
savedFile, err := spotifydl.DonwloadFromURL(ctx, playlistURL, apiEntity)
if err != nil {
log.Print(err)
rollbar.Error(err)
return
}
log.Println(i)
file := tgbotapi.FilePath(savedFile)
sendAudioRequest := tgbotapi.NewAudio(update.Message.Chat.ID, file)
msg.Text = savedFile
if _, err := bot.Send(sendAudioRequest); err != nil {
rollbar.Error(err)
log.Panic(err)
}
e := os.Remove(savedFile)
if e != nil {
rollbar.Critical(err)
log.Fatal(e)
}
}
func handleUpdate(update tgbotapi.Update) {
if update.Message == nil { // ignore any non-Message updates
return
}
if update.CallbackQuery != nil {
// Respond to the callback query, telling Telegram to show the user
// a message with the data received.
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data)
if _, err := bot.Request(callback); err != nil {
rollbar.Error(err)
log.Panic(err)
}
// And finally, send a message containing the data received.
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Data)
if _, err := bot.Send(msg); err != nil {
rollbar.Error(err)
log.Panic(err)
}
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
apiEntity.TelegramMessageConfig = msg
if update.Message.IsCommand() {
// Extract the command from the Message.
switch update.Message.Command() {
case "start":
apiEntity.TelegramMessageConfig.ReplyMarkup = commandsKeyboard
utils.LogWithBot("🔗 Just send me a link that looks like: https://open.spotify.com/track/111111111111?si=xxxxxxxxx\nFeel free to use /help", apiEntity)
case "help":
apiEntity.TelegramMessageConfig.ReplyMarkup = commandsKeyboard
utils.LogWithBot("ℹ I understand:\n/status\n/send URL (aliases /s, /download /d, /play /p)\n/help\nOr just send me a link that looks like: https://open.spotify.com/track/", apiEntity)
case "status":
utils.LogWithBot("\U0001F9EA Beta test", apiEntity)
case "send", "download", "play", "s", "d", "p":
if len(update.Message.Entities) == 0 { // ignore any Message without Entities
return
}
cmds := update.Message.CommandArguments()
if len(cmds) == 0 {
utils.LogWithBot("\U0001F97A Missing URL after command, see /help.", apiEntity)
return
}
words := strings.Fields(cmds)
playlistURL := words[0]
go func(n int) {
ProcessUrl(n, playlistURL, update, msg)
}(update.Message.Date)
default:
utils.LogWithBot("😕 I dont know that command.", apiEntity)
}
}
switch update.Message.Text {
case "open":
msg.ReplyMarkup = commandsKeyboard
if _, err := bot.Send(msg); err != nil {
rollbar.Critical(err)
log.Panic(err)
}
case "close":
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
if _, err := bot.Send(msg); err != nil {
rollbar.Critical(err)
log.Panic(err)
}
default:
if len(update.Message.Entities) == 0 { // ignore any Message without Entities
return
}
if !update.Message.Entities[0].IsURL() { // ignore any Message without URL Entity type
return
}
playlistURL := update.Message.Text
go func(n int) {
ProcessUrl(n, playlistURL, update, msg)
}(update.Message.Date)
}
}
func main() {
updateConfig := tgbotapi.NewUpdate(0)
updateConfig.Timeout = 30
updates := bot.GetUpdatesChan(updateConfig)
for update := range updates {
go func(update tgbotapi.Update) {
handleUpdate(update)
}(update)
}
}