/
handlers.go
120 lines (97 loc) · 2.97 KB
/
handlers.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
package bot
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/ShoshinNikita/log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ShoshinNikita/tg-to-rss-bot/internal/params"
"github.com/ShoshinNikita/tg-to-rss-bot/internal/youtube"
)
const helpMsg = `It is a bot, that adds received videos from YouTube into RSS feed
Commands:
/help – get help
/link – get link to RSS feed
Bot repo: https://github.com/ShoshinNikita/tg-to-rss-bot`
func (b *Bot) start(msg *tgbotapi.Message) {
b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "For start send link to a YouTube video"))
}
func (b *Bot) help(msg *tgbotapi.Message) {
b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, helpMsg))
}
func (b *Bot) sendLink(msg *tgbotapi.Message) {
b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, params.Host+"/feed"))
}
func (b *Bot) wrongCommand(msg *tgbotapi.Message) {
b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "Error: wrong command"))
}
func (b *Bot) video(msg *tgbotapi.Message) {
stringURL := msg.Text
// Add http:// or https://
if !(strings.HasPrefix(stringURL, "http://") || strings.HasPrefix(stringURL, "https://")) {
stringURL = "https://" + stringURL
}
u, err := url.ParseRequestURI(stringURL)
if err != nil {
b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "❌ invalid link"))
return
}
id := func() string {
// Full link (https://youtube.com/watch?v=VIDEO_ID)
res := u.Query().Get("v")
if res != "" {
return res
}
// Short link (https://youtu.be/VIDEO_ID)
s := strings.Split(u.Path, "/")
return s[len(s)-1]
}()
v, err := youtube.NewVideo(id)
if err != nil {
log.Errorf("can't process video with ID \"%s\": %s\n", id, err)
b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "❌ can't get video meta data: "+err.Error()))
return
}
msgText := fmt.Sprintf("Video: %s\n", v.Title)
botMsg, err := b.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, msgText))
// Check error because we will use botMsg.MessageID
if err != nil {
log.Errorf("can't send a message: %s\n", err)
return
}
msgID := botMsg.MessageID
status := ""
ok := false
for m := range v.Download() {
switch {
case m.IsFatalError:
ok = false
status = "❌ " + m.Msg
case m.IsFinished:
ok = true
status = "✅ " + m.Msg
default:
status = "* " + m.Msg
}
msgText += "\n " + status
b.bot.Send(tgbotapi.NewEditMessageText(msg.Chat.ID, msgID, msgText))
if m.IsFatalError || m.IsFinished {
break
}
}
if ok {
// Add into feed
audioLink := params.DataFolder + v.Filename
err := b.feed.Add(v.Author, v.Title, v.Description, audioLink, time.Now())
if err != nil {
log.Errorf("can't add item into RSS feed: %s\n", err)
msgText += "\n " + "❌ can't add item into RSS feed: " + err.Error()
b.bot.Send(tgbotapi.NewEditMessageText(msg.Chat.ID, msgID, msgText))
} else {
log.Infof("add new item. Title: \"%s\"\n", v.Title)
msgText += "\n " + "✅ add new item into RSS feed"
b.bot.Send(tgbotapi.NewEditMessageText(msg.Chat.ID, msgID, msgText))
}
}
}