This commit is contained in:
cdle
2021-09-29 12:21:30 +08:00
parent 7bead09766
commit 72cf7a22fd
5 changed files with 140 additions and 1 deletions
+2 -1
View File
@@ -9,4 +9,5 @@ jd_wskey
logs/*
jd_*
data
dwz
dwz
replies
+1
View File
@@ -13,6 +13,7 @@ func init() {
// fmt.Println(replies)
var template = `
var content = {{1}}
var data = request
`
fmt.Println(template)
+129
View File
@@ -0,0 +1,129 @@
package core
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"github.com/beego/beego/v2/adapter/httplib"
"github.com/beego/beego/v2/adapter/logs"
"github.com/robertkrimen/otto"
)
type JsReply string
func init() {
files, err := ioutil.ReadDir(ExecPath + "/develop")
if err != nil {
logs.Warn("打开文件夹%s错误,%v", ExecPath+"/develop", err)
return
}
for _, v := range files {
jr := string(ExecPath + "/develop/" + v.Name())
data := ""
if strings.Contains(jr, "http") {
data, err = httplib.Get(jr).String()
if err != nil {
logs.Warn("回复:%s获取失败%v", jr, err)
continue
}
} else {
f, err := os.Open(jr)
if err != nil {
logs.Warn("回复:%s打开失败%v", jr, err)
continue
}
v, _ := ioutil.ReadAll(f)
data = string(v)
}
rules := []string{}
for _, v := range regexp.MustCompile(`\[rule:\s*([^\s\[\]]+)\s*\]`).FindAllStringSubmatch(data, -1) {
rules = append(rules, v[1])
}
if len(rules) == 0 {
logs.Warn("回复:%s找不到规则", jr, err)
continue
}
request := func(call otto.Value) interface{} {
url := ""
dataType := ""
method := "get"
body := ""
{
v, _ := call.Object().Get("url")
url = v.String()
}
{
v, _ := call.Object().Get("dataType")
dataType = v.String()
}
{
v, _ := call.Object().Get("body")
body = v.String()
}
var req *httplib.BeegoHTTPRequest
switch strings.ToLower(method) {
case "delete":
req = httplib.Delete(url)
case "post":
req = httplib.Post(url)
case "put":
req = httplib.Put(url)
default:
req = httplib.Get(url)
}
if body != "" {
req.Body(body)
}
data, err := req.String()
if err != nil {
return nil
}
if strings.Contains(dataType, "json") {
obj, _ := otto.New().Object(fmt.Sprintf(`(%s)`, data))
return obj
}
result, _ := otto.ToValue(data)
return result
}
var handler = func(s Sender) interface{} {
template := data
for k, v := range s.GetMatch() {
template = strings.Replace(template, fmt.Sprintf(`param(%d)`, k+1), fmt.Sprintf(`"%s"`, v), -1)
}
vm := otto.New()
vm.Set("request", request)
vm.Set("sendText", func(call otto.Value) interface{} {
s.Reply(call.String())
return nil
})
vm.Set("sendImage", func(call otto.Value) interface{} {
s.Reply(ImageUrl(call.String()))
return nil
})
rt, err := vm.Run(template + `
""
`)
result := rt.String()
if err != nil {
return err
}
for _, v := range regexp.MustCompile(`\[image:\s*([^\s\[\]]+)\s*\]`).FindAllStringSubmatch(result, -1) {
s.Reply(ImageUrl(v[1]))
result = strings.Replace(result, fmt.Sprintf(`[image:%s]`, v[1]), "", -1)
}
if result == "" {
return nil
}
return result
}
logs.Warn("回复:%s添加成功", jr)
functions = append(functions, Function{
Handle: handler,
Rules: rules,
})
}
}
+1
View File
@@ -18,6 +18,7 @@ require (
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/pkg/errors v0.9.1
github.com/robertkrimen/otto v0.0.0-20210927222213-f9375a256948
github.com/robfig/cron/v3 v3.0.1
github.com/silenceper/wechat/v2 v2.0.9
github.com/sirupsen/logrus v1.8.1
+7
View File
@@ -36,6 +36,8 @@ github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx2
github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/coreos/etcd v3.3.25+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
@@ -212,6 +214,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robertkrimen/otto v0.0.0-20210927222213-f9375a256948 h1:JgBmCxzPLP5ldSfEsVYhMUlJ7gQsOeBIPZOOeUdTTU8=
github.com/robertkrimen/otto v0.0.0-20210927222213-f9375a256948/go.mod h1:/mK7FZ3mFYEn9zvNPhpngTyatyehSwte5bJZ4ehL5Xw=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@@ -398,6 +402,9 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0=
gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375/go.mod h1:lNEQeAhU009zbRxng+XOj5ITVgY24WcbNnQopyfKoYQ=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/tucnak/telebot.v2 v2.4.0 h1:nOeqOWnOAD3dzbKW+NRumd8zjj5vrWwSa0WRTxvgfag=