2024-04-01 09:42:29 +08:00
|
|
|
|
package workers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"go-bot/config"
|
2024-10-25 23:15:20 +08:00
|
|
|
|
"slices"
|
2024-07-05 22:33:55 +08:00
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
"github.com/imroc/req/v3"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
type Worker interface {
|
|
|
|
|
CheckPermission() string
|
|
|
|
|
GetMsg() string
|
|
|
|
|
SendMsg(msg string) bool
|
2024-07-14 21:38:39 +08:00
|
|
|
|
GetImage(file string) string
|
2024-09-01 14:28:52 +08:00
|
|
|
|
GetHisMsg(id string) (string, string, string)
|
2024-07-05 22:33:55 +08:00
|
|
|
|
}
|
2024-04-01 09:42:29 +08:00
|
|
|
|
type StdAns struct {
|
2024-06-30 21:56:34 +08:00
|
|
|
|
AllowGroup []interface{}
|
|
|
|
|
AllowUser []interface{}
|
|
|
|
|
AllowRole []interface{}
|
|
|
|
|
BlockGroup []interface{}
|
|
|
|
|
BlockUser []interface{}
|
2024-07-02 13:21:29 +08:00
|
|
|
|
Master []interface{}
|
2024-04-01 09:42:29 +08:00
|
|
|
|
GroupNotAllow string
|
|
|
|
|
UserNotAllow string
|
|
|
|
|
RoleNotAllow string
|
|
|
|
|
Parms []string
|
|
|
|
|
UID string
|
|
|
|
|
GID string
|
|
|
|
|
Role string
|
|
|
|
|
MID string
|
|
|
|
|
RawMsg string
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 定义一个map类型的cfg,用于存储配置信息
|
2024-06-30 16:12:26 +08:00
|
|
|
|
var cfg map[string]interface{}
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 定义一个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),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 09:42:29 +08:00
|
|
|
|
func NewStdAns(parms []string, uid, gid, role, mid, rawMsg string) *StdAns {
|
2024-06-30 21:56:34 +08:00
|
|
|
|
// var cfg map[string]interface{}
|
2024-06-30 16:12:26 +08:00
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
cfg = config.GetConfig()
|
|
|
|
|
// println("AllowGroup:", cfg["AllowGroup"].([]interface{}))
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return &StdAns{
|
|
|
|
|
Parms: parms,
|
|
|
|
|
UID: uid,
|
|
|
|
|
GID: gid,
|
|
|
|
|
Role: role,
|
|
|
|
|
MID: mid,
|
|
|
|
|
RawMsg: rawMsg,
|
2024-06-30 21:56:34 +08:00
|
|
|
|
AllowGroup: cfg["AllowGroup"].([]interface{}),
|
|
|
|
|
AllowUser: cfg["AllowUser"].([]interface{}),
|
|
|
|
|
AllowRole: cfg["AllowRole"].([]interface{}),
|
|
|
|
|
BlockGroup: cfg["BlockGroup"].([]interface{}),
|
|
|
|
|
BlockUser: cfg["BlockUser"].([]interface{}),
|
2024-07-02 13:21:29 +08:00
|
|
|
|
Master: cfg["Master"].([]interface{}),
|
2024-04-01 09:42:29 +08:00
|
|
|
|
GroupNotAllow: "汝所在的群组不被允许这样命令咱呢.",
|
|
|
|
|
UserNotAllow: "汝不被允许呢.",
|
|
|
|
|
RoleNotAllow: "汝的角色不被允许哦.",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 23:15:20 +08:00
|
|
|
|
// 将 []interface{} 转换为 []string 的辅助函数
|
|
|
|
|
func toStringSlice(slice []interface{}) []string {
|
|
|
|
|
strSlice := make([]string, len(slice))
|
|
|
|
|
for i, v := range slice {
|
|
|
|
|
strSlice[i] = v.(string) // 假设切片中的元素都是 string
|
|
|
|
|
}
|
|
|
|
|
return strSlice
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 09:42:29 +08:00
|
|
|
|
func (s *StdAns) CheckPermission() string {
|
2024-10-25 23:15:20 +08:00
|
|
|
|
if (len(s.AllowGroup) > 0 && !slices.Contains(toStringSlice(s.AllowGroup), s.GID)) || slices.Contains(toStringSlice(s.BlockGroup), s.GID) {
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return s.GroupNotAllow
|
|
|
|
|
}
|
2024-10-25 23:15:20 +08:00
|
|
|
|
if (len(s.AllowUser) > 0 && !slices.Contains(toStringSlice(s.AllowUser), s.UID)) || slices.Contains(toStringSlice(s.BlockUser), s.UID) {
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return s.UserNotAllow
|
|
|
|
|
}
|
2024-10-25 23:15:20 +08:00
|
|
|
|
if len(s.AllowRole) > 0 && !slices.Contains(toStringSlice(s.AllowRole), s.Role) {
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return s.RoleNotAllow
|
|
|
|
|
}
|
2024-07-01 10:05:42 +08:00
|
|
|
|
return "ok"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
}
|
2024-10-25 23:15:20 +08:00
|
|
|
|
|
|
|
|
|
// func contains[T comparable](slice []T, value T) bool {
|
|
|
|
|
// for _, item := range slice {
|
|
|
|
|
// if item == value {
|
|
|
|
|
// return true
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return false
|
|
|
|
|
// }
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
|
|
func (s *StdAns) GetMsg() string {
|
2024-04-01 11:19:52 +08:00
|
|
|
|
// 暂时设置权限校验不通过什么都不提示
|
|
|
|
|
return "-1"
|
|
|
|
|
// if s.Parms[0] != "" {
|
|
|
|
|
// return "咱也不知道 " + s.Parms[0] + "是啥呢!"
|
|
|
|
|
// } else {
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
2024-04-01 11:19:52 +08:00
|
|
|
|
// return "汝再调戏咱,咱可就生气了!!"
|
|
|
|
|
// }
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
|
|
}
|
2024-09-01 14:28:52 +08:00
|
|
|
|
func (s *StdAns) GetHisMsg(id string) (string, string, string) {
|
2024-07-14 21:38:39 +08:00
|
|
|
|
if id == "" {
|
2024-09-01 14:28:52 +08:00
|
|
|
|
return "", "", ""
|
2024-07-14 21:38:39 +08:00
|
|
|
|
}
|
|
|
|
|
url := cfg["POSTURL"].(string) + "/get_msg?message_id=" + id
|
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
cli := req.C()
|
|
|
|
|
resp, err := cli.R().
|
|
|
|
|
SetSuccessResult(&data).
|
|
|
|
|
Get(url)
|
2024-07-14 21:38:39 +08:00
|
|
|
|
if err != nil {
|
2024-09-08 16:46:05 +08:00
|
|
|
|
fmt.Println("Error sending request:", err)
|
2024-09-01 14:28:52 +08:00
|
|
|
|
return "", "", ""
|
2024-07-14 21:38:39 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2024-09-01 14:28:52 +08:00
|
|
|
|
if data["status"] == "ok" {
|
2024-09-01 20:37:45 +08:00
|
|
|
|
|
2024-09-01 16:36:05 +08:00
|
|
|
|
message := data["data"].(map[string]interface{})["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})
|
|
|
|
|
return message["file"].(string), message["url"].(string), message["file_size"].(string)
|
2024-08-31 19:39:19 +08:00
|
|
|
|
} else {
|
2024-09-01 14:28:52 +08:00
|
|
|
|
// fmt.Println("响应返回:", body)
|
|
|
|
|
return "", "", ""
|
|
|
|
|
// println("raw_message:", raw_message)
|
|
|
|
|
|
2024-08-31 19:39:19 +08:00
|
|
|
|
}
|
2024-07-14 21:38:39 +08:00
|
|
|
|
|
|
|
|
|
}
|
2024-04-01 09:42:29 +08:00
|
|
|
|
func (s *StdAns) SendMsg(msg string) bool {
|
2024-04-01 11:19:52 +08:00
|
|
|
|
if msg == "-1" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-04-03 21:53:57 +08:00
|
|
|
|
|
2024-07-09 15:35:59 +08:00
|
|
|
|
url := cfg["POSTURL"].(string) + "/send_msg"
|
2024-04-03 21:53:57 +08:00
|
|
|
|
// println("core:", url)
|
2024-04-15 15:26:46 +08:00
|
|
|
|
// fmt.Println("请求地址:", url)
|
2024-08-31 21:21:06 +08:00
|
|
|
|
// fmt.Println("响应信息:\n", msg)
|
2024-09-08 16:46:05 +08:00
|
|
|
|
client := req.C()
|
|
|
|
|
|
|
|
|
|
resp, err := client.R().
|
|
|
|
|
SetHeader("Content-Type", "application/json").
|
|
|
|
|
SetBodyJsonMarshal(map[string]interface{}{
|
|
|
|
|
// "action": "send_msg",
|
|
|
|
|
// "params": map[string]interface{}{
|
|
|
|
|
"group_id": s.GID,
|
|
|
|
|
"message": msg,
|
|
|
|
|
// },
|
|
|
|
|
}).
|
|
|
|
|
Post(url)
|
|
|
|
|
if err != nil {
|
2024-04-01 09:42:29 +08:00
|
|
|
|
fmt.Println("Error sending request:", err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
2024-04-01 11:19:52 +08:00
|
|
|
|
// res, _ := io.ReadAll(resp.Body)
|
2024-08-31 21:21:06 +08:00
|
|
|
|
// fmt.Println("响应返回:", body)
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return true
|
|
|
|
|
}
|
2024-07-14 21:38:39 +08:00
|
|
|
|
|
|
|
|
|
func (s *StdAns) GetImage(file string) string {
|
|
|
|
|
if file == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
url := cfg["POSTURL"].(string) + "/get_image?file=" + file
|
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
client := req.C()
|
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
resp, err := client.R().
|
|
|
|
|
SetSuccessResult(&data).
|
|
|
|
|
Get(url)
|
|
|
|
|
if err != nil {
|
2024-07-14 21:38:39 +08:00
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
fmt.Println("Error sending request:", err)
|
2024-07-14 21:38:39 +08:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
2024-09-08 16:46:05 +08:00
|
|
|
|
|
2024-08-31 21:21:06 +08:00
|
|
|
|
// fmt.Println("url:", url)
|
2024-08-31 19:39:19 +08:00
|
|
|
|
path, ok := data["data"].(map[string]interface{})["file"].(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ""
|
|
|
|
|
} else {
|
|
|
|
|
// fmt.Println("响应返回:", body)
|
|
|
|
|
return path
|
|
|
|
|
}
|
2024-07-14 21:38:39 +08:00
|
|
|
|
|
|
|
|
|
}
|