-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (59 loc) · 1.56 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
package main
import (
"context"
"os"
"path"
"github.com/andersnormal/autobot/pkg/plugins"
"github.com/andersnormal/autobot/pkg/plugins/runtime"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var helloRuntime = &runtime.Runtime{
RunE: runE,
}
func init() {
runtime.OnInitialize(initConfig)
}
func initConfig() {
// set some default flags
pflag.String("name", path.Base(os.Args[0]), "plugin name")
pflag.String("log_format", runtime.DefaultLogFormat, "log format")
pflag.String("log_level", runtime.DefaultLogLevel, "log level")
pflag.BoolP("verbose", "v", false, "verbose")
pflag.BoolP("debug", "d", false, "debug")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
// unmarshal to config
if err := viper.Unmarshal(runtime.Env()); err != nil {
log.Fatalf(errors.Wrap(err, "cannot unmarshal runtime").Error())
}
}
func runE(env *runtime.Environment) error {
// have root context ...
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// plugin ....
plugin, _ := plugins.WithContext(ctx, env)
// log ..
plugin.Log().Infof("starting hello world plugin ...")
// Processing incoming messages ...
msgFunc := func(ctx plugins.Context) error {
ctx.Send(ctx.Message().Reply("hello world"))
return nil
}
// use the schedule function from the plugin
if err := plugin.ReplyWithFunc(msgFunc); err != nil {
return err
}
if err := plugin.Wait(); err != nil {
return err
}
return nil
}
func main() {
if err := helloRuntime.Execute(); err != nil {
log.Fatal(err)
}
}