diff --git a/controller/msg.go b/controller/msg.go new file mode 100644 index 0000000..a295f33 --- /dev/null +++ b/controller/msg.go @@ -0,0 +1,2 @@ +package controller + diff --git a/go.mod b/go.mod index 4021bee..44b71a4 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module go-bot/bot go 1.18 -require github.com/eatmoreapple/openwechat v1.1.11 // indirect +require github.com/eatmoreapple/openwechat v1.1.11 diff --git a/main.go b/main.go index 95a9061..243c475 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,9 @@ package main import ( "fmt" + + "go-bot/bot/controller" + "github.com/eatmoreapple/openwechat" ) @@ -11,12 +14,22 @@ func main() { // 注册消息处理函数 bot.MessageHandler = func(msg *openwechat.Message) { - if msg.IsText() && msg.Content == "专升本英语" { - msg.ReplyText("success") - + controller.SingleChoice(msg) } + + + if msg.IsText() && msg.Content == "打卡开启" { + module.SignInBegin(msg) + } + + if msg.IsText() && msg.Content == "打卡关闭" { + module.SignInEnd(msg) + } + + } + // 注册登陆二维码回调 bot.UUIDCallback = openwechat.PrintlnQrcodeUrl diff --git a/module/english.go b/module/english.go index 3e92819..4c46151 100644 --- a/module/english.go +++ b/module/english.go @@ -1,10 +1,10 @@ -package english +package module + import ( - "fmt" "github.com/eatmoreapple/openwechat" ) -func English(string ){ - +func SingleChoice(msg *openwechat.Message) { + msg.ReplyText("我执行成功了") } \ No newline at end of file diff --git a/module/signIn.go b/module/signIn.go new file mode 100644 index 0000000..14edfbb --- /dev/null +++ b/module/signIn.go @@ -0,0 +1,40 @@ +package module + +import ( + "github.com/eatmoreapple/openwechat" + "fmt" + "github.com/robfig/cron" + "time" +) + +var task +func SignInBegin(msg *openwechat.Message) { + // 新建一个定时任务对象 + // 根据cron表达式进行时间调度,cron可以精确到秒,大部分表达式格式也是从秒开始。 + //crontab := cron.New() 默认从分开始进行时间调度 + crontab := cron.New(cron.WithSeconds()) //精确到秒 + //定义定时器调用的任务函数 + task := func() { + // 获取全部群聊 + groups, err := self.Groups() + // 发送到微信 + msg.ReplyText("同志们开卷") + } + //定时任务 + spec := "*/10 * * * * ?" //cron表达式,每五秒一次 + // 添加定时任务, + crontab.AddFunc(spec, task) + // 启动定时器 + crontab.Start() + msg.ReplyText("打卡程序启动成功!") + // 定时任务是另起协程执行的,这里使用 select 简答阻塞.实际开发中需要 + // 根据实际情况进行控制 + select {} //阻塞主线程停止 + +} + +func SignInEnd(msg *openwechat.Message){ + //关闭 + defer task.Stop() + msg.ReplyText("打卡程序结束运行成功!") +} \ No newline at end of file