Skip to content

Commit

Permalink
feat: 选择困难症帮手插件的选择结果可以作为指令联动其它插件 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsau authored Mar 21, 2023
1 parent 79aaa74 commit dc4e414
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 23 deletions.
33 changes: 28 additions & 5 deletions engine/robot/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,29 @@ func (ctx *Ctx) SendTextAndAt(groupWxId, wxId, text string) error {
return ctx.framework.SendTextAndAt(groupWxId, wxId, "", text)
}

// SendTextAndPushEvent 发送文本消息到指定好友并将消息推送压入队列进行插件匹配
func (ctx *Ctx) SendTextAndPushEvent(wxId, text string) error {
// SendTextAndSendEvent 发送文本消息到指定好友并将文本消息压入队列进行插件匹配
func (ctx *Ctx) SendTextAndSendEvent(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
}

ctx.SendEvent(wxId, text)
return nil
}

// SendEvent 将文本消息压入队列进行插件匹配
func (ctx *Ctx) SendEvent(wxId, text string) {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
if text == "" {
return
}

// 加入消息监听队列
event := Event{
Expand All @@ -187,7 +203,6 @@ func (ctx *Ctx) SendTextAndPushEvent(wxId, text string) error {
},
}
eventBuffer.ProcessEvent(&event, ctx.framework)
return nil
}

// SendImage 发送图片消息到指定好友
Expand Down Expand Up @@ -326,12 +341,20 @@ func (ctx *Ctx) ReplyTextAndAt(text string) error {
return ctx.SendTextAndAt(ctx.Event.FromGroup, ctx.Event.FromWxId, text)
}

// ReplyTextAndPushEvent 回复文本消息并将消息推送压入队列进行插件匹配
// ReplyTextAndPushEvent 回复文本消息并将文本消息压入队列进行插件匹配
func (ctx *Ctx) ReplyTextAndPushEvent(text string) error {
if text == "" {
return nil
}
return ctx.SendTextAndPushEvent(ctx.Event.FromUniqueID, text)
return ctx.SendTextAndSendEvent(ctx.Event.FromUniqueID, text)
}

// PushEvent 将文本消息压入队列进行插件匹配
func (ctx *Ctx) PushEvent(text string) {
if text == "" {
return
}
ctx.SendEvent(ctx.Event.FromUniqueID, text)
}

// ReplyImage 回复图片消息
Expand Down
4 changes: 2 additions & 2 deletions plugins/choose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 权限:所有好友和群聊
* 指令:
* [x] `帮我选择[选项1]还是[选项2]还是[选项3]还是[选项4]`

* 扩展
* 选择结果可以与插件进行联动,例如输入`帮我选择来点女神图片还是来点女仆图片`,执行的结果会触发`localimage`插件输出图片
## 预览:

![img](preview.png)
6 changes: 5 additions & 1 deletion plugins/choose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ func handle(ctx *robot.Ctx) {
options = append(options, strconv.Itoa(count+1)+". "+option)
}
result := rawOptions[rand.Intn(len(rawOptions))]
ctx.ReplyTextAndAt("选项有:\n" + strings.Join(options, "\n") + "\n\n选择结果:\n" + result)
err := ctx.ReplyTextAndAt("选项有:\n" + strings.Join(options, "\n") + "\n\n选择结果:\n" + result)
// 将结果放到匹配队列,触发其它插件
if err == nil {
ctx.PushEvent(result)
}
}
Binary file modified plugins/choose/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions plugins/friendadd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package friendadd

