/
view_cmd_add_source.go
56 lines (45 loc) · 1.31 KB
/
view_cmd_add_source.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
package bot
import (
"context"
"fmt"
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"
)
const parseModeMarkdownV2 = "MarkdownV2"
type SourceRepository interface {
Add(ctx context.Context, source models.Source) (int64, error)
}
func ViewCmdAddSource(storage SourceRepository) botkit.ViewFunc {
type addSourceArgs struct {
Name string `json:"name"`
URL string `json:"url"`
Priority int `json:"priority"`
}
return func(ctx context.Context, bot *tgbotapi.BotAPI, update tgbotapi.Update) error {
args, err := botkit.ParseJSON[addSourceArgs](update.Message.CommandArguments())
if err != nil {
return fmt.Errorf("parse JSON: %w", err)
}
source := models.Source{
Name: args.Name,
URL: args.URL,
}
sourceID, err := storage.Add(ctx, source)
if err != nil {
return fmt.Errorf("add source: %w", err)
}
var (
msgText = fmt.Sprintf(
"Source added with ID: `%d`\\. Use this ID for updating the source or deleting it\\.",
sourceID,
)
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
}
}