Skip to content

Commit 6c79c2d

Browse files
committed
Add paths from env variables, support string telegram channel names
1 parent dbeb8da commit 6c79c2d

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Flags:
1515
--interval int How often to output transcoding status (default 5)
1616
--keep-old Keep old version of video if transcoded version is larger (default true)
1717
--log string The log level to output (default "info")
18+
--nice Whether to lower the priority of ffmpeg process (default true)
1819
--stderr Whether to output ffmpeg stderr stream
1920
--tg-bot-key string Telegram Bot API Key
20-
--tg-chat-id int Telegram Bot Chat ID
21+
--tg-chat-id string Telegram Bot Chat ID
2122
```

cmd/root.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"errors"
54
"github.com/Vilsol/transcoder-go/config"
65
"github.com/Vilsol/transcoder-go/models"
76
"github.com/Vilsol/transcoder-go/notifications"
@@ -49,16 +48,18 @@ var rootCmd = &cobra.Command{
4948
config.InitializeConfig()
5049
notifications.InitializeNotifications()
5150
},
52-
Args: func(cmd *cobra.Command, args []string) error {
53-
if len(args) < 1 {
54-
return errors.New("must supply at least a single path")
55-
}
56-
57-
return nil
58-
},
5951
Run: func(cmd *cobra.Command, args []string) {
6052
fileList := make([]string, 0)
6153

54+
if len(args) == 0 {
55+
args = viper.GetStringSlice("paths")
56+
}
57+
58+
if len(args) == 0 {
59+
log.Fatalf("You must supply at least a single path via CLI argument or PATHS env variable")
60+
return
61+
}
62+
6263
for _, arg := range args {
6364
files, err := filepath.Glob(arg)
6465

@@ -71,6 +72,10 @@ var rootCmd = &cobra.Command{
7172
fileList = append(fileList, files...)
7273
}
7374

75+
if len(fileList) == 0 {
76+
log.Error("Specified paths did not match any files")
77+
}
78+
7479
for _, fileName := range fileList {
7580
if terminated {
7681
return
@@ -215,7 +220,7 @@ func init() {
215220
rootCmd.PersistentFlags().Bool("nice", true, "Whether to lower the priority of ffmpeg process")
216221

217222
rootCmd.PersistentFlags().String("tg-bot-key", "", "Telegram Bot API Key")
218-
rootCmd.PersistentFlags().Int64("tg-chat-id", 0, "Telegram Bot Chat ID")
223+
rootCmd.PersistentFlags().String("tg-chat-id", "", "Telegram Bot Chat ID")
219224

220225
_ = viper.BindPFlag("flags", rootCmd.PersistentFlags().Lookup("flags"))
221226
_ = viper.BindPFlag("extensions", rootCmd.PersistentFlags().Lookup("extensions"))

notifications/telegram.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"github.com/go-telegram-bot-api/telegram-bot-api"
88
log "github.com/sirupsen/logrus"
99
"github.com/spf13/viper"
10+
"strconv"
1011
"time"
1112
)
1213

1314
var tgBot *tgbotapi.BotAPI
1415

1516
func init() {
1617
initialize = append(initialize, func() {
17-
if viper.GetString("tg-bot-key") != "" && viper.GetInt64("tg-chat-id") != 0 {
18+
if viper.GetString("tg-bot-key") != "" && viper.GetString("tg-chat-id") != "" {
1819
var err error
1920
tgBot, err = tgbotapi.NewBotAPI(viper.GetString("tg-bot-key"))
2021

@@ -28,8 +29,24 @@ func init() {
2829

2930
lastMessage := int64(0)
3031

32+
chatIdStr := viper.GetString("tg-chat-id")
33+
34+
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
35+
36+
if err != nil {
37+
chat, err := tgBot.GetChat(tgbotapi.ChatConfig{
38+
SuperGroupUsername: chatIdStr,
39+
})
40+
41+
if err != nil {
42+
log.Fatalf("Chat not found: %s", chatIdStr)
43+
}
44+
45+
chatId = chat.ID
46+
}
47+
3148
start = append(start, func(data *models.NotificationData) {
32-
message := tgbotapi.NewMessage(viper.GetInt64("tg-chat-id"), generateTelegramMessageText(data, nil))
49+
message := tgbotapi.NewMessage(chatId, generateTelegramMessageText(data, nil))
3350
message.ParseMode = tgbotapi.ModeMarkdown
3451
send, err := tgBot.Send(message)
3552

@@ -49,7 +66,7 @@ func init() {
4966
}
5067

5168
if currentMessage != nil {
52-
message := tgbotapi.NewEditMessageText(viper.GetInt64("tg-chat-id"), currentMessage.MessageID, generateTelegramMessageText(data, nil))
69+
message := tgbotapi.NewEditMessageText(chatId, currentMessage.MessageID, generateTelegramMessageText(data, nil))
5370
message.ParseMode = tgbotapi.ModeMarkdown
5471
_, err := tgBot.Send(message)
5572

@@ -63,7 +80,7 @@ func init() {
6380

6481
end = append(end, func(data *models.NotificationData, result models.Result) {
6582
if currentMessage != nil {
66-
message := tgbotapi.NewEditMessageText(viper.GetInt64("tg-chat-id"), currentMessage.MessageID, generateTelegramMessageText(data, &result))
83+
message := tgbotapi.NewEditMessageText(chatId, currentMessage.MessageID, generateTelegramMessageText(data, &result))
6784
message.ParseMode = tgbotapi.ModeMarkdown
6885
_, err := tgBot.Send(message)
6986

0 commit comments

Comments
 (0)