-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.go
74 lines (56 loc) · 1.56 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
68
69
70
71
72
73
74
package main
import (
"fmt"
"os"
"github.com/mymmrac/telego"
th "github.com/mymmrac/telego/telegohandler"
)
// ==== DEPRECATED ====
// See example handler_groups_and_middleware/main.go for a proper way of middleware implementation.
// ====================
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)
}
// Get updates channel
updates, _ := bot.UpdatesViaLongPolling(nil)
// Create bot handler and specify from where to get updates
bh, _ := th.NewBotHandler(bot, updates)
// Stop handling updates
defer bh.Stop()
// Stop getting updates
defer bot.StopLongPolling()
// Define auth predicate
auth := func(update telego.Update) bool {
var userID int64
// Get user ID from the message
if update.Message != nil && update.Message.From != nil {
userID = update.Message.From.ID
}
// Get user ID from the callback query
if update.CallbackQuery != nil {
userID = update.CallbackQuery.From.ID
}
// Reject if no user
if userID == 0 {
return false
}
// Accept
if userID == 1234 {
return true
}
return false
}
bh.Handle(func(bot *telego.Bot, update telego.Update) {
// DO AUTHORIZED STUFF...
}, auth) // Check for authorization
bh.Handle(func(bot *telego.Bot, update telego.Update) {
// DO NOT AUTHORIZED STUFF...
}, th.Not(auth)) // Process unauthorized update
// Start handling updates
bh.Start()
}