-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.go
67 lines (54 loc) · 1.38 KB
/
run.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
package main
import (
"context"
"net/http"
"log/slog"
"time"
"github.com/iliazeus/achievement-bot/internal/sticker"
"github.com/iliazeus/achievement-bot/internal/tg"
_ "github.com/joho/godotenv/autoload"
)
func run(slog *slog.Logger, botToken string, tempChatId int) error {
stickerMaker, err := sticker.NewStickerMaker()
if err != nil {
return err
}
client, err := tg.NewClient(http.DefaultClient, botToken)
if err != nil {
return err
}
slog.Info("starting update loop")
return client.RunUpdateLoop(context.Background(), func(ctx context.Context, upd *tg.Update, err error) {
if err != nil {
slog.Error(err.Error())
time.Sleep(1 * time.Second)
return
}
slog := slog.With("update_id", upd.UpdateID)
slog.Debug("got update")
switch {
case upd.InlineQuery != nil:
query := upd.InlineQuery
slog := slog.With("inline_query.id", query.ID)
slog.Info("got inline query", "query", query.Query)
sticker, err := stickerMaker.MakeSticker(query.Query)
if err != nil {
slog.Error(err.Error())
return
}
msg, err := client.SendSticker(ctx, tempChatId, sticker)
if err != nil {
slog.Error(err.Error())
return
}
err = client.AnswerInlineQuery(
ctx, query.ID,
tg.InlineQueryAnswer{ID: 0, Type: "sticker", StickerFileID: msg.Sticker.FileID},
)
if err != nil {
slog.Error(err.Error())
return
}
}
})
}