71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/eatmoreapple/openwechat"
|
||
"github.com/robfig/cron/v3"
|
||
"go-bot/bot/database"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
// 新建一个定时任务对象
|
||
// 根据cron表达式进行时间调度,cron可以精确到秒,大部分表达式格式也是从秒开始。
|
||
// crontab := cron.New() 默认从分开始进行时间调度
|
||
var crontab = cron.New(cron.WithSeconds()) //精确到秒
|
||
func SignInBegin(msg *openwechat.Message, self *openwechat.Self) {
|
||
groups, err := self.Groups()
|
||
name := groups.GetByNickName("叫啥好呢")
|
||
fmt.Printf(name.NickName, err)
|
||
|
||
//定义定时器调用的任务函数
|
||
task := func() {
|
||
self.SendTextToGroup(name, "同志们学习了!")
|
||
}
|
||
|
||
//定时任务
|
||
spec := "0 0 9 * * ?" //cron表达式,每天9点
|
||
// 添加定时任务,
|
||
crontab.AddFunc(spec, task)
|
||
// 启动定时器
|
||
crontab.Start()
|
||
|
||
self.SendTextToGroup(name, "打卡程序启动成功!")
|
||
// 定时任务是另起协程执行的,这里使用 select 简答阻塞.实际开发中需要
|
||
// 根据实际情况进行控制
|
||
//阻塞主线程停止
|
||
//select {
|
||
//查询语句,保持程序运行,在这里等同于for{}
|
||
//}
|
||
}
|
||
|
||
func SignInEnd(msg *openwechat.Message, self *openwechat.Self) {
|
||
|
||
groups, err := self.Groups()
|
||
name := groups.GetByNickName("叫啥好呢")
|
||
fmt.Printf(name.NickName, err)
|
||
crontab.Stop()
|
||
// 发送到微信
|
||
self.SendTextToGroup(name, "打卡程序结束运行成功!")
|
||
}
|
||
|
||
func SignIn(msg *openwechat.Message, self *openwechat.Self) {
|
||
// 获取消息发送者
|
||
sender, err := msg.SenderInGroup()
|
||
nickName := sender.NickName
|
||
|
||
//查询数据库是否有此人 或此人就获得积分
|
||
integration := database.SelectUserIntegrationByNickName(nickName)
|
||
fmt.Println(integration)
|
||
integration = database.UpDataUserIntegrationByNickName(nickName, integration)
|
||
|
||
// 获取群聊组
|
||
groups, err := self.Groups()
|
||
name := groups.GetByNickName("叫啥好呢")
|
||
fmt.Printf(name.NickName, err)
|
||
|
||
// 当前时间
|
||
timeStr := time.Now().Format("2006-01-02 15:04:05")
|
||
self.SendTextToGroup(name, nickName+":今日打卡成功!\n"+"积分:"+strconv.Itoa(integration)+"\n时间:"+timeStr)
|
||
}
|