/
option.go
75 lines (64 loc) · 1.39 KB
/
option.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
75
package bot
import (
"encoding/json"
"go.uber.org/zap"
grpc "google.golang.org/grpc"
"berty.tech/core/api/client"
"berty.tech/core/api/p2p"
"berty.tech/core/entity"
)
type Option func(b *Bot) error
func WithTCPDaemon(addr string) Option {
return func(b *Bot) error {
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
return err
}
return WithGRPCConn(conn)(b)
}
}
func WithDefaultDaemon() Option {
return WithTCPDaemon("127.0.0.1:1337")
}
func WithGRPCConn(conn *grpc.ClientConn) Option {
return func(b *Bot) error {
b.client = client.New(conn)
return nil
}
}
func WithClient(client *client.Client) Option {
return func(b *Bot) error {
b.client = client
return nil
}
}
func WithAutoAcceptInvites() Option {
return func(b *Bot) error {
b.AddHandlerFunc(func(b *Bot, e *Event) error {
if e.Kind != p2p.Kind_ContactRequest {
return nil
}
_, err := b.client.Node().ContactAcceptRequest(e.ctx, &entity.Contact{
ID: e.SenderID,
})
return err
})
return nil
}
}
func WithMessageHandlerFunc(f MessageHandlerFunc) Option {
return func(b *Bot) error {
b.AddMessageHandlerFunc(f)
return nil
}
}
func WithLogger() Option {
return func(b *Bot) error {
b.AddHandlerFunc(func(_ *Bot, e *Event) error {
out, _ := json.Marshal(e)
logger().Debug("received event", zap.String("event", string(out)))
return nil
})
return nil
}
}