forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
141 lines (126 loc) · 4.86 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"syscall"
qrterminal "github.com/mdp/qrterminal/v3"
"github.com/oklog/run"
"go.uber.org/zap"
"moul.io/srand"
"moul.io/u"
"moul.io/zapconfig"
"berty.tech/berty/v2/go/pkg/bertybot"
"berty.tech/berty/v2/go/pkg/bertyversion"
)
var (
username = u.CurrentUsername("anon")
node1Addr = flag.String("addr1", "127.0.0.1:9091", "first remote 'berty daemon' address")
node2Addr = flag.String("addr2", "127.0.0.1:9092", "second remote 'berty daemon' address")
displayName1 = flag.String("name1", username+" (testbot1)", "first bot's display name")
displayName2 = flag.String("name2", username+" (testbot2)", "second bot's display name")
debug = flag.Bool("debug", false, "debug mode")
logFormat = flag.String("log-format", "console", strings.Join(zapconfig.AvailablePresets, ", "))
)
func main() {
flag.Parse()
rand.Seed(srand.MustSecure())
if err := Main(); err != nil {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
os.Exit(1)
}
}
type TestBot struct {
Bot1, Bot2 *bertybot.Bot
ctx context.Context
logger *zap.Logger
}
func Main() error {
config := zapconfig.Configurator{}
config.SetPreset(*logFormat)
logger := config.MustBuild()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// init testbot
testbot := &TestBot{ctx: ctx, logger: logger}
if err := testbot.InitBot1(); err != nil {
return fmt.Errorf("init bot 1: %w", err)
}
if err := testbot.InitBot2(); err != nil {
return fmt.Errorf("init bot 2: %w", err)
}
// FIXME: bot1 and bot2 should be contact
// start testbot
var g run.Group
g.Add(func() error { return testbot.Bot1.Start(ctx) }, func(error) { cancel() })
g.Add(func() error { return testbot.Bot2.Start(ctx) }, func(error) { cancel() })
g.Add(run.SignalHandler(ctx, syscall.SIGKILL))
return g.Run()
}
func (testbot *TestBot) VersionCommand(ctx bertybot.Context) {
_ = ctx.ReplyString("version: " + bertyversion.Version)
// FIXME: also returns the version of the remote messenger and protocol
}
// InitBot1 initializes the entrypoint bot
func (testbot *TestBot) InitBot1() error {
logger := testbot.logger.Named("bot1")
// init bot
opts := []bertybot.NewOption{}
opts = append(opts,
bertybot.WithLogger(logger.Named("lib")), // configure a logger
bertybot.WithDisplayName(*displayName1), // bot name
bertybot.WithInsecureMessengerGRPCAddr(*node1Addr), // connect to running berty messenger daemon
bertybot.WithRecipe(bertybot.AutoAcceptIncomingContactRequestRecipe()), // accept incoming contact requests
bertybot.WithRecipe(bertybot.WelcomeMessageRecipe("welcome to testbot1")), // send welcome message to new contacts and new conversations
bertybot.WithRecipe(bertybot.EchoRecipe("you said1: ")), // reply to messages with the same message
// FIXME: with auto-send `/help` suggestion on welcome
bertybot.WithCommand("version", "show version", testbot.VersionCommand),
)
if *debug {
opts = append(opts, bertybot.WithRecipe(bertybot.DebugEventRecipe(logger.Named("debug")))) // debug events
}
bot, err := bertybot.New(opts...)
if err != nil {
return fmt.Errorf("bot initialization failed: %w", err)
}
// display link and qr code
logger.Info("retrieve instance Berty ID",
zap.String("pk", bot.PublicKey()),
zap.String("link", bot.BertyIDURL()),
)
qrterminal.GenerateHalfBlock(bot.BertyIDURL(), qrterminal.L, os.Stdout)
testbot.Bot1 = bot
return nil
}
// InitBot2 initializes the companion bot
func (testbot *TestBot) InitBot2() error {
logger := testbot.logger.Named("bot2")
// init bot
opts := []bertybot.NewOption{}
opts = append(opts,
bertybot.WithLogger(logger.Named("lib")), // configure a logger
bertybot.WithDisplayName(*displayName2), // bot name
bertybot.WithInsecureMessengerGRPCAddr(*node2Addr), // connect to running berty messenger daemon
bertybot.WithRecipe(bertybot.AutoAcceptIncomingContactRequestRecipe()), // accept incoming contact requests
bertybot.WithRecipe(bertybot.WelcomeMessageRecipe("welcome to testbot2")), // send welcome message to new contacts and new conversations
bertybot.WithRecipe(bertybot.EchoRecipe("you said2: ")), // reply to messages with the same message
)
if *debug {
opts = append(opts, bertybot.WithRecipe(bertybot.DebugEventRecipe(logger.Named("debug")))) // debug events
}
bot, err := bertybot.New(opts...)
if err != nil {
return fmt.Errorf("bot initialization failed: %w", err)
}
// display link and qr code
logger.Info("retrieve instance Berty ID",
zap.String("pk", bot.PublicKey()),
zap.String("link", bot.BertyIDURL()),
)
qrterminal.GenerateHalfBlock(bot.BertyIDURL(), qrterminal.L, os.Stdout)
testbot.Bot2 = bot
return nil
}