Skip to content

Commit

Permalink
fix: 定时任务恢复后提醒的消息也监听下 (#55)
Browse files Browse the repository at this point in the history
* 定时任务恢复后发的消息也监听下

---------

Co-authored-by: shuang.gao <[email protected]>
Co-authored-by: Yqchilde <[email protected]>
  • Loading branch information
3 people authored Mar 9, 2023
1 parent d7c5934 commit d83e782
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
42 changes: 26 additions & 16 deletions engine/robot/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ func (ctx *Ctx) SendTextAndAt(groupWxId, wxId, text string) error {
return ctx.framework.SendTextAndAt(groupWxId, wxId, "", text)
}

// SendTextAndListen 发送文本消息到指定好友并监听发送的文本消息
func (ctx *Ctx) SendTextAndListen(wxId, text string) error {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
if text == "" {
return nil
}
err := ctx.framework.SendText(wxId, text)
if err != nil {
return err
}

// 加入消息监听队列
event := Event{
Type: EventSelfMessage,
FromUniqueID: wxId,
Message: &Message{
Type: MsgTypeText,
Content: text,
},
}
eventBuffer.ProcessEvent(&event, ctx.framework)
return nil
}

// SendImage 发送图片消息到指定好友
// 支持本地文件,图片路径以local://开头
func (ctx *Ctx) SendImage(wxId, path string) error {
Expand Down Expand Up @@ -310,22 +335,7 @@ func (ctx *Ctx) ReplyTextAndListen(text string) error {
if text == "" {
return nil
}
err := ctx.SendText(ctx.Event.FromUniqueID, text)
if err != nil {
return err
}

// 加入消息监听队列
event := Event{
Type: EventSelfMessage,
FromUniqueID: ctx.Event.FromUniqueID,
Message: &Message{
Type: MsgTypeText,
Content: text,
},
}
eventBuffer.ProcessEvent(&event, ctx.framework)
return nil
return ctx.SendTextAndListen(ctx.Event.FromUniqueID, text)
}

// ReplyImage 回复图片消息
Expand Down
12 changes: 6 additions & 6 deletions plugins/manager/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func registerCronjob() {
// 恢复每月的提醒任务
if matched := regexp.MustCompile(RegexOfRemindEveryMonth).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddRemindOfEveryMonth(ctx, cronJob.Tag, matched, func() {
ctx.SendText(cronJob.GroupId, cronJob.Remind)
ctx.SendTextAndListen(cronJob.GroupId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复每月提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -80,7 +80,7 @@ func registerCronjob() {
// 恢复每周的提醒任务
if matched := regexp.MustCompile(RegexOfRemindEveryWeek).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddRemindOfEveryWeek(ctx, cronJob.Tag, matched, func() {
ctx.SendText(cronJob.GroupId, cronJob.Remind)
ctx.SendTextAndListen(cronJob.GroupId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复每周提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -89,7 +89,7 @@ func registerCronjob() {
// 恢复每天的提醒任务
if matched := regexp.MustCompile(RegexOfRemindEveryDay).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddRemindOfEveryDay(ctx, cronJob.Tag, matched, func() {
ctx.SendText(cronJob.GroupId, cronJob.Remind)
ctx.SendTextAndListen(cronJob.GroupId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复每天提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -98,7 +98,7 @@ func registerCronjob() {
// 恢复间隔提醒任务
if matched := regexp.MustCompile(RegexOfRemindInterval).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddRemindForInterval(ctx, cronJob.Tag, matched, func() {
ctx.SendText(cronJob.GroupId, cronJob.Remind)
ctx.SendTextAndListen(cronJob.GroupId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复间隔提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -107,7 +107,7 @@ func registerCronjob() {
// 恢复指定时间提醒任务
if matched := regexp.MustCompile(RegexOfRemindSpecifyTime).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddRemindForSpecifyTime(ctx, cronJob.Tag, matched, func() {
ctx.SendText(cronJob.GroupId, cronJob.Remind)
ctx.SendTextAndListen(cronJob.GroupId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复指定时间提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -116,7 +116,7 @@ func registerCronjob() {
// 恢复表达式提醒任务
if matched := regexp.MustCompile(RegexOfRemindExpression).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddRemindForExpression(ctx, cronJob.Tag, matched, func() {
ctx.SendText(cronJob.GroupId, cronJob.Remind)
ctx.SendTextAndListen(cronJob.GroupId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复表达式提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand Down

0 comments on commit d83e782

Please sign in to comment.