-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
79 lines (60 loc) · 1.53 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package github
import (
"encoding/json"
"errors"
"github.com/flowline-io/flowbot/internal/bots"
"github.com/flowline-io/flowbot/internal/ruleset/cron"
"github.com/flowline-io/flowbot/internal/types"
"github.com/flowline-io/flowbot/pkg/flog"
)
const Name = "github"
var handler bot
var Config configType
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")
}
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 (bot) Bootstrap() error {
// load setting rule
formRules = append(formRules, bots.SettingCovertForm(Name, settingRules))
return nil
}
func (b bot) Rules() []interface{} {
return []interface{}{
commandRules,
formRules,
}
}
func (b bot) Command(ctx types.Context, content interface{}) (types.MsgPayload, error) {
return bots.RunCommand(commandRules, ctx, content)
}
func (b bot) Cron(send types.SendFunc) (*cron.Ruleset, error) {
return bots.RunCron(cronRules, Name, send)
}
func (b bot) Form(ctx types.Context, values types.KV) (types.MsgPayload, error) {
return bots.RunForm(formRules, ctx, values)
}