有指针错误
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
9
.idea/go-bot-dongl.iml
generated
Normal file
9
.idea/go-bot-dongl.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/go-bot-dongl.iml" filepath="$PROJECT_DIR$/.idea/go-bot-dongl.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
164
config/config.go
164
config/config.go
@@ -1,164 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eatmoreapple/openwechat"
|
||||
)
|
||||
|
||||
|
||||
// RegisterHandler 注册消息处理函数, 根据自己的需求自定义
|
||||
// matchFunc返回true则表示处理对应的handlers
|
||||
func (m *MessageMatchDispatcher) RegisterHandler(matchFunc MatchFunc, handlers ...MessageContextHandler) {
|
||||
if matchFunc == nil {
|
||||
panic("MatchFunc can not be nil")
|
||||
}
|
||||
node := &matchNode{matchFunc: matchFunc, group: handlers}
|
||||
m.matchNodes = append(m.matchNodes, node)
|
||||
}
|
||||
|
||||
// OnText 注册处理消息类型为Text的处理函数
|
||||
func (m *MessageMatchDispatcher) OnText(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsText() }, handlers...)
|
||||
}
|
||||
|
||||
// OnImage 注册处理消息类型为Image的处理函数
|
||||
func (m *MessageMatchDispatcher) OnImage(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsPicture() }, handlers...)
|
||||
}
|
||||
|
||||
// OnEmoticon 注册处理消息类型为Emoticon的处理函数(表情包)
|
||||
func (m *MessageMatchDispatcher) OnEmoticon(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsEmoticon() }, handlers...)
|
||||
}
|
||||
|
||||
// OnVoice 注册处理消息类型为Voice的处理函数
|
||||
func (m *MessageMatchDispatcher) OnVoice(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsVoice() }, handlers...)
|
||||
}
|
||||
|
||||
// OnFriendAdd 注册处理消息类型为FriendAdd的处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriendAdd(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsFriendAdd() }, handlers...)
|
||||
}
|
||||
|
||||
// OnCard 注册处理消息类型为Card的处理函数
|
||||
func (m *MessageMatchDispatcher) OnCard(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsCard() }, handlers...)
|
||||
}
|
||||
|
||||
// OnMedia 注册处理消息类型为Media(多媒体消息,包括但不限于APP分享、文件分享)的处理函数
|
||||
func (m *MessageMatchDispatcher) OnMedia(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsMedia() }, handlers...)
|
||||
}
|
||||
|
||||
// OnFriendByNickName 注册根据好友昵称是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriendByNickName(nickName string, handlers ...MessageContextHandler) {
|
||||
matchFunc := func(message *Message) bool {
|
||||
if message.IsSendByFriend() {
|
||||
sender, err := message.Sender()
|
||||
return err == nil && sender.NickName == nickName
|
||||
}
|
||||
return false
|
||||
}
|
||||
m.RegisterHandler(matchFunc, handlers...)
|
||||
}
|
||||
|
||||
// OnFriend 注册发送者为好友的处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriend(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsSendByFriend() }, handlers...)
|
||||
}
|
||||
|
||||
// OnGroup 注册发送者为群组的处理函数
|
||||
func (m *MessageMatchDispatcher) OnGroup(handlers ...MessageContextHandler) {
|
||||
m.RegisterHandler(func(message *Message) bool { return message.IsSendByGroup() }, handlers...)
|
||||
}
|
||||
|
||||
// OnUser 注册根据消息发送者的行为是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnUser(f func(user *User) bool, handlers ...MessageContextHandler) {
|
||||
mf := func(message *Message) bool {
|
||||
sender, err := message.Sender()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return f(sender)
|
||||
}
|
||||
m.RegisterHandler(mf, handlers...)
|
||||
}
|
||||
|
||||
// OnFriendByRemarkName 注册根据好友备注是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnFriendByRemarkName(remarkName string, handlers ...MessageContextHandler) {
|
||||
f := func(user *User) bool {
|
||||
return user.IsFriend() && user.RemarkName == remarkName
|
||||
}
|
||||
m.OnUser(f, handlers...)
|
||||
}
|
||||
|
||||
// OnGroupByGroupName 注册根据群名是否匹配的消息处理函数
|
||||
func (m *MessageMatchDispatcher) OnGroupByGroupName(groupName string, handlers ...MessageContextHandler) {
|
||||
f := func(user *User) bool {
|
||||
return user.IsGroup() && user.NickName == groupName
|
||||
}
|
||||
m.OnUser(f, handlers...)
|
||||
}
|
||||
|
||||
// SetAsync 设置是否异步处理
|
||||
func (m *MessageMatchDispatcher) SetAsync(async bool) {
|
||||
m.async = async
|
||||
}
|
||||
|
||||
|
||||
// Dispatch impl MessageDispatcher
|
||||
// 遍历 MessageMatchDispatcher 所有的消息处理函数
|
||||
// 获取所有匹配上的函数
|
||||
// 执行处理的消息处理方法
|
||||
func (m *MessageMatchDispatcher) Dispatch(msg *Message) {
|
||||
var group MessageContextHandlerGroup
|
||||
for _, node := range m.matchNodes {
|
||||
if node.matchFunc(msg) {
|
||||
group = append(group, node.group...)
|
||||
}
|
||||
}
|
||||
ctx := &MessageContext{Message: msg, messageHandlers: group}
|
||||
if m.async {
|
||||
go m.do(ctx)
|
||||
} else {
|
||||
m.do(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// SenderFriendRequired 只匹配好友
|
||||
func SenderFriendRequired() MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return user.IsFriend() })
|
||||
}
|
||||
|
||||
// SenderGroupRequired 只匹配群组
|
||||
func SenderGroupRequired() MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return user.IsGroup() })
|
||||
}
|
||||
|
||||
// SenderMpRequired 只匹配公众号
|
||||
func SenderMpRequired() MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return user.IsMP() })
|
||||
}
|
||||
|
||||
// SenderNickNameEqualMatchFunc 根据用户昵称是否等于指定字符串的匹配函数
|
||||
func SenderNickNameEqualMatchFunc(nickname string) MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return user.NickName == nickname })
|
||||
}
|
||||
|
||||
// SenderRemarkNameEqualMatchFunc 根据用户备注是否等于指定字符串的匹配函数
|
||||
func SenderRemarkNameEqualMatchFunc(remarkName string) MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return user.RemarkName == remarkName })
|
||||
}
|
||||
|
||||
// SenderNickNameContainsMatchFunc 根据用户昵称是否包含指定字符串的匹配函数
|
||||
func SenderNickNameContainsMatchFunc(nickname string) MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return strings.Contains(user.NickName, nickname) })
|
||||
}
|
||||
|
||||
// SenderRemakeNameContainsFunc 根据用户备注名是否包含指定字符串的匹配函数
|
||||
func SenderRemakeNameContainsFunc(remakeName string) MatchFunc {
|
||||
return SenderMatchFunc(func(user *User) bool { return strings.Contains(user.RemarkName, remakeName) })
|
||||
}
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/eatmoreapple/openwechat"
|
||||
"fmt"
|
||||
"github.com/robfig/cron"
|
||||
"time"
|
||||
"github.com/eatmoreapple/openwechat"
|
||||
"go-bot/bot/tool"
|
||||
)
|
||||
|
||||
//获取
|
||||
self, err := getCurrentUser()
|
||||
var self = tool.GetCurrentUser()
|
||||
|
||||
func SignInBegin(msg *openwechat.Message) {
|
||||
groups, err := tool.GetGroups()
|
||||
name := groups.GetByNickName("叫啥好呢")
|
||||
fmt.Printf(name.NickName, err)
|
||||
|
||||
// 发送到微信
|
||||
self.SendTextToGroup("叫啥好呢", "打卡程序启动成功!" + err)
|
||||
self.SendTextToGroup(name, "打卡程序启动成功!")
|
||||
}
|
||||
|
||||
func SignInEnd(msg *openwechat.Message){
|
||||
func SignInEnd(msg *openwechat.Message) {
|
||||
groups, err := tool.GetGroups()
|
||||
name := groups.GetByNickName("叫啥好呢")
|
||||
fmt.Printf(name.NickName, err)
|
||||
|
||||
// 发送到微信
|
||||
self.SendTextToGroup("叫啥好呢", "打卡程序结束运行成功!" + err)
|
||||
}
|
||||
self.SendTextToGroup(name, "打卡程序结束运行成功!")
|
||||
}
|
||||
|
||||
37
main.go
37
main.go
@@ -2,23 +2,17 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go-bot/bot/controller"
|
||||
|
||||
"github.com/eatmoreapple/openwechat"
|
||||
"go-bot/bot/controller"
|
||||
"go-bot/bot/tool"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
|
||||
// //创建热存储容器对象
|
||||
// reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json")
|
||||
// //执行热登录
|
||||
// bot.HotLogin(reloadStorage)
|
||||
// os.File("storage.json")
|
||||
// 创建bot 对象
|
||||
bot := openwechat.DefaultBot(openwechat.Desktop)
|
||||
// 创建热存储容器对象
|
||||
reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json")
|
||||
|
||||
// 注册消息处理函数
|
||||
bot.MessageHandler = func(msg *openwechat.Message) {
|
||||
@@ -35,15 +29,20 @@ func main() {
|
||||
controller.SignInEnd(msg)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 注册登陆二维码回调
|
||||
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
|
||||
|
||||
// 登陆
|
||||
if err := bot.Login(); err != nil {
|
||||
fmt.Println(err)
|
||||
//if err := bot.Login(); err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
//}
|
||||
|
||||
// 执行热登录
|
||||
err := bot.HotLogin(reloadStorage)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -51,12 +50,9 @@ func main() {
|
||||
self, err := bot.GetCurrentUser()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
tool.SetCurrentUser(*self)
|
||||
return
|
||||
}
|
||||
func getCurrentUser(){
|
||||
return self
|
||||
}
|
||||
|
||||
|
||||
// 获取所有的好友
|
||||
friends, err := self.Friends()
|
||||
@@ -67,5 +63,8 @@ func main() {
|
||||
fmt.Println(groups, err)
|
||||
|
||||
// 阻塞主goroutine, 直到发生异常或者用户主动退出
|
||||
bot.Block()
|
||||
err = bot.Block()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
1
storage.json
Normal file
1
storage.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Cookies":{"https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login":[],"https://login.wx.qq.com/jslogin":[],"https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck":[],"https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit":[],"https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage":[{"Name":"wxuin","Value":"2194844303","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-19T15:58:03Z","RawExpires":"Tue, 19-Jul-2022 15:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxuin=2194844303; Domain=wx2.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:58:03 GMT; Secure","Unparsed":null},{"Name":"wxsid","Value":"3ja7eY9R5gZKROxB","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-19T15:58:03Z","RawExpires":"Tue, 19-Jul-2022 15:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxsid=3ja7eY9R5gZKROxB; Domain=wx2.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:58:03 GMT; Secure","Unparsed":null},{"Name":"wxloadtime","Value":"1658203083","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-19T15:58:03Z","RawExpires":"Tue, 19-Jul-2022 15:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxloadtime=1658203083; Domain=wx2.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:58:03 GMT; Secure","Unparsed":null},{"Name":"mm_lang","Value":"zh_CN","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-19T15:58:03Z","RawExpires":"Tue, 19-Jul-2022 15:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"mm_lang=zh_CN; Domain=wx2.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:58:03 GMT; Secure","Unparsed":null},{"Name":"wxuin","Value":"2194844303","Path":"/","Domain":".qq.com","Expires":"2022-07-19T15:58:03Z","RawExpires":"Tue, 19-Jul-2022 15:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxuin=2194844303; Domain=.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:58:03 GMT; Secure","Unparsed":null},{"Name":"webwx_data_ticket","Value":"gSd7va7i40Env/bo1zQ5tKeD","Path":"/","Domain":".qq.com","Expires":"2022-07-19T15:58:03Z","RawExpires":"Tue, 19-Jul-2022 15:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"webwx_data_ticket=gSd7va7i40Env/bo1zQ5tKeD; Domain=.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:58:03 GMT; Secure","Unparsed":null},{"Name":"webwx_auth_ticket","Value":"CIsBEMzU3/UNGoABiuzA9TE2LDNQHw+tveTc40qRTDPP9vKnwE7U4MTo//0WtCZi1vXisrxkntmtOLK/YhqnpS9UJDZP2THQ3JA5yKVr2gKJVB0adjF9ZdGcVibHNHIXZQEMs8mNJXAZJV4KaShujAq2jMTYnhl3OWzsTawVyVOMy1hr6DDuVt4bf1A=","Path":"/","Domain":"wx2.qq.com","Expires":"2032-07-16T03:58:03Z","RawExpires":"Fri, 16-Jul-2032 03:58:03 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"webwx_auth_ticket=CIsBEMzU3/UNGoABiuzA9TE2LDNQHw+tveTc40qRTDPP9vKnwE7U4MTo//0WtCZi1vXisrxkntmtOLK/YhqnpS9UJDZP2THQ3JA5yKVr2gKJVB0adjF9ZdGcVibHNHIXZQEMs8mNJXAZJV4KaShujAq2jMTYnhl3OWzsTawVyVOMy1hr6DDuVt4bf1A=; Domain=wx2.qq.com; Path=/; Expires=Fri, 16-Jul-2032 03:58:03 GMT; Secure","Unparsed":null}],"https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify":[],"https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync":[{"Name":"wxpluginkey","Value":"1658191861","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-19T15:57:29Z","RawExpires":"Tue, 19-Jul-2022 15:57:29 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxpluginkey=1658191861; Domain=wx2.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:57:29 GMT; Secure","Unparsed":null},{"Name":"wxuin","Value":"2194844303","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-22T03:57:29Z","RawExpires":"Fri, 22-Jul-2022 03:57:29 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxuin=2194844303; Domain=wx2.qq.com; Path=/; Expires=Fri, 22-Jul-2022 03:57:29 GMT; Secure","Unparsed":null},{"Name":"wxsid","Value":"PXBUQpeS/rNCHphb","Path":"/","Domain":"wx2.qq.com","Expires":"2022-07-19T15:57:29Z","RawExpires":"Tue, 19-Jul-2022 15:57:29 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"wxsid=PXBUQpeS/rNCHphb; Domain=wx2.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:57:29 GMT; Secure","Unparsed":null},{"Name":"webwx_data_ticket","Value":"gScVE0QIARq8/TLoQa8zSdk+","Path":"/","Domain":".qq.com","Expires":"2022-07-19T15:57:29Z","RawExpires":"Tue, 19-Jul-2022 15:57:29 GMT","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"webwx_data_ticket=gScVE0QIARq8/TLoQa8zSdk+; Domain=.qq.com; Path=/; Expires=Tue, 19-Jul-2022 15:57:29 GMT; Secure","Unparsed":null}]},"BaseRequest":{"Uin":2194844303,"Sid":"3ja7eY9R5gZKROxB","Skey":"@crypt_f512dfa9_3080a1d9cb4d4e22faf871ad4076cedf","DeviceID":"e434466226300474"},"LoginInfo":{"Ret":0,"WxUin":2194844303,"IsGrayScale":1,"Message":"","SKey":"@crypt_f512dfa9_3080a1d9cb4d4e22faf871ad4076cedf","WxSid":"3ja7eY9R5gZKROxB","PassTicket":"cRef7K39Z95crml48C6x2ISoyYVxFxCEVBR9AP9Dn5wdiMLZYyP9x3qdDjjTn5UI"},"WechatDomain":"wx2.qq.com","UUID":"4fCYctjW2A=="}
|
||||
25
tool/tool.go
Normal file
25
tool/tool.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package tool
|
||||
|
||||
import "github.com/eatmoreapple/openwechat"
|
||||
|
||||
var self openwechat.Self
|
||||
|
||||
// SetCurrentUser 设置 当前登录用户
|
||||
func SetCurrentUser(user openwechat.Self) {
|
||||
self = user
|
||||
}
|
||||
|
||||
// GetCurrentUser 获取当前登录用户
|
||||
func GetCurrentUser() openwechat.Self {
|
||||
return self
|
||||
}
|
||||
|
||||
// GetFriends 获取所有的好友
|
||||
func GetFriends() (openwechat.Friends, error) {
|
||||
return self.Friends()
|
||||
}
|
||||
|
||||
// GetGroups 获取所有的群组
|
||||
func GetGroups() (openwechat.Groups, error) {
|
||||
return self.Groups()
|
||||
}
|
||||
Reference in New Issue
Block a user