update
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cdle/sillyGirl/im"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Yaml struct {
|
||||
Im []im.Config
|
||||
Replies []Reply
|
||||
}
|
||||
|
||||
var ExecPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
|
||||
var Config Yaml
|
||||
|
||||
func init() {
|
||||
content, err := ioutil.ReadFile(ExecPath + "/conf/config.yaml")
|
||||
if err != nil {
|
||||
logs.Warn("解析config.yaml读取错误: %v", err)
|
||||
}
|
||||
if yaml.Unmarshal(content, &Config) != nil {
|
||||
logs.Warn("解析config.yaml出错: %v", err)
|
||||
}
|
||||
InitReplies()
|
||||
initToHandleMessage()
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/cdle/sillyGirl/im"
|
||||
"github.com/cdle/sillyGirl/im/tg"
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
)
|
||||
|
||||
type Function struct {
|
||||
Rules []string
|
||||
FindAll bool
|
||||
Admin bool
|
||||
Handle func(s im.Sender) bool
|
||||
}
|
||||
|
||||
var functions = []Function{
|
||||
{
|
||||
Rules: []string{"^傻妞 (.*)$", "^傻妞$"},
|
||||
Handle: func(s im.Sender) bool {
|
||||
m := s.Get()
|
||||
if m != "" {
|
||||
s.Reply(fmt.Sprintf("哎呀,傻妞不懂%s是什么意思啦。", m))
|
||||
} else {
|
||||
s.Reply("请说,我在。")
|
||||
}
|
||||
return true
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var Senders chan im.Sender
|
||||
|
||||
func initToHandleMessage() {
|
||||
for _, im := range Config.Im {
|
||||
switch im.Type {
|
||||
case "tg":
|
||||
tg.Handler = func(message *tb.Message) {
|
||||
Senders <- &tg.Sender{
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
go tg.RunBot(&im)
|
||||
case "qq":
|
||||
|
||||
}
|
||||
}
|
||||
Senders = make(chan im.Sender)
|
||||
go func() {
|
||||
for {
|
||||
go handleMessage(<-Senders)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func AddCommand(cmd *Function) {
|
||||
functions = append(functions, *cmd)
|
||||
}
|
||||
|
||||
func handleMessage(sender im.Sender) {
|
||||
for _, function := range functions {
|
||||
for _, rule := range function.Rules {
|
||||
var matched bool
|
||||
if function.FindAll {
|
||||
if res := regexp.MustCompile(rule).FindAllStringSubmatch(sender.GetContent(), -1); len(res) > 0 {
|
||||
tmp := [][]string{}
|
||||
for i := range res {
|
||||
tmp = append(tmp, res[i][1:])
|
||||
}
|
||||
sender.SetAllMatch(tmp)
|
||||
matched = true
|
||||
}
|
||||
} else {
|
||||
if res := regexp.MustCompile(rule).FindStringSubmatch(sender.GetContent()); len(res) > 0 {
|
||||
sender.SetMatch(res[1:])
|
||||
matched = true
|
||||
}
|
||||
}
|
||||
if matched && function.Handle(sender) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/beego/beego/v2/adapter/httplib"
|
||||
"github.com/buger/jsonparser"
|
||||
"github.com/cdle/sillyGirl/im"
|
||||
)
|
||||
|
||||
type Reply struct {
|
||||
Rules []string
|
||||
Type string //text url
|
||||
Content string
|
||||
Request struct {
|
||||
Url string
|
||||
Method string
|
||||
Body string // form-data raw
|
||||
Headers []string
|
||||
ResponseType string `yaml:"response_type"` //text json image
|
||||
Get string
|
||||
}
|
||||
}
|
||||
|
||||
func InitReplies() {
|
||||
for _, v := range Config.Replies {
|
||||
reply := v
|
||||
var handler func(s im.Sender) bool
|
||||
if reply.Type != "url" {
|
||||
handler = func(s im.Sender) bool {
|
||||
s.Reply(reply.Content)
|
||||
return true
|
||||
}
|
||||
}
|
||||
handler = func(s im.Sender) bool {
|
||||
url := reply.Request.Url
|
||||
body := reply.Request.Body
|
||||
for k, v := range s.GetMatch() {
|
||||
url = strings.Replace(url, fmt.Sprintf(`{{%d}}`, k+1), v, -1)
|
||||
body = strings.Replace(body, fmt.Sprintf(`{{%d}}`, k+1), v, -1)
|
||||
}
|
||||
var req httplib.BeegoHTTPRequest
|
||||
if strings.ToLower(reply.Request.Method) == "post" {
|
||||
req = *httplib.Post(url)
|
||||
} else {
|
||||
req = *httplib.Get(url)
|
||||
}
|
||||
for _, header := range reply.Request.Headers {
|
||||
ss := strings.Split(header, ":")
|
||||
if len(ss) > 0 {
|
||||
req.Header(ss[0], strings.Join(ss[1:], ":"))
|
||||
}
|
||||
}
|
||||
if reply.Request.Body != "" {
|
||||
req.Body(body)
|
||||
}
|
||||
rsp, err := req.Response()
|
||||
if err != nil {
|
||||
if reply.Content != "" {
|
||||
s.Reply(reply.Content)
|
||||
} else {
|
||||
s.Reply(err)
|
||||
}
|
||||
return true
|
||||
}
|
||||
switch reply.Request.ResponseType {
|
||||
case "image":
|
||||
s.Reply(rsp)
|
||||
case "json":
|
||||
d, _ := ioutil.ReadAll(rsp.Body)
|
||||
f, err := jsonparser.GetString(d, strings.Split(reply.Request.Get, ".")...)
|
||||
if err != nil {
|
||||
s.Reply(err)
|
||||
return true
|
||||
}
|
||||
s.Reply(f)
|
||||
default:
|
||||
d, _ := ioutil.ReadAll(rsp.Body)
|
||||
fmt.Println(string(d))
|
||||
s.Reply(d)
|
||||
}
|
||||
return true
|
||||
}
|
||||
functions = append(functions, Function{
|
||||
Rules: reply.Rules,
|
||||
Handle: handler,
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user