Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
500e241f8d | ||
|
|
6c2bf3fc9c | ||
|
|
91d2fc50e2 | ||
|
|
119c2a5359 | ||
|
|
06a64c5e5a | ||
|
|
60bfa0e8a0 | ||
|
|
d08937563a | ||
|
|
697f5560a4 | ||
|
|
86daff5763 | ||
|
|
a6e935e233 | ||
|
|
ead2e06c4c | ||
|
|
51078eba12 | ||
|
|
1611019673 | ||
|
|
11d79e4540 | ||
|
|
8f15b58825 |
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go-wechat/config"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
@@ -10,11 +11,9 @@ import (
|
|||||||
var MySQL *gorm.DB
|
var MySQL *gorm.DB
|
||||||
|
|
||||||
func InitMySQLClient() {
|
func InitMySQLClient() {
|
||||||
dsn := "wechat:wechat123@tcp(10.0.0.31:3307)/wechat?charset=utf8mb4&parseTime=True&loc=Local"
|
|
||||||
|
|
||||||
// 创建连接对象
|
// 创建连接对象
|
||||||
mysqlConfig := mysql.Config{
|
mysqlConfig := mysql.Config{
|
||||||
DSN: dsn,
|
DSN: config.Conf.MySQL.GetDSN(),
|
||||||
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式
|
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式
|
||||||
DontSupportRenameColumn: true, // 用 `change` 重命名列
|
DontSupportRenameColumn: true, // 用 `change` 重命名列
|
||||||
}
|
}
|
||||||
|
|||||||
38
config.yaml
38
config.yaml
@@ -1,13 +1,45 @@
|
|||||||
|
# 微信HOOK配置
|
||||||
|
wechat:
|
||||||
|
# 微信HOOK接口地址
|
||||||
|
host: 10.0.0.73:19088
|
||||||
|
# 是否在启动的时候自动设置hook服务的回调
|
||||||
|
autoSetCallback: false
|
||||||
|
# 回调IP,如果是Docker运行,本参数必填,如果Docker修改了映射,格式为 ip:port
|
||||||
|
callback: 10.0.0.51
|
||||||
|
|
||||||
|
# 数据库
|
||||||
|
mysql:
|
||||||
|
host: 10.0.0.31
|
||||||
|
port: 3307
|
||||||
|
user: wechat
|
||||||
|
password: wechat123
|
||||||
|
db: wechat
|
||||||
|
|
||||||
task:
|
task:
|
||||||
enable: true
|
enable: true
|
||||||
syncFriends:
|
syncFriends:
|
||||||
enable: true
|
enable: true
|
||||||
cron: '0 * * * *'
|
cron: '*/5 * * * *' # 五分钟一次
|
||||||
waterGroup:
|
waterGroup:
|
||||||
enable: true
|
enable: false
|
||||||
cron: '30 9 * * *'
|
cron: '30 9 * * *'
|
||||||
|
# 需要发送水群排行榜的群Id
|
||||||
groups:
|
groups:
|
||||||
- '18958257758@chatroom'
|
- '18958257758@chatroom'
|
||||||
- '49448748645@chatroom'
|
- '49448748645@chatroom'
|
||||||
|
# 不计入统计范围的用户Id
|
||||||
blacklist:
|
blacklist:
|
||||||
- 'wxid_778868788691exit2'
|
- 'wxid_7788687886912'
|
||||||
|
|
||||||
|
# AI回复
|
||||||
|
ai:
|
||||||
|
# 是否启用
|
||||||
|
enable: false
|
||||||
|
# 模型,不填默认gpt-3.5-turbo-0613
|
||||||
|
model: gpt-3.5-turbo-0613
|
||||||
|
# OpenAI Api key
|
||||||
|
apiKey: sk-xxxx
|
||||||
|
# 接口代理域名,不填默认ChatGPT官方地址
|
||||||
|
baseUrl: https://sxxx
|
||||||
|
# 人设
|
||||||
|
personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。
|
||||||
11
config/ai.go
Normal file
11
config/ai.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
// ai
|
||||||
|
// @description: AI配置
|
||||||
|
type ai struct {
|
||||||
|
Enable bool `json:"enable" yaml:"enable"` // 是否启用AI
|
||||||
|
Model string `json:"model" yaml:"model"` // 模型
|
||||||
|
ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key
|
||||||
|
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
|
||||||
|
Personality string `json:"personality" yaml:"personality"` // 人设
|
||||||
|
}
|
||||||
@@ -5,7 +5,10 @@ var Conf Config
|
|||||||
// Config
|
// Config
|
||||||
// @description: 配置
|
// @description: 配置
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Task task `json:"task" yaml:"task"` // 定时任务配置
|
Task task `json:"task" yaml:"task"` // 定时任务配置
|
||||||
|
MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置
|
||||||
|
Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手
|
||||||
|
Ai ai `json:"ai" yaml:"ai"` // AI配置
|
||||||
}
|
}
|
||||||
|
|
||||||
// task
|
// task
|
||||||
|
|||||||
24
config/mysql.go
Normal file
24
config/mysql.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mysql
|
||||||
|
// @description: MySQL配置
|
||||||
|
type mysql struct {
|
||||||
|
Host string `mapstructure:"host" yaml:"host"` // 主机
|
||||||
|
Port int `mapstructure:"port" yaml:"port"` // 端口
|
||||||
|
User string `mapstructure:"user" yaml:"user"` // 用户名
|
||||||
|
Password string `mapstructure:"password" yaml:"password"` // 密码
|
||||||
|
Db string `mapstructure:"db" yaml:"db"` // 数据库名称
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDSN
|
||||||
|
// @description: 返回 MySQL 连接字符串
|
||||||
|
// @receiver c
|
||||||
|
// @return string
|
||||||
|
func (c mysql) GetDSN() string {
|
||||||
|
return fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
|
c.User, c.Password, c.Host, c.Port, c.Db)
|
||||||
|
}
|
||||||
33
config/wechat.go
Normal file
33
config/wechat.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// wxHelper
|
||||||
|
// @description: 微信助手
|
||||||
|
type wechat struct {
|
||||||
|
Host string `json:"host" yaml:"host"` // 接口地址
|
||||||
|
AutoSetCallback bool `json:"autoSetCallback" yaml:"autoSetCallback"` // 是否自动设置回调地址
|
||||||
|
Callback string `json:"callback" yaml:"callback"` // 回调地址
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check
|
||||||
|
// @description: 检查配置是否可用
|
||||||
|
// @receiver w
|
||||||
|
// @return bool
|
||||||
|
func (w wechat) Check() bool {
|
||||||
|
if w.Host == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if w.AutoSetCallback && w.Callback == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w wechat) GetURL(uri string) string {
|
||||||
|
host := w.Host
|
||||||
|
if !strings.HasPrefix(w.Host, "http://") {
|
||||||
|
host = "http://" + w.Host
|
||||||
|
}
|
||||||
|
return host + uri
|
||||||
|
}
|
||||||
48
docker-compose.yaml
Normal file
48
docker-compose.yaml
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
services:
|
||||||
|
wechat:
|
||||||
|
image: lxh01/wxhelper-docker:3.9.5.81
|
||||||
|
container_name: gw-wechat
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
- WINEDEBUG=fixme-all
|
||||||
|
volumes:
|
||||||
|
- ./data/wechat:/home/app/.wine/drive_c/users/app/Documents/WeChat\ Files
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
- "19088:19088"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:19088/api/checkLogin"]
|
||||||
|
interval: 60s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
|
||||||
|
mysql:
|
||||||
|
image: mysql:8
|
||||||
|
container_name: gw-db
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
wechat:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
- MYSQL_ROOT_PASSWORD=wechat
|
||||||
|
- MYSQL_USER=wechat
|
||||||
|
- MYSQL_PASSWORD=wechat
|
||||||
|
- MYSQL_DATABASE=wechat
|
||||||
|
volumes:
|
||||||
|
- ./data/db:/var/lib/mysql
|
||||||
|
|
||||||
|
|
||||||
|
wxhelper:
|
||||||
|
image: gitee.ltd/lxh/go-wxhelper:latest
|
||||||
|
container_name: gw-service
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
- mysql
|
||||||
|
volumes:
|
||||||
|
# 配置文件请参阅项目根目录的config.yaml文件
|
||||||
|
- ./config/config.yaml:/app/config.yaml
|
||||||
|
ports:
|
||||||
|
- "19099:19099"
|
||||||
@@ -19,13 +19,13 @@ func (Friend) TableName() string {
|
|||||||
// GroupUser
|
// GroupUser
|
||||||
// @description: 群成员
|
// @description: 群成员
|
||||||
type GroupUser struct {
|
type GroupUser struct {
|
||||||
GroupId string `json:"groupId"` // 群Id
|
GroupId string `json:"groupId"` // 群Id
|
||||||
Account string `json:"account"` // 账号
|
Account string `json:"account"` // 账号
|
||||||
HeadImage string `json:"headImage"` // 头像
|
HeadImage string `json:"headImage"` // 头像
|
||||||
Nickname string `json:"nickname"` // 昵称
|
Nickname string `json:"nickname"` // 昵称
|
||||||
Wxid string `json:"wxid"` // 微信Id
|
Wxid string `json:"wxid"` // 微信Id
|
||||||
IsMember bool `json:"isMember" gorm:"type:tinyint(1)"` // 是否群成员
|
IsMember bool `json:"isMember" gorm:"type:tinyint(1)"` // 是否群成员
|
||||||
LeaveTime time.Time `json:"leaveTime"` // 离开时间
|
LeaveTime *time.Time `json:"leaveTime"` // 离开时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func (GroupUser) TableName() string {
|
func (GroupUser) TableName() string {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -3,9 +3,11 @@ module go-wechat
|
|||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/duke-git/lancet/v2 v2.2.7
|
||||||
github.com/fsnotify/fsnotify v1.6.0
|
github.com/fsnotify/fsnotify v1.6.0
|
||||||
github.com/go-co-op/gocron v1.34.1
|
github.com/go-co-op/gocron v1.34.1
|
||||||
github.com/go-resty/resty/v2 v2.8.0
|
github.com/go-resty/resty/v2 v2.8.0
|
||||||
|
github.com/sashabaranov/go-openai v1.17.5
|
||||||
github.com/spf13/viper v1.17.0
|
github.com/spf13/viper v1.17.0
|
||||||
gorm.io/driver/mysql v1.5.1
|
gorm.io/driver/mysql v1.5.1
|
||||||
gorm.io/gorm v1.25.4
|
gorm.io/gorm v1.25.4
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -51,6 +51,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/duke-git/lancet/v2 v2.2.7 h1:u9zr6HR+MDUvZEtTlAFtSTIgZfEFsN7cKi27n5weZsw=
|
||||||
|
github.com/duke-git/lancet/v2 v2.2.7/go.mod h1:zGa2R4xswg6EG9I6WnyubDbFO/+A/RROxIbXcwryTsc=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||||
@@ -175,6 +177,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c
|
|||||||
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
|
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
|
||||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||||
|
github.com/sashabaranov/go-openai v1.17.5 h1:ItBzlrrfTtkFWOFlgfOhk3y/xRBC4PJol4gdbiK7hgg=
|
||||||
|
github.com/sashabaranov/go-openai v1.17.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||||
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
|
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
|
||||||
|
|||||||
74
handler/at_message.go
Normal file
74
handler/at_message.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
"go-wechat/config"
|
||||||
|
"go-wechat/entity"
|
||||||
|
"go-wechat/utils"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// handleAtMessage
|
||||||
|
// @description: 处理At机器人的消息
|
||||||
|
// @param m
|
||||||
|
func handleAtMessage(m entity.Message) {
|
||||||
|
if !config.Conf.Ai.Enable {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 预处理一下发送的消息,用正则去掉@机器人的内容
|
||||||
|
re := regexp.MustCompile(`@([^ ]+)`)
|
||||||
|
matches := re.FindStringSubmatch(m.Content)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
// 过滤掉第一个匹配到的
|
||||||
|
m.Content = strings.Replace(m.Content, matches[0], "", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组装消息体
|
||||||
|
messages := make([]openai.ChatCompletionMessage, 0)
|
||||||
|
if config.Conf.Ai.Personality != "" {
|
||||||
|
// 填充人设
|
||||||
|
messages = append(messages, openai.ChatCompletionMessage{
|
||||||
|
Role: openai.ChatMessageRoleSystem,
|
||||||
|
Content: config.Conf.Ai.Personality,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 填充用户消息
|
||||||
|
messages = append(messages, openai.ChatCompletionMessage{
|
||||||
|
Role: openai.ChatMessageRoleUser,
|
||||||
|
Content: m.Content,
|
||||||
|
})
|
||||||
|
|
||||||
|
// 配置模型
|
||||||
|
model := openai.GPT3Dot5Turbo0613
|
||||||
|
if config.Conf.Ai.Model != "" {
|
||||||
|
model = config.Conf.Ai.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认使用AI回复
|
||||||
|
conf := openai.DefaultConfig(config.Conf.Ai.ApiKey)
|
||||||
|
if config.Conf.Ai.BaseUrl != "" {
|
||||||
|
conf.BaseURL = fmt.Sprintf("%s/v1", config.Conf.Ai.BaseUrl)
|
||||||
|
}
|
||||||
|
client := openai.NewClientWithConfig(conf)
|
||||||
|
resp, err := client.CreateChatCompletion(
|
||||||
|
context.Background(),
|
||||||
|
openai.ChatCompletionRequest{
|
||||||
|
Model: model,
|
||||||
|
Messages: messages,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("OpenAI聊天发起失败: %v", err.Error())
|
||||||
|
utils.SendMessage(m.FromUser, m.GroupUser, "AI炸啦~", 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
utils.SendMessage(m.FromUser, m.GroupUser, "\n"+resp.Choices[0].Message.Content, 0)
|
||||||
|
}
|
||||||
@@ -26,8 +26,14 @@ func Parse(remoteAddr net.Addr, msg []byte) {
|
|||||||
groupUser := ""
|
groupUser := ""
|
||||||
msgStr := m.Content
|
msgStr := m.Content
|
||||||
if strings.Contains(m.FromUser, "@") {
|
if strings.Contains(m.FromUser, "@") {
|
||||||
// 系统消息不单独处理
|
switch m.Type {
|
||||||
if m.Type != types.MsgTypeRecalled && m.Type != types.MsgTypeSys {
|
case types.MsgTypeRecalled:
|
||||||
|
// 消息撤回
|
||||||
|
case types.MsgTypeSys:
|
||||||
|
// 系统消息
|
||||||
|
go handleSysMessage(m)
|
||||||
|
default:
|
||||||
|
// 默认消息处理
|
||||||
groupUser = strings.Split(m.Content, "\n")[0]
|
groupUser = strings.Split(m.Content, "\n")[0]
|
||||||
groupUser = strings.ReplaceAll(groupUser, ":", "")
|
groupUser = strings.ReplaceAll(groupUser, ":", "")
|
||||||
|
|
||||||
@@ -50,5 +56,10 @@ func Parse(remoteAddr net.Addr, msg []byte) {
|
|||||||
ent.DisplayFullContent = m.DisplayFullContent
|
ent.DisplayFullContent = m.DisplayFullContent
|
||||||
ent.Raw = string(msg)
|
ent.Raw = string(msg)
|
||||||
|
|
||||||
|
// 处理At机器人的消息
|
||||||
|
if strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你") {
|
||||||
|
go handleAtMessage(ent)
|
||||||
|
}
|
||||||
|
|
||||||
go service.SaveMessage(ent)
|
go service.SaveMessage(ent)
|
||||||
}
|
}
|
||||||
|
|||||||
19
handler/sys_message.go
Normal file
19
handler/sys_message.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go-wechat/model"
|
||||||
|
"go-wechat/utils"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// handleSysMessage
|
||||||
|
// @description: 系统消息处理
|
||||||
|
// @param m
|
||||||
|
func handleSysMessage(m model.Message) {
|
||||||
|
// 有人进群
|
||||||
|
if strings.Contains(m.Content, "\"邀请\"") && strings.Contains(m.Content, "\"加入了群聊") {
|
||||||
|
// 发一张图乐呵乐呵
|
||||||
|
// 自己欢迎自己图片地址 D:\Share\emoticon\welcome-yourself.gif
|
||||||
|
utils.SendImage(m.FromUser, "D:\\Share\\emoticon\\welcome-yourself.gif", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package config
|
package initialization
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"go-wechat/client"
|
||||||
|
"go-wechat/config"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,23 +23,29 @@ func InitConfig() {
|
|||||||
log.Panicf("读取配置文件失败: %v", err)
|
log.Panicf("读取配置文件失败: %v", err)
|
||||||
}
|
}
|
||||||
// 绑定配置文件
|
// 绑定配置文件
|
||||||
if err := vp.Unmarshal(&Conf); err != nil {
|
if err := vp.Unmarshal(&config.Conf); err != nil {
|
||||||
log.Panicf("配置文件解析失败: %v", err)
|
log.Panicf("配置文件解析失败: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("配置文件解析完成: %+v", Conf)
|
log.Printf("配置文件解析完成: %+v", config.Conf)
|
||||||
|
if !config.Conf.Wechat.Check() {
|
||||||
|
log.Panicf("微信HOOK配置缺失")
|
||||||
|
}
|
||||||
// 初始化数据库连接
|
// 初始化数据库连接
|
||||||
//db.Init()
|
client.InitMySQLClient()
|
||||||
//redis.Init()
|
//redis.Init()
|
||||||
|
|
||||||
// 下面的代码是配置变动之后自动刷新的
|
// 下面的代码是配置变动之后自动刷新的
|
||||||
vp.WatchConfig()
|
vp.WatchConfig()
|
||||||
vp.OnConfigChange(func(e fsnotify.Event) {
|
vp.OnConfigChange(func(e fsnotify.Event) {
|
||||||
// 绑定配置文件
|
// 绑定配置文件
|
||||||
if err := vp.Unmarshal(&Conf); err != nil {
|
if err := vp.Unmarshal(&config.Conf); err != nil {
|
||||||
log.Printf("配置文件更新失败: %v", err)
|
log.Printf("配置文件更新失败: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
if !config.Conf.Wechat.Check() {
|
||||||
|
log.Panicf("微信HOOK配置缺失")
|
||||||
|
}
|
||||||
// 初始化数据库连接
|
// 初始化数据库连接
|
||||||
//db.Init()
|
client.InitMySQLClient()
|
||||||
//redis.Init()
|
//redis.Init()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
14
main.go
14
main.go
@@ -2,18 +2,19 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"go-wechat/client"
|
|
||||||
"go-wechat/config"
|
"go-wechat/config"
|
||||||
"go-wechat/handler"
|
"go-wechat/handler"
|
||||||
|
"go-wechat/initialization"
|
||||||
"go-wechat/tasks"
|
"go-wechat/tasks"
|
||||||
|
"go-wechat/utils"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
config.InitConfig()
|
initialization.InitConfig()
|
||||||
client.InitMySQLClient()
|
|
||||||
tasks.InitTasks()
|
tasks.InitTasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +38,13 @@ func process(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// 如果启用了自动配置回调,就设置一下
|
||||||
|
if config.Conf.Wechat.AutoSetCallback {
|
||||||
|
utils.ClearCallback()
|
||||||
|
time.Sleep(500 * time.Millisecond) // 休眠五百毫秒再设置
|
||||||
|
utils.SetCallback(config.Conf.Wechat.Callback)
|
||||||
|
}
|
||||||
|
|
||||||
// 建立 tcp 服务
|
// 建立 tcp 服务
|
||||||
listen, err := net.Listen("tcp", "0.0.0.0:19099")
|
listen, err := net.Listen("tcp", "0.0.0.0:19099")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
106
readme.md
106
readme.md
@@ -1,6 +1,104 @@
|
|||||||
|
## 食用方式
|
||||||
|
0. 新建一个文件夹
|
||||||
### 手动打包镜像
|
|
||||||
```shell
|
```shell
|
||||||
docker build --push -t repo.lxh.io/lxh/go-wxhelper:latest .
|
mkdir wechat-hook # 名字随便写
|
||||||
|
cd wechat-hook
|
||||||
|
```
|
||||||
|
1. 创建配置文件`config.yaml`
|
||||||
|
```shell
|
||||||
|
mkdir config # 先创建一个文件夹保存配置文件,文件名不要变
|
||||||
|
vim config.yaml # 编辑配置文件,内容如下
|
||||||
|
```
|
||||||
|
```yaml
|
||||||
|
# 微信HOOK配置
|
||||||
|
wechat:
|
||||||
|
# 微信HOOK接口地址
|
||||||
|
host: wechat:19088
|
||||||
|
# 是否在启动的时候自动设置hook服务的回调
|
||||||
|
autoSetCallback: true
|
||||||
|
# 回调IP,如果是Docker运行,本参数必填,如果Docker修改了映射,格式为 ip:port,如果使用项目提供的docker-compsoe.yaml文件启动,可以不写
|
||||||
|
callback:
|
||||||
|
|
||||||
|
# 数据库
|
||||||
|
mysql:
|
||||||
|
host: mysql
|
||||||
|
port: 3306
|
||||||
|
user: wechat
|
||||||
|
password: wechat
|
||||||
|
db: wechat
|
||||||
|
|
||||||
|
task:
|
||||||
|
enable: false
|
||||||
|
syncFriends:
|
||||||
|
enable: true
|
||||||
|
cron: '0 * * * *'
|
||||||
|
waterGroup:
|
||||||
|
enable: true
|
||||||
|
cron: '30 9 * * *'
|
||||||
|
# 需要发送水群排行榜的群Id
|
||||||
|
groups:
|
||||||
|
- '11111@chatroom' # 自行配置
|
||||||
|
# 不计入统计范围的用户Id
|
||||||
|
blacklist:
|
||||||
|
- 'wxid_xxxx' # 自行配置
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 创建`docker-compose.yaml`文件
|
||||||
|
```yaml
|
||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
services:
|
||||||
|
wechat:
|
||||||
|
image: lxh01/wxhelper-docker:3.9.5.81
|
||||||
|
container_name: gw-wechat
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
- WINEDEBUG=fixme-all
|
||||||
|
volumes:
|
||||||
|
- ./data/wechat:/home/app/.wine/drive_c/users/app/Documents/WeChat\ Files
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
- "19088:19088"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:19088/api/checkLogin"]
|
||||||
|
interval: 60s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
|
||||||
|
mysql:
|
||||||
|
image: mysql:8
|
||||||
|
container_name: gw-db
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
wechat:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
- MYSQL_ROOT_PASSWORD=wechat
|
||||||
|
- MYSQL_USER=wechat
|
||||||
|
- MYSQL_PASSWORD=wechat
|
||||||
|
- MYSQL_DATABASE=wechat
|
||||||
|
volumes:
|
||||||
|
- ./data/db:/var/lib/mysql
|
||||||
|
|
||||||
|
|
||||||
|
wxhelper:
|
||||||
|
image: gitee.ltd/lxh/go-wxhelper:latest
|
||||||
|
container_name: gw-service
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
- mysql
|
||||||
|
volumes:
|
||||||
|
# 配置文件请参阅项目根目录的config.yaml文件
|
||||||
|
- ./config/config.yaml:/app/config.yaml
|
||||||
|
ports:
|
||||||
|
- "19099:19099"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 启动
|
||||||
|
```shell
|
||||||
|
# 以下命令选个能用的就行
|
||||||
|
docker-compose up -d # 老版本
|
||||||
|
docker compose up -d # 新版本
|
||||||
```
|
```
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
"go-wechat/client"
|
"go-wechat/client"
|
||||||
|
"go-wechat/config"
|
||||||
"go-wechat/constant"
|
"go-wechat/constant"
|
||||||
"go-wechat/entity"
|
"go-wechat/entity"
|
||||||
"go-wechat/model"
|
"go-wechat/model"
|
||||||
@@ -27,7 +28,7 @@ func syncFriends() {
|
|||||||
resp, err := hc.R().
|
resp, err := hc.R().
|
||||||
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
SetResult(&base).
|
SetResult(&base).
|
||||||
Post("http://10.0.0.73:19088/api/getContactList")
|
Post(config.Conf.Wechat.GetURL("/api/getContactList"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("获取好友列表失败: %s", err.Error())
|
log.Printf("获取好友列表失败: %s", err.Error())
|
||||||
return
|
return
|
||||||
@@ -105,7 +106,7 @@ func syncGroupUsers(tx *gorm.DB, gid string) {
|
|||||||
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
SetBody(string(pbs)).
|
SetBody(string(pbs)).
|
||||||
SetResult(&baseResp).
|
SetResult(&baseResp).
|
||||||
Post("http://10.0.0.73:19088/api/getMemberFromChatRoom")
|
Post(config.Conf.Wechat.GetURL("/api/getMemberFromChatRoom"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("获取群成员信息失败: %s", err.Error())
|
log.Printf("获取群成员信息失败: %s", err.Error())
|
||||||
return
|
return
|
||||||
@@ -189,7 +190,7 @@ func getContactProfile(wxid string) (ent model.ContactProfile, err error) {
|
|||||||
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
SetBody(string(pbs)).
|
SetBody(string(pbs)).
|
||||||
SetResult(&baseResp).
|
SetResult(&baseResp).
|
||||||
Post("http://10.0.0.73:19088/api/getContactProfile")
|
Post(config.Conf.Wechat.GetURL("/api/getContactProfile"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("获取成员详情失败: %s", err.Error())
|
log.Printf("获取成员详情失败: %s", err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
62
utils/callback.go
Normal file
62
utils/callback.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
"go-wechat/config"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ClearCallback
|
||||||
|
// @description: 清理微信HOOK回调
|
||||||
|
func ClearCallback() {
|
||||||
|
res := resty.New()
|
||||||
|
resp, err := res.R().
|
||||||
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
|
Post(config.Conf.Wechat.GetURL("/api/unhookSyncMsg"))
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("清理微信HOOK回调失败: %s", err.Error())
|
||||||
|
}
|
||||||
|
log.Printf("清理微信HOOK回调结果: %s", resp.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCallback
|
||||||
|
// @description: 设置微信HOOK回调
|
||||||
|
// @param host
|
||||||
|
func SetCallback(userHost string) {
|
||||||
|
// 获取本机IP地址
|
||||||
|
host := net.ParseIP(netutil.GetInternalIp()).String()
|
||||||
|
port := 19099
|
||||||
|
if userHost != "" {
|
||||||
|
uh := strings.Split(strings.TrimSpace(userHost), ":")
|
||||||
|
host = uh[0]
|
||||||
|
if len(uh) == 2 {
|
||||||
|
port, _ = strconv.Atoi(uh[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组装参数
|
||||||
|
param := map[string]any{
|
||||||
|
"port": port, // socket端口
|
||||||
|
"ip": host, // socketIP
|
||||||
|
"url": "", // http接口地址
|
||||||
|
"timeout": 3000, // 超时毫秒数
|
||||||
|
"enableHttp": 0, // 是否使用http接口
|
||||||
|
}
|
||||||
|
pbs, _ := json.Marshal(param)
|
||||||
|
log.Printf("设置微信HOOK回调参数: %s", string(pbs))
|
||||||
|
|
||||||
|
res := resty.New()
|
||||||
|
resp, err := res.R().
|
||||||
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
|
SetBody(string(pbs)).
|
||||||
|
Post(config.Conf.Wechat.GetURL("/api/hookSyncMsg"))
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("设置微信HOOK回调失败: %s", err.Error())
|
||||||
|
}
|
||||||
|
log.Printf("设置微信HOOK回调结果: %s", resp.String())
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
|
"go-wechat/config"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -17,18 +18,30 @@ func SendMessage(toId, atId, msg string, retryCount int) {
|
|||||||
log.Printf("重试五次失败,停止发送")
|
log.Printf("重试五次失败,停止发送")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组装参数
|
// 组装参数
|
||||||
param := map[string]any{
|
param := map[string]any{
|
||||||
"wxid": toId, // 群或好友Id
|
"wxid": toId, // 群或好友Id
|
||||||
"msg": msg, // 消息
|
"msg": msg, // 消息
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 接口地址
|
||||||
|
apiUrl := config.Conf.Wechat.GetURL("/api/sendTextMsg")
|
||||||
|
if atId != "" {
|
||||||
|
apiUrl = config.Conf.Wechat.GetURL("/api/sendAtText")
|
||||||
|
param = map[string]any{
|
||||||
|
"chatRoomId": toId,
|
||||||
|
"wxids": atId,
|
||||||
|
"msg": msg, // 消息
|
||||||
|
}
|
||||||
|
}
|
||||||
pbs, _ := json.Marshal(param)
|
pbs, _ := json.Marshal(param)
|
||||||
|
|
||||||
res := resty.New()
|
res := resty.New()
|
||||||
resp, err := res.R().
|
resp, err := res.R().
|
||||||
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
SetBody(string(pbs)).
|
SetBody(string(pbs)).
|
||||||
Post("http://10.0.0.73:19088/api/sendTextMsg")
|
Post(apiUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("发送文本消息失败: %s", err.Error())
|
log.Printf("发送文本消息失败: %s", err.Error())
|
||||||
// 休眠五秒后重新发送
|
// 休眠五秒后重新发送
|
||||||
@@ -37,3 +50,35 @@ func SendMessage(toId, atId, msg string, retryCount int) {
|
|||||||
}
|
}
|
||||||
log.Printf("发送文本消息结果: %s", resp.String())
|
log.Printf("发送文本消息结果: %s", resp.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendImage
|
||||||
|
// @description: 发送图片
|
||||||
|
// @param toId string 群或者好友Id
|
||||||
|
// @param imgPath string 图片路径
|
||||||
|
// @param retryCount int 重试次数
|
||||||
|
func SendImage(toId, imgPath string, retryCount int) {
|
||||||
|
if retryCount > 5 {
|
||||||
|
log.Printf("重试五次失败,停止发送")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组装参数
|
||||||
|
param := map[string]any{
|
||||||
|
"wxid": toId, // 群或好友Id
|
||||||
|
"imagePath": imgPath, // 图片地址
|
||||||
|
}
|
||||||
|
pbs, _ := json.Marshal(param)
|
||||||
|
|
||||||
|
res := resty.New()
|
||||||
|
resp, err := res.R().
|
||||||
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
|
SetBody(string(pbs)).
|
||||||
|
Post(config.Conf.Wechat.GetURL("/api/sendImagesMsg"))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("发送图片消息失败: %s", err.Error())
|
||||||
|
// 休眠五秒后重新发送
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
SendImage(toId, imgPath, retryCount+1)
|
||||||
|
}
|
||||||
|
log.Printf("发送图片消息结果: %s", resp.String())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user