Skip to content

Commit

Permalink
feat: add a way of starting the bot with account manager
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 5, 2018
1 parent e237b53 commit 618bf99
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 46 deletions.
4 changes: 4 additions & 0 deletions core/.scripts/generate-logger.sh
Expand Up @@ -5,6 +5,10 @@ for pkg in $(git grep func\ | grep .go: | cut -d: -f1 | uniq | xargs -n1 dirname
logname="core."$(echo $pkg | tr / .)
pkgname=$(basename $pkg)

if [ "$pkg" = "api/node/graphql/models" ]; then
logname="vendor.graphql.models"
fi

if grep "package main" $pkg/*.go >/dev/null 2>/dev/null; then
pkgname="main"
fi
Expand Down
2 changes: 1 addition & 1 deletion core/api/node/graphql/models/logger.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/bot/bot.go
Expand Up @@ -2,6 +2,7 @@ package bot

import (
"context"
"fmt"
"io"
"time"

Expand Down Expand Up @@ -39,7 +40,10 @@ func (b *Bot) Start() error {
return err
}
for {
fmt.Println("for")
time.Sleep(1000 * time.Millisecond)
event, err := stream.Recv()
fmt.Println("event", event, err)
if err == io.EOF {
break
}
Expand Down
16 changes: 16 additions & 0 deletions core/bot/generic.go
@@ -0,0 +1,16 @@
package bot

import (
"fmt"

"berty.tech/core/entity"
)

func GenericOptions() []Option {
return []Option{
WithAutoAcceptInvites(),
WithMessageHandlerFunc(func(b *Bot, e *Event, msg *entity.Message) error {
return b.Reply(e, &entity.Message{Text: fmt.Sprintf("hello! (%s)", msg.Text)})
}),
}
}
9 changes: 9 additions & 0 deletions core/bot/logger.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions core/bot/option.go
Expand Up @@ -2,8 +2,8 @@ package bot

import (
"encoding/json"
"log"

"go.uber.org/zap"
grpc "google.golang.org/grpc"

"berty.tech/core/api/client"
Expand Down Expand Up @@ -56,11 +56,18 @@ func WithAutoAcceptInvites() Option {
}
}

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)
log.Println("received event", string(out))
logger().Debug("received event", zap.String("event", string(out)))
return nil
})
return nil
Expand Down
6 changes: 6 additions & 0 deletions core/cmd/berty/daemon.go
Expand Up @@ -19,6 +19,7 @@ type daemonOptions struct {
hideBanner bool `mapstructure:"hide-banner"`
dropDatabase bool `mapstructure:"drop-database"`
initOnly bool `mapstructure:"init-only"`
withBot bool `mapstructure:"with-bot"`

// p2p
identity string `mapstructure:"identity"`
Expand All @@ -41,6 +42,7 @@ func daemonSetupFlags(flags *pflag.FlagSet, opts *daemonOptions) {
flags.BoolVar(&opts.initOnly, "init-only", false, "stop after node initialization (useful for integration tests")
flags.BoolVar(&opts.noP2P, "no-p2p", false, "Disable p2p Driver")
flags.BoolVar(&opts.hop, "hop", false, "enable relay hop (should not be enable for client)")
flags.BoolVar(&opts.withBot, "bot", false, "enable bot")
flags.BoolVar(&opts.mdns, "mdns", true, "enable mdns discovery")
flags.StringVar(&opts.grpcBind, "grpc-bind", ":1337", "gRPC listening address")
flags.StringVar(&opts.gqlBind, "gql-bind", ":8700", "Bind graphql api")
Expand Down Expand Up @@ -112,6 +114,10 @@ func daemon(opts *daemonOptions) error {
accountOptions = append(accountOptions, account.WithEnqueurNetwork())
}

if opts.withBot {
accountOptions = append(accountOptions, account.WithBot())
}

if opts.initOnly {
accountOptions = append(accountOptions, account.WithInitOnly())
}
Expand Down
12 changes: 2 additions & 10 deletions core/go.mod
Expand Up @@ -2,7 +2,6 @@ module berty.tech/core

require (
cloud.google.com/go v0.30.0 // indirect
github.com/0xAX/notificator v0.0.0-20171022182052-88d57ee9043b // indirect
github.com/99designs/gqlgen v0.6.0
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
Expand All @@ -11,16 +10,14 @@ require (
github.com/brianvoe/gofakeit v3.13.0+incompatible
github.com/btcsuite/btcd v0.0.0-20181013004428-67e573d211ac // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/codegangsta/envy v0.0.0-20141216192214-4b78388c8ce4 // indirect
github.com/codegangsta/gin v0.0.0-20171026143024-cafe2ce98974 // indirect
github.com/coreos/go-semver v0.2.0 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/fd/go-nat v1.0.0 // indirect
github.com/go-check/check v0.0.0-20180628173108-788fd7840127 // indirect
github.com/go-gormigrate/gormigrate v1.2.1
github.com/go-sql-driver/mysql v1.4.0 // indirect
github.com/gogo/protobuf v1.1.1
github.com/golang/lint v0.0.0-20181011164241-5906bd5c48cd // indirect
github.com/golang/protobuf v1.2.0
github.com/google/go-cmp v0.2.0 // indirect
github.com/google/uuid v1.0.0 // indirect
Expand Down Expand Up @@ -51,6 +48,7 @@ require (
github.com/joho/godotenv v1.3.0 // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0 // indirect
github.com/libp2p/go-addr-util v2.0.6+incompatible // indirect
github.com/libp2p/go-buffer-pool v0.1.1 // indirect
Expand Down Expand Up @@ -92,7 +90,6 @@ require (
github.com/libp2p/go-ws-transport v2.0.14+incompatible // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mattn/go-shellwords v1.0.3 // indirect
github.com/mattn/go-sqlite3 v1.9.0 // indirect
github.com/miekg/dns v1.0.13 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
Expand Down Expand Up @@ -139,16 +136,11 @@ require (
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e // indirect
golang.org/x/lint v0.0.0-20181011164241-5906bd5c48cd // indirect
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1
golang.org/x/oauth2 v0.0.0-20181003184128-c57b0facaced // indirect
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba // indirect
golang.org/x/tools v0.0.0-20181013182035-5e66757b835f // indirect
google.golang.org/appengine v1.2.0 // indirect
google.golang.org/genproto v0.0.0-20181004005441-af9cb2a35e7f // indirect
google.golang.org/grpc v1.15.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
gopkg.in/urfave/cli.v1 v1.20.0 // indirect
honnef.co/go/tools v0.0.0-20180920025451-e3ad64cb4ed3 // indirect
)

0 comments on commit 618bf99

Please sign in to comment.