/
source.go
54 lines (43 loc) · 1.42 KB
/
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
package bot
import (
"context"
"fmt"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ndrewnee/lesswrong-bot/models"
)
var sourceKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Lesswrong.ru", "1"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Slate Start Codex", "2"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Astral Codex Ten", "3"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Lesswrong.com", "4"),
),
)
func (b *Bot) ChangeSource(ctx context.Context, userID int, newSource models.Source) (string, interface{}, error) {
key := fmt.Sprintf("source:%d", userID)
sourceValue, err := b.storage.Get(ctx, key)
if err != nil {
log.Printf("[ERROR] Get source failed: %s, key: %s", err, key)
}
source := models.Source(sourceValue)
if !source.IsValid() {
source = models.SourceLesswrongRu
}
if newSource == "" {
return "Current source is " + source.String(), sourceKeyboard, nil
}
if !newSource.IsValid() {
return "New source is invalid. Current source is " + source.String(), sourceKeyboard, nil
}
if err := b.storage.Set(ctx, key, newSource.Value(), 0); err != nil {
return "", nil, fmt.Errorf("set source failed: %s, key: %s, source: %s", err, key, newSource)
}
return "Changed source to " + newSource.String(), nil, nil
}