Skip to content

Commit

Permalink
feat: 增加每隔监控数据->每隔5分钟获取好友列表、群组列表、订阅公众号列表(😜防止新手进入没启动框架拉不到数据,也为了更动态的获取数据)
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Feb 7, 2023
1 parent 88baeea commit 17d2187
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
14 changes: 14 additions & 0 deletions engine/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Init(c *Config) *Robot {
if c.MaxProcessTime == 0 {
c.MaxProcessTime = time.Minute * 3
}
go monitoringWechatData()

return &Robot{
BotConfig: c,
Expand Down Expand Up @@ -258,6 +259,19 @@ func preProcessMessageEvent(e *Event) {
}
}

// monitoringWechatData 监控微信数据
func monitoringWechatData() {
ticker := time.NewTicker(5 * time.Minute)
for range ticker.C {
friendsList, _ := WxBot.Framework.GetFriendsList(true)
groupList, _ := WxBot.Framework.GetGroupList(true)
subscriptionList, _ := WxBot.Framework.GetSubscriptionList(true)
WxBot.FriendsList = friendsList
WxBot.GroupList = groupList
WxBot.SubscriptionList = subscriptionList
}
}

// GetCTX 获取当前系统中的CTX
func GetCTX() *Ctx {
t := time.NewTimer(3 * time.Minute)
Expand Down
49 changes: 25 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

var conf robot.Config
var ping = true

func main() {
// 初始化配置
Expand All @@ -48,7 +49,7 @@ func main() {
v.GetUint("frameworks.servePort"),
))
if ipPort, err := net.CheckoutIpPort(v.GetString("frameworks.apiUrl")); err == nil {
if ping := net.PingConn(ipPort, time.Second*20); !ping {
if ping = net.PingConn(ipPort, time.Second*20); !ping {
log.Warn("[main] 无法连接到千寻框架,网络无法Ping通")
}
}
Expand All @@ -60,37 +61,37 @@ func main() {
v.GetUint("frameworks.servePort"),
))
if ipPort, err := net.CheckoutIpPort(v.GetString("frameworks.apiUrl")); err == nil {
if ping := net.PingConn(ipPort, time.Second*20); !ping {
if ping = net.PingConn(ipPort, time.Second*20); !ping {
log.Warn("[main] 无法连接到VLW框架,网络无法Ping通")
}
}
default:
log.Fatalf("[main] 请在配置文件中指定机器人框架后再启动")
}

bot := robot.Init(&conf)

log.Println("[main] 开始获取账号数据...")
friendsList, err := bot.Framework.GetFriendsList(true)
if err != nil {
log.Errorf("[main] 获取好友列表失败,error: %s", err.Error())
}
groupList, err := bot.Framework.GetGroupList(true)
if err != nil {
log.Errorf("[main] 获取群组列表失败,error: %s", err.Error())
}
subscriptionList, err := bot.Framework.GetSubscriptionList(true)
if err != nil {
log.Errorf("[main] 获取公众号列表失败,error: %s", err.Error())
robot.WxBot = robot.Init(&conf)
if ping {
log.Println("[main] 开始获取账号数据...")
friendsList, err := robot.WxBot.Framework.GetFriendsList(true)
if err != nil {
log.Errorf("[main] 获取好友列表失败,error: %s", err.Error())
}
groupList, err := robot.WxBot.Framework.GetGroupList(true)
if err != nil {
log.Errorf("[main] 获取群组列表失败,error: %s", err.Error())
}
subscriptionList, err := robot.WxBot.Framework.GetSubscriptionList(true)
if err != nil {
log.Errorf("[main] 获取公众号列表失败,error: %s", err.Error())
}
robot.WxBot.FriendsList = friendsList
robot.WxBot.GroupList = groupList
robot.WxBot.SubscriptionList = subscriptionList
log.Printf("[main] 共获取到%d个好友", len(friendsList))
log.Printf("[main] 共获取到%d个群组", len(groupList))
log.Printf("[main] 共获取到%d个公众号", len(subscriptionList))
}
bot.FriendsList = friendsList
bot.GroupList = groupList
bot.SubscriptionList = subscriptionList
robot.WxBot = bot

log.Printf("[main] 共获取到%d个好友", len(friendsList))
log.Printf("[main] 共获取到%d个群组", len(groupList))
log.Printf("[main] 共获取到%d个公众号", len(subscriptionList))
log.Printf("[main] 机器人%s开始工作", conf.BotNickname)
bot.Run()
robot.WxBot.Run()
}

0 comments on commit 17d2187

Please sign in to comment.