210 lines
5.4 KiB
Go
210 lines
5.4 KiB
Go
package workers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"go-bot/config"
|
||
|
||
"github.com/parnurzeal/gorequest"
|
||
)
|
||
|
||
type Worker interface {
|
||
CheckPermission() string
|
||
GetMsg() string
|
||
SendMsg(msg string) bool
|
||
GetImage(file string) string
|
||
GetHisMsg(id string) string
|
||
}
|
||
type StdAns struct {
|
||
AllowGroup []interface{}
|
||
AllowUser []interface{}
|
||
AllowRole []interface{}
|
||
BlockGroup []interface{}
|
||
BlockUser []interface{}
|
||
Master []interface{}
|
||
GroupNotAllow string
|
||
UserNotAllow string
|
||
RoleNotAllow string
|
||
Parms []string
|
||
UID string
|
||
GID string
|
||
Role string
|
||
MID string
|
||
RawMsg string
|
||
}
|
||
|
||
// 定义一个map类型的cfg,用于存储配置信息
|
||
var cfg map[string]interface{}
|
||
|
||
// 定义一个WorkerFactory类型的函数,用于创建Worker
|
||
type WorkerFactory func(parms []string, uid, gid, role, mid, rawMsg string) Worker
|
||
|
||
// 定义一个workerFactories的map,用于存储WorkerFactory
|
||
var workerFactories = make(map[string]WorkerFactory)
|
||
|
||
// 定义一个RegisterWorkerFactory函数,用于注册WorkerFactory
|
||
func RegisterWorkerFactory(name string, factory WorkerFactory) {
|
||
workerFactories[name] = factory
|
||
fmt.Printf("Register WorkerFactory: %s\n", name)
|
||
}
|
||
|
||
// 定义一个NewWorker函数,用于创建Worker
|
||
func NewWorker(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||
fmt.Printf("NewWorker: %s, %s, %s, %s, %s\n", parms, uid, gid, role, mid)
|
||
if factory, ok := workerFactories[parms[0]]; ok {
|
||
|
||
fmt.Printf("Use WorkerFactory: %s\n", parms[0])
|
||
return factory(parms, uid, gid, role, mid, rawMsg)
|
||
}
|
||
return &Emm{
|
||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||
}
|
||
}
|
||
|
||
func NewStdAns(parms []string, uid, gid, role, mid, rawMsg string) *StdAns {
|
||
// var cfg map[string]interface{}
|
||
|
||
cfg = config.GetConfig()
|
||
// println("AllowGroup:", cfg["AllowGroup"].([]interface{}))
|
||
return &StdAns{
|
||
Parms: parms,
|
||
UID: uid,
|
||
GID: gid,
|
||
Role: role,
|
||
MID: mid,
|
||
RawMsg: rawMsg,
|
||
AllowGroup: cfg["AllowGroup"].([]interface{}),
|
||
AllowUser: cfg["AllowUser"].([]interface{}),
|
||
AllowRole: cfg["AllowRole"].([]interface{}),
|
||
BlockGroup: cfg["BlockGroup"].([]interface{}),
|
||
BlockUser: cfg["BlockUser"].([]interface{}),
|
||
Master: cfg["Master"].([]interface{}),
|
||
GroupNotAllow: "汝所在的群组不被允许这样命令咱呢.",
|
||
UserNotAllow: "汝不被允许呢.",
|
||
RoleNotAllow: "汝的角色不被允许哦.",
|
||
}
|
||
}
|
||
|
||
func (s *StdAns) CheckPermission() string {
|
||
if (len(s.AllowGroup) > 0 && !contains(s.AllowGroup, s.GID)) || contains(s.BlockGroup, s.GID) {
|
||
return s.GroupNotAllow
|
||
}
|
||
if (len(s.AllowUser) > 0 && !contains(s.AllowUser, s.UID)) || contains(s.BlockUser, s.UID) {
|
||
return s.UserNotAllow
|
||
}
|
||
if len(s.AllowRole) > 0 && !contains(s.AllowRole, s.Role) {
|
||
return s.RoleNotAllow
|
||
}
|
||
return "ok"
|
||
}
|
||
func contains(slice []interface{}, value string) bool {
|
||
for _, item := range slice {
|
||
if item == value {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func (s *StdAns) GetMsg() string {
|
||
// 暂时设置权限校验不通过什么都不提示
|
||
return "-1"
|
||
// if s.Parms[0] != "" {
|
||
// return "咱也不知道 " + s.Parms[0] + "是啥呢!"
|
||
// } else {
|
||
|
||
// return "汝再调戏咱,咱可就生气了!!"
|
||
// }
|
||
|
||
}
|
||
func (s *StdAns) GetHisMsg(id string) string {
|
||
if id == "" {
|
||
return ""
|
||
}
|
||
url := cfg["POSTURL"].(string) + "/get_msg?message_id=" + id
|
||
|
||
request := gorequest.New()
|
||
resp, body, errs := request.Post(url).End()
|
||
if len(errs) > 0 {
|
||
|
||
fmt.Println("Error sending request:", errs)
|
||
return ""
|
||
}
|
||
defer resp.Body.Close()
|
||
data := make(map[string]interface{})
|
||
err := json.Unmarshal([]byte(body), &data)
|
||
if err != nil {
|
||
fmt.Println("解析JSON失败:", err)
|
||
return ""
|
||
}
|
||
|
||
// fmt.Println("响应返回:", body)
|
||
return data["data"].(map[string]interface{})["message"].(string)
|
||
|
||
}
|
||
func (s *StdAns) SendMsg(msg string) bool {
|
||
if msg == "-1" {
|
||
return false
|
||
}
|
||
requestBody, err := json.Marshal(map[string]interface{}{
|
||
// "action": "send_msg",
|
||
// "params": map[string]interface{}{
|
||
"group_id": s.GID,
|
||
"message": msg,
|
||
// },
|
||
})
|
||
if err != nil {
|
||
fmt.Println("Error marshaling JSON:", err)
|
||
return false
|
||
}
|
||
_, err = json.MarshalIndent(requestBody, "", " ")
|
||
if err != nil {
|
||
fmt.Println("Error marshaling JSON:", err)
|
||
return false
|
||
}
|
||
// fmt.Println(string(re))
|
||
|
||
url := cfg["POSTURL"].(string) + "/send_msg"
|
||
// println("core:", url)
|
||
// fmt.Println("请求地址:", url)
|
||
fmt.Println("响应信息:\n", msg)
|
||
request := gorequest.New()
|
||
resp, body, errs := request.Post(url).
|
||
Set("Content-Type", "application/json").
|
||
Send(string(requestBody)).End()
|
||
if len(errs) > 0 {
|
||
|
||
fmt.Println("Error sending request:", err)
|
||
return false
|
||
}
|
||
defer resp.Body.Close()
|
||
// res, _ := io.ReadAll(resp.Body)
|
||
fmt.Println("响应返回:", body)
|
||
return true
|
||
}
|
||
|
||
func (s *StdAns) GetImage(file string) string {
|
||
if file == "" {
|
||
return ""
|
||
}
|
||
url := cfg["POSTURL"].(string) + "/get_image?file=" + file
|
||
|
||
request := gorequest.New()
|
||
resp, body, errs := request.Post(url).End()
|
||
if len(errs) > 0 {
|
||
|
||
fmt.Println("Error sending request:", errs)
|
||
return ""
|
||
}
|
||
defer resp.Body.Close()
|
||
data := make(map[string]interface{})
|
||
err := json.Unmarshal([]byte(body), &data)
|
||
if err != nil {
|
||
fmt.Println("解析JSON失败:", err)
|
||
return ""
|
||
}
|
||
path := data["data"].(map[string]interface{})["file"].(string)
|
||
|
||
// fmt.Println("响应返回:", body)
|
||
return path
|
||
}
|