-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (37 loc) · 987 Bytes
/
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
// Binary bg-run implements alternative to Run pattern.
package main
import (
"context"
"go.uber.org/zap"
"github.com/gotd/contrib/bg"
"github.com/gotd/td/examples"
"github.com/gotd/td/telegram"
)
func main() {
// Some users find explicit client.Run(ctx, f) pattern not very convenient.
//
// However, it is possible to implement wrapper and use classic "Connect"
// pattern instead.
//
// The `contrib/bg` package is example implementation of such pattern.
examples.Run(func(ctx context.Context, log *zap.Logger) error {
client, err := telegram.ClientFromEnvironment(telegram.Options{
Logger: log,
})
if err != nil {
return err
}
// bg.Connect will call Run in background.
// Call stop() to disconnect and release resources.
stop, err := bg.Connect(client)
if err != nil {
return err
}
defer func() { _ = stop() }()
// Now you can use client.
if _, err := client.Auth().Status(ctx); err != nil {
return err
}
return nil
})
}