This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gochatbot-multi.go
94 lines (81 loc) · 1.87 KB
/
gochatbot-multi.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
// +build all,multi multi
package main // import "cirello.io/gochatbot"
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"sync"
"cirello.io/gochatbot/bot"
"cirello.io/gochatbot/brain"
"cirello.io/gochatbot/providers"
"cirello.io/gochatbot/rules/cron"
"cirello.io/gochatbot/rules/plugins"
"cirello.io/gochatbot/rules/regex"
"cirello.io/gochatbot/rules/rpc"
)
func main() {
var wg sync.WaitGroup
var botCount int
wd, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalln("plugins: error detecting working directory:", err)
}
for {
e := &envGet{botCount}
if e.getenv("GOCHATBOT_NAME") == "" {
break
}
wg.Add(1)
go func(e *envGet) {
name := e.getenv("GOCHATBOT_NAME")
if name == "" {
name = "gochatbot"
}
provider := providers.Detect(e.getenv)
if err := provider.Error(); err != nil {
log.SetOutput(os.Stderr)
log.Fatalln("error in message provider:", err)
}
memory := brain.Detect(e.getenv)
if err := memory.Error(); err != nil {
log.SetOutput(os.Stderr)
log.Fatalln("error in brain memory:", err)
}
options := []bot.Option{
bot.MessageProvider(provider),
bot.RegisterRuleset(regex.New(regexRules)),
bot.RegisterRuleset(cron.New(cronRules)),
bot.RegisterRuleset(plugins.New(wd)),
}
rpcHostAddr := e.getenv("GOCHATBOT_RPC_BIND")
if rpcHostAddr != "" {
l, err := net.Listen("tcp4", rpcHostAddr)
if err != nil {
log.Fatalf("rpc: cannot bind. err: %v", err)
}
options = append(
options,
bot.RegisterRuleset(rpc.New(l)),
)
}
bot.New(
name,
memory,
options...,
).Process()
wg.Done()
}(e)
botCount++
}
wg.Wait()
}
type envGet struct {
idx int
}
func (e envGet) getenv(key string) string {
newKey := strings.Replace(key, "GOCHATBOT_", fmt.Sprint("GOCHATBOT_", e.idx, "_"), 1)
return os.Getenv(newKey)
}