/
golyrics-telegram.go
156 lines (134 loc) · 3.38 KB
/
golyrics-telegram.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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/mamal72/golyrics"
"github.com/tucnak/telebot"
)
func getTrackTitle(track *golyrics.Track) string {
return fmt.Sprintf("%s:%s", track.Artist, track.Name)
}
func onStart(bot *telebot.Bot, message *telebot.Message) {
bot.SendMessage(
message.Chat,
fmt.Sprintf(`Hello, %s!
Send me a track name to get lyrics of it! 🎵`, message.Sender.FirstName),
nil,
)
}
func onHelp(bot *telebot.Bot, message *telebot.Message) {
bot.SendMessage(
message.Chat,
"Just send me a track name to get lyrics of it! 🎵",
nil,
)
}
func onError(bot *telebot.Bot, message *telebot.Message, err error) {
fmt.Println(err)
bot.SendMessage(
message.Chat,
"An error happened! ☹️",
nil,
)
}
func onNotFound(bot *telebot.Bot, message *telebot.Message, query string) {
bot.SendMessage(
message.Chat,
fmt.Sprintf("No lyrics found for \"%s\"! ☹️", query),
nil,
)
}
func onNoTextMessage(bot *telebot.Bot, message *telebot.Message) {
bot.SendMessage(
message.Chat,
"I only understand text messages. ☹️",
nil,
)
}
func sendLyrics(bot *telebot.Bot, message *telebot.Message, track *golyrics.Track) {
if len(track.Lyrics) == 0 {
track.Lyrics = fmt.Sprintf("No lyrics found for %s:%s! ☹️\nIt may be an album, not a track!", track.Artist, track.Name)
}
bot.SendMessage(
message.Chat,
fmt.Sprintf("🎵 *%s* by *%s*:\n\n%s", track.Name, track.Artist, track.Lyrics),
&telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
},
)
}
func sendSuggestions(bot *telebot.Bot, message *telebot.Message, query string, suggestions *[]golyrics.Track) {
replyKeyboard := [][]string{}
for _, track := range *suggestions {
trackTitle := getTrackTitle(&track)
replyKeyboard = append(replyKeyboard, []string{trackTitle})
}
bot.SendMessage(
message.Chat,
fmt.Sprintf("Found *%d* items for \"*%s*\". Which one is it?", len(*suggestions), query),
&telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
ReplyMarkup: telebot.ReplyMarkup{
ForceReply: true,
OneTimeKeyboard: true,
CustomKeyboard: replyKeyboard,
},
},
)
}
func main() {
// Load ENV vars from .env file
godotenv.Load()
botToken := os.Getenv("BOT_TOKEN")
if len(botToken) == 0 {
log.Fatalln("No Telegram bot token (BOT_TOKEN) provided in .env file or ENV variables")
}
bot, err := telebot.NewBot(botToken)
if err != nil {
log.Fatalln(err)
}
messages := make(chan telebot.Message, 100)
bot.Listen(messages, 30*time.Second)
for message := range messages {
messageText := strings.TrimSpace(message.Text)
// Only text messages
if len(messageText) == 0 {
onNoTextMessage(bot, &message)
continue
}
// Bot started
if message.Text == "/start" {
onStart(bot, &message)
continue
}
// Bot help
if message.Text == "/help" {
onHelp(bot, &message)
continue
}
suggestions, err := golyrics.SearchTrack(message.Text)
// On error
if err != nil {
onError(bot, &message, err)
continue
}
// On not found
if len(suggestions) == 0 {
onNotFound(bot, &message, messageText)
continue
}
trackTitle := getTrackTitle(&suggestions[0])
if len(suggestions) > 1 && trackTitle != messageText {
sendSuggestions(bot, &message, messageText, &suggestions)
continue
}
track := suggestions[0]
track.FetchLyrics()
sendLyrics(bot, &message, &track)
continue
}
}