forked from mymmrac/telego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (44 loc) · 1.55 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
package main
import (
"fmt"
"os"
"github.com/mymmrac/telego"
th "github.com/mymmrac/telego/telegohandler"
)
func main() {
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)
}
updates, _ := bot.UpdatesViaLongPolling(nil)
bh, _ := th.NewBotHandler(bot, updates)
// Stop handling updates
defer bh.Stop()
// Stop getting updates
defer bot.StopLongPolling()
// Should not be here, because the order of handlers do meter.
//
// bh.HandleMessage(func(bot *telego.Bot, message telego.Message) {
// fmt.Println("Message:", message.Text)
// })
//
// When you are defining handlers only the first matched handler will process update, that means that in this
// specific example, matching any message will "shadow" any other handler that matches on messages.
// For any message it will match and handling of start command will never happen.
//
// The general idea is that you should define the most specific handlers first (like for specific command or etc.)
// and only then more generic handlers (like any message).
// Will match any message with command `/start`
bh.HandleMessage(func(bot *telego.Bot, message telego.Message) {
fmt.Println("Start")
}, th.CommandEqual("start"))
// Will match to any message
bh.HandleMessage(func(bot *telego.Bot, message telego.Message) {
fmt.Println("Message:", message.Text)
})
// Start handling
bh.Start()
}