-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (55 loc) · 1.79 KB
/
main.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"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/kbgod/lumex"
"github.com/kbgod/lumex/router"
"github.com/rs/zerolog"
)
var logger = zerolog.New(
zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
w.Out = os.Stderr
w.TimeFormat = time.RFC3339
}),
).With().Timestamp().Logger()
func main() {
bot, err := lumex.NewBot(os.Getenv("BOT_TOKEN"), nil)
if err != nil {
logger.Fatal().Err(err).Msg("failed to create bot")
}
logger.Info().Str("username", bot.User.Username).Msg("bot authorized successfully")
r := router.New(bot, router.WithErrorHandler(func(ctx *router.Context, err error) {
if err != nil {
logger.Error().Err(err).Interface("upd", ctx.Update).Msg("handle update error")
}
}))
r.OnStart(func(ctx *router.Context) error {
menu := lumex.NewInlineMenu()
var buttons []lumex.InlineKeyboardButton
for i := 0; i < 5; i++ {
sid := fmt.Sprintf("%d", i)
buttons = append(buttons, lumex.CallbackBtn("Product "+sid, "product:"+sid))
}
for i := 0; i < 5; i++ {
sid := fmt.Sprintf("%d", i)
buttons = append(buttons, lumex.CallbackBtn("Category "+sid, "category:"+sid))
}
menu.Fill(2, buttons...)
return ctx.ReplyWithMenuVoid("Menu", menu)
})
r.OnCallbackPrefix("product", func(ctx *router.Context) error {
return ctx.AnswerAlertVoid("You selected product " + ctx.ShiftCallbackData(":"))
})
r.OnCallbackPrefix("category", func(ctx *router.Context) error {
return ctx.AnswerAlertVoid("You selected category " + ctx.ShiftCallbackData(":"))
})
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
ctx := context.Background()
r.Listen(ctx, interrupt, 5*time.Second, 100, nil)
logger.Info().Str("username", bot.User.Username).Msg("bot stopped")
}