-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrouter_example_test.go
More file actions
60 lines (49 loc) · 1.26 KB
/
router_example_test.go
File metadata and controls
60 lines (49 loc) · 1.26 KB
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
package tgbot_test
import (
"context"
"flag"
"log"
"os"
tgbot "github.com/olebedev/go-tgbot"
"github.com/olebedev/go-tgbot/client/messages"
"github.com/olebedev/go-tgbot/models"
)
var token *string
func ExampleNew() {
token = flag.String("token", "", "telegram bot token")
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := tgbot.New(ctx, *token)
// setup global middleware
r.Use(tgbot.Recover)
r.Use(tgbot.Logger(os.Stdout))
// modify path to be able to match user's commands via router
r.Use(func(c *tgbot.Context) error {
c.Path = c.Path + c.Text
return nil
})
// bind handler
r.Bind(`^/message/(?:.*)/text/start(?:\s(.*))?$`, func(c *tgbot.Context) error {
log.Println(c.Capture) // - ^ from path
log.Println(c.Update.Message.Text) // or c.Text
// send greeting message back
message := "hi there what's up"
resp, err := r.Messages.SendMessage(
messages.NewSendMessageParams().WithBody(&models.SendMessageBody{
Text: &message,
ChatID: c.Update.Message.Chat.ID,
}),
)
if err != nil {
return err
}
if resp != nil {
log.Println(resp.Payload.Result.MessageID)
}
return nil
})
if err := r.Poll(ctx, models.AllowedUpdateMessage); err != nil {
log.Fatal(err)
}
}