-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
195 lines (166 loc) · 5.06 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
package main
import (
"log"
"github.com/camelva/erzo"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
var BotPhrase = Responses.Get("en")
func main() {
config := loadConfig("config.yml")
bot, err := tgbotapi.NewBotAPI(config.Telegram.Token)
if err != nil {
log.Fatal(err)
}
bot.Debug = false
log.Printf("Authorized on account %s\n", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
var msg *tgbotapi.Message
if update.Message != nil {
msg = update.Message
} else if update.ChannelPost != nil {
msg = update.ChannelPost
} else {
continue
}
dbMsgID := reportMessage(msg)
if err := handleMessage(bot, msg); err != nil {
handleError(bot, msg, err, dbMsgID)
}
}
}
func handleError(bot *tgbotapi.BotAPI, msg *tgbotapi.Message, err error, dbMsgID int) {
var responseMsg string
var errCode int
switch err.(type) {
case erzo.ErrNotURL:
responseMsg = BotPhrase.ErrNotURL()
err = nil // its not error
log.Println("Received message without link. Responding...")
case erzo.ErrUnsupportedService:
responseMsg = BotPhrase.ErrUnsupportedService()
errCode = 10
log.Println("Received message with link from unsupported service. Responding...")
case erzo.ErrUnsupportedProtocol:
// almost similar for user but we need to report about it
responseMsg = BotPhrase.ErrUnsupportedService()
errCode = 31
case erzo.ErrUnsupportedType:
if err.(erzo.ErrUnsupportedType).Format == "playlist" {
responseMsg = BotPhrase.ErrPlaylist()
} else {
responseMsg = BotPhrase.ErrUnsupportedFormat()
}
errCode = 11
log.Println("Received message with unsupported url type. Responding...")
case erzo.ErrCantFetchInfo:
// most of the time, can't fetch if song is unavailable, and that's what we respond to user
// we don't really need to report this error to analytic, but lets keep it for more verbose
responseMsg = BotPhrase.ErrUnavailableSong()
errCode = 20
case erzo.ErrDownloadingError:
// it means we fetched all info, but could not download it. Tell user to try again
responseMsg = BotPhrase.ErrUndefined()
errCode = 30
case erzo.ErrUndefined:
responseMsg = BotPhrase.ErrUndefined()
errCode = 99
default:
responseMsg = BotPhrase.ErrUndefined()
errCode = 99
}
if err != nil && err.Error() == "Request Entity Too Large" {
responseMsg = "Looks like this song weighs too much.\n" +
"Telegram limits uploading files size to 50mb and we can't avoid this limit.\n" +
"Please try another one"
errCode = 19
}
if err != nil {
reportError(dbMsgID, err, errCode)
}
if msg.Chat.Type != "private" {
return
}
sendMessage(bot, msg.Chat.ID, responseMsg)
}
func handleMessage(bot *tgbotapi.BotAPI, message *tgbotapi.Message) error {
// Update responses language first
language := "en"
if message.From != nil {
language = message.From.LanguageCode
}
BotPhrase = Responses.Get(language)
var isPrivateChat = message.Chat.Type == "private"
var tmpMessageID int
var chatID = message.Chat.ID
log.Println("Received new message", message.Text)
if message.IsCommand() {
response := checkForCommands(message)
sendMessage(bot, chatID, response)
return nil
}
if isPrivateChat {
tmpMessageID = sendMessage(bot, chatID, BotPhrase.ProcessStart())
}
defer deleteTempMessage(bot, message.Chat, tmpMessageID)
songFile, err := erzo.Get(message.Text, erzo.Truncate(true))
if err != nil {
return err
}
log.Println("Downloaded song. Uploading to user...")
if isPrivateChat {
// update old temp message
tmpMessageID = sendMessage(bot, chatID, BotPhrase.ProcessUploading(), tmpMessageID)
}
// Inform user about uploading
// but we don't care about possible errors
_, _ = bot.Send(tgbotapi.NewChatAction(chatID, "upload_audio"))
audioMsg := tgbotapi.NewAudioUpload(chatID, songFile)
// and only then send song file
if _, err := bot.Send(audioMsg); err != nil {
return err
}
log.Println("Waiting for another message ~_~")
return nil
}
func checkForCommands(message *tgbotapi.Message) (response string) {
log.Println("Received command -", message.Text, ".Responding...")
switch message.Command() {
case "help":
return BotPhrase.CmdHelp()
case "start":
return BotPhrase.CmdStart()
default:
return BotPhrase.CmdUnknown()
}
}
func sendMessage(bot *tgbotapi.BotAPI, chatID int64, text string, oldMsgContainer ...int) (msgID int) {
var msgObj tgbotapi.Chattable
if oldMsgContainer != nil {
// Edit old message instead of creating new
oldMsgID := oldMsgContainer[0]
msgObj = tgbotapi.NewEditMessageText(chatID, oldMsgID, text)
} else {
msgObj = tgbotapi.NewMessage(chatID, text)
}
// two tries
for range make([]int, 2) {
sentMsg, err := bot.Send(msgObj)
if err != nil {
continue
}
return sentMsg.MessageID
}
return 0
}
func deleteTempMessage(bot *tgbotapi.BotAPI, chat *tgbotapi.Chat, messageID int) {
if chat.Type != "private" {
return
}
msgToDelete := tgbotapi.NewDeleteMessage(chat.ID, messageID)
if _, err := bot.DeleteMessage(msgToDelete); err != nil {
log.Printf("error while deleting temp message: %s", err)
}
}