1
0
forked from x/icebergs
This commit is contained in:
harveyshao 2021-10-13 01:47:51 +08:00
parent 7a72e0cd72
commit b5c6ba7019
5 changed files with 162 additions and 0 deletions

117
cmd.go Normal file
View File

@ -0,0 +1,117 @@
package ice
import (
"reflect"
"strings"
kit "shylinux.com/x/toolkits"
)
func ref(obj interface{}) (reflect.Type, reflect.Value) {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
if t.Kind() == reflect.Ptr {
t, v = t.Elem(), v.Elem()
}
return t, v
}
func val(m *Message, arg ...string) []reflect.Value {
args := []reflect.Value{reflect.ValueOf(m)}
for _, v := range arg {
args = append(args, reflect.ValueOf(v))
}
return args
}
func transMethod(config *Config, command *Command, obj interface{}) {
t, v := ref(obj)
for i := 0; i < v.NumMethod(); i++ {
method := v.Method(i)
var h func(*Message, ...string)
switch method.Interface().(type) {
case func(*Message, ...string):
h = func(m *Message, arg ...string) { method.Call(val(m, arg...)) }
case func(*Message):
h = func(m *Message, arg ...string) { method.Call(val(m)) }
default:
continue
}
if key := strings.ToLower(t.Method(i).Name); key == "list" {
command.Hand = func(m *Message, c *Context, cmd string, arg ...string) { h(m, arg...) }
} else {
if action, ok := command.Action[key]; !ok {
command.Action[key] = &Action{Hand: h}
} else {
action.Hand = h
}
}
}
}
func transField(config *Config, command *Command, obj interface{}) {
t, v := ref(obj)
for i := 0; i < v.NumField(); i++ {
if t.Field(i).Type.Kind() == reflect.Struct {
if v.Field(i).CanInterface() {
transField(config, command, v.Field(i).Interface())
}
}
}
meta := kit.Value(config.Value, kit.MDB_META)
for i := 0; i < v.NumField(); i++ {
key, tag := t.Field(i).Name, t.Field(i).Tag
if data := tag.Get("data"); data != "" {
kit.Value(meta, key, data)
}
if name := tag.Get("name"); name != "" {
if help := tag.Get("help"); key == "list" {
command.Name, command.Help = name, help
config.Name, config.Help = name, help
} else if action, ok := command.Action[key]; ok {
action.Name, action.Help = name, help
}
}
}
}
func Cmd(key string, obj interface{}) {
if obj == nil {
return
}
command := &Command{Action: map[string]*Action{}}
config := &Config{Value: kit.Data()}
transMethod(config, command, obj)
transField(config, command, obj)
last := Index
list := strings.Split(key, ".")
for i := 1; i < len(list); i++ {
has := false
Pulse.Search(strings.Join(list[:i], ".")+".", func(p *Context, s *Context) {
has, last = true, s
})
if !has {
context := &Context{Name: list[i-1]}
last.Register(context, nil)
last = context
}
if i < len(list)-1 {
continue
}
last.Merge(&Context{Commands: map[string]*Command{
CTX_INIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) {
if action, ok := command.Action["init"]; ok {
action.Hand(m, arg...)
} else {
m.Load()
}
}},
CTX_EXIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) {
m.Save()
}},
list[i]: command,
}, Configs: map[string]*Config{list[i]: config}})
}
}

2
go.sum
View File

@ -17,3 +17,5 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
shylinux.com/x/toolkits v0.3.0 h1:rF9SIE02VCfgpuI2P/H7EoJaWpf+zomxAaRHiYD7yjE=
shylinux.com/x/toolkits v0.3.0/go.mod h1:8LbYHe7oxBIqb6s4MSOD+4d28QvPdvkyCVtwB/JW7AA=
shylinux.com/x/toolkits v0.3.3 h1:yNH54CANAv7cPT264tpnDt9G2BVfGwLC9y3rxtU6pkg=
shylinux.com/x/toolkits v0.3.3/go.mod h1:8LbYHe7oxBIqb6s4MSOD+4d28QvPdvkyCVtwB/JW7AA=

View File

@ -497,3 +497,7 @@ func SelectAction(list map[string]*Action, fields ...string) map[string]*Action
}
return res
}
func (m *Message) SetAppend(key ...string) {
m.Set(MSG_APPEND, key...)
}

38
misc/wework/bot.go Normal file
View File

@ -0,0 +1,38 @@
package wework
import (
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/web"
kit "shylinux.com/x/toolkits"
)
const BOT = "bot"
type Bot struct {
short string `data:"name"`
field string `data:"time,name,token,ekey,hook"`
create string `name:"list name token ekey hook" help:"创建"`
list string `name:"list name chat text:textarea auto create" help:"机器人"`
}
func (b Bot) Create(m *ice.Message, arg ...string) {
m.Cmdy(mdb.INSERT, m.PrefixKey(), "", mdb.HASH, arg)
}
func (b Bot) List(m *ice.Message, arg ...string) {
m.Fields(len(arg), m.Config(kit.MDB_FIELD))
m.Cmdy(mdb.SELECT, m.PrefixKey(), "", mdb.HASH, m.Config(kit.MDB_SHORT), arg)
if len(arg) > 2 {
m.Cmd(web.SPIDE, mdb.CREATE, arg[0], m.Append("hook"))
m.SetAppend()
m.Cmdy(web.SPIDE, arg[0], "", kit.Format(kit.Dict(
"chatid", arg[1],
"msgtype", "text", "text", kit.Dict(
"content", arg[2],
),
)))
}
}
func init() { ice.Cmd("web.chat.wework.bot", Bot{}) }

1
misc/wework/wework.go Normal file
View File

@ -0,0 +1 @@
package wework