import (
"fmt"
"strings"

"github.com/yqchilde/wxbot/engine/control"
Expand All @@ -27,7 +26,6 @@ func init() {
log.Errorf("同意好友请求失败: %v", err)
return
}
ctx.SendText(f.WxId, fmt.Sprintf("你好,我是%s,感谢你发现并使用我,快来与我对话体验 ChatGPT!", nickname))
}
})
}
14 changes: 7 additions & 7 deletions plugins/manager/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func registerCronjob() {
// 恢复每月的插件任务
if matched := regexp.MustCompile(RegexOfPluginEveryMonth).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddCronjobOfEveryMonth(ctx, cronJob.Tag, matched, func() {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复每月插件任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -174,7 +174,7 @@ func registerCronjob() {
// 恢复每周的插件任务
if matched := regexp.MustCompile(RegexOfPluginEveryWeek).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddCronjobOfEveryWeek(ctx, cronJob.Tag, matched, func() {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复每周插件任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -183,7 +183,7 @@ func registerCronjob() {
// 恢复每天的插件任务
if matched := regexp.MustCompile(RegexOfPluginEveryDay).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddCronjobOfEveryDay(ctx, cronJob.Tag, matched, func() {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复每天插件任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -192,7 +192,7 @@ func registerCronjob() {
// 恢复间隔插件任务
if matched := regexp.MustCompile(RegexOfPluginInterval).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddCronjobForInterval(ctx, cronJob.Tag, matched, func() {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复间隔插件任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -201,7 +201,7 @@ func registerCronjob() {
// 恢复指定时间插件任务
if matched := regexp.MustCompile(RegexOfPluginSpecifyTime).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddCronjobForSpecifyTime(ctx, cronJob.Tag, matched, func() {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复指定时间插件任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand All @@ -210,7 +210,7 @@ func registerCronjob() {
// 恢复表达式插件任务
if matched := regexp.MustCompile(RegexOfPluginExpression).FindStringSubmatch(cronJob.Desc); matched != nil {
if _, err := AddCronjobForExpression(ctx, cronJob.Tag, matched, func() {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}); err != nil {
log.Errorf("恢复表达式插件任务失败: jobId: %d, error: %v", cronJob.Id, err)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func registerCronjob() {
}
}
if isWorkDay {
ctx.SendTextAndPushEvent(cronJob.WxId, cronJob.Remind)
ctx.SendEvent(cronJob.WxId, cronJob.Remind)
}
}); err != nil {
log.Errorf("恢复表达式提醒任务失败: jobId: %d, error: %v", cronJob.Id, err)
Expand Down
12 changes: 6 additions & 6 deletions plugins/manager/cronjob_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func SetPluginCommand(engine *control.Engine) {
remind := ctx.MessageString()

// 设置定时任务
if _, err := AddCronjobOfEveryMonth(ctx, jobTag, matched, func() { ctx.ReplyTextAndPushEvent(remind) }); err != nil {
if _, err := AddCronjobOfEveryMonth(ctx, jobTag, matched, func() { ctx.PushEvent(remind) }); err != nil {
ctx.ReplyTextAndAt(fmt.Errorf("设置失败: %v", err).Error())
return
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func SetPluginCommand(engine *control.Engine) {
remind := ctx.MessageString()

// 设置定时任务
if _, err := AddCronjobOfEveryWeek(ctx, jobTag, matched, func() { ctx.ReplyTextAndPushEvent(remind) }); err != nil {
if _, err := AddCronjobOfEveryWeek(ctx, jobTag, matched, func() { ctx.PushEvent(remind) }); err != nil {
ctx.ReplyText(fmt.Errorf("设置失败: %v", err).Error())
return
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func SetPluginCommand(engine *control.Engine) {
remind := ctx.MessageString()

// 设置定时任务
if _, err := AddCronjobOfEveryDay(ctx, jobTag, matched, func() { ctx.ReplyTextAndPushEvent(remind) }); err != nil {
if _, err := AddCronjobOfEveryDay(ctx, jobTag, matched, func() { ctx.PushEvent(remind) }); err != nil {
ctx.ReplyTextAndAt(fmt.Errorf("设置失败: %v", err).Error())
return
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func SetPluginCommand(engine *control.Engine) {
remind := ctx.MessageString()

// 设置定时任务
if _, err := AddCronjobForInterval(ctx, jobTag, matched, func() { ctx.ReplyTextAndPushEvent(remind) }); err != nil {
if _, err := AddCronjobForInterval(ctx, jobTag, matched, func() { ctx.PushEvent(remind) }); err != nil {
ctx.ReplyText(fmt.Errorf("设置失败: %v", err).Error())
return
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func SetPluginCommand(engine *control.Engine) {
remind := ctx.MessageString()

// 设置定时任务
if _, err := AddCronjobForSpecifyTime(ctx, jobTag, matched, func() { ctx.ReplyTextAndPushEvent(remind) }); err != nil {
if _, err := AddCronjobForSpecifyTime(ctx, jobTag, matched, func() { ctx.PushEvent(remind) }); err != nil {
ctx.ReplyText(fmt.Errorf("设置失败: %v", err).Error())
return
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func SetPluginCommand(engine *control.Engine) {
remind := ctx.MessageString()

// 设置定时任务
if _, err := AddCronjobForExpression(ctx, jobTag, matched, func() { ctx.ReplyTextAndPushEvent(remind) }); err != nil {
if _, err := AddCronjobForExpression(ctx, jobTag, matched, func() { ctx.PushEvent(remind) }); err != nil {
ctx.ReplyText(fmt.Errorf("设置失败: %v", err).Error())
return
}
Expand Down

0 comments on commit dc4e414

Please sign in to comment.