-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
executable file
·65 lines (49 loc) · 1.21 KB
/
bot.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
package web
import (
"encoding/json"
"errors"
"github.com/flowline-io/flowbot/internal/bots"
"github.com/flowline-io/flowbot/internal/types"
"github.com/flowline-io/flowbot/pkg/flog"
"net/http"
)
const Name = "web"
var handler bot
func init() {
bots.Register(Name, &handler)
}
type bot struct {
initialized bool
bots.Base
}
type configType struct {
Enabled bool `json:"enabled"`
}
func (bot) Init(jsonconf json.RawMessage) error {
// Check if the handler is already initialized
if handler.initialized {
return errors.New("already initialized")
}
var config configType
if err := json.Unmarshal(jsonconf, &config); err != nil {
return errors.New("failed to parse config: " + err.Error())
}
if !config.Enabled {
flog.Info("bot %s disabled", Name)
return nil
}
handler.initialized = true
return nil
}
func (bot) IsReady() bool {
return handler.initialized
}
func (b bot) Input(_ types.Context, _ types.KV, _ interface{}) (types.MsgPayload, error) {
return nil, nil
}
func (b bot) Command(ctx types.Context, content interface{}) (types.MsgPayload, error) {
return bots.RunCommand(commandRules, ctx, content)
}
func (bot) Webapp() func(rw http.ResponseWriter, req *http.Request) {
return webapp
}