/
view_cmd_list_sources.go
54 lines (45 loc) · 1.28 KB
/
view_cmd_list_sources.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
package bot
import (
"context"
"fmt"
"github.com/to77e/news-fetching-bot/internal/botkit/markup"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/to77e/news-fetching-bot/internal/botkit"
"github.com/to77e/news-fetching-bot/internal/models"
)
type SourceLister interface {
Sources(ctx context.Context) ([]*models.Source, error)
}
func ViewCmdListSources(lister SourceLister) botkit.ViewFunc {
return func(ctx context.Context, bot *tgbotapi.BotAPI, update tgbotapi.Update) error {
sources, err := lister.Sources(ctx)
if err != nil {
return fmt.Errorf("list sources: %w", err)
}
var sourceInfos []string
for _, v := range sources {
src := formatSource(v)
sourceInfos = append(sourceInfos, src)
}
msgText := fmt.Sprintf(
"List sources \\(total %d\\):\n\n%s",
len(sources),
strings.Join(sourceInfos, "\n\n"),
)
reply := tgbotapi.NewMessage(update.Message.Chat.ID, msgText)
reply.ParseMode = parseModeMarkdownV2
if _, err := bot.Send(reply); err != nil {
return fmt.Errorf("send message: %w", err)
}
return nil
}
}
func formatSource(source *models.Source) string {
return fmt.Sprintf(
"*%s*\nID: `%d`\nfeed URL: %s",
markup.EscapeForMarkdown(source.Name),
source.ID,
markup.EscapeForMarkdown(source.URL),
)
}