-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.go
57 lines (47 loc) · 1.46 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
package main
import (
"context"
"fmt"
"os"
"github.com/mymmrac/telego"
th "github.com/mymmrac/telego/telegohandler"
tu "github.com/mymmrac/telego/telegoutil"
)
func main() {
ctx := context.Background()
botToken := os.Getenv("TOKEN")
// Note: Please keep in mind that default logger may expose sensitive information, use in development only
bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Get updates channel
updates, _ := bot.UpdatesViaLongPolling(ctx, nil)
// Create bot handler and specify from where to get updates
bh, _ := th.NewBotHandler(bot, updates)
// Register new handler with match on command `/start`
bh.Handle(func(ctx *th.Context, update telego.Update) error {
// Send message
_, _ = bot.SendMessage(ctx, tu.Messagef(
tu.ID(update.Message.Chat.ID),
"Hello %s!", update.Message.From.FirstName,
))
return nil
}, th.CommandEqual("start"))
// Register new handler with match on any command
// Handlers will match only once and in order of registration, so this handler will be called on any command except
// `/start` command
bh.Handle(func(ctx *th.Context, update telego.Update) error {
// Send message
_, _ = bot.SendMessage(ctx, tu.Message(
tu.ID(update.Message.Chat.ID),
"Unknown command, use /start",
))
return nil
}, th.AnyCommand())
// Stop handling updates
defer func() { _ = bh.Stop() }()
// Start handling updates
_ = bh.Start()
}