diff --git a/backend/model/user.go b/backend/model/user.go
index 096f0ba3..e37783c9 100644
--- a/backend/model/user.go
+++ b/backend/model/user.go
@@ -26,6 +26,7 @@ type User struct {
type UserSetting struct {
NotificationTrigger string `json:"notification_trigger" bson:"notification_trigger"`
DingTalkRobotWebhook string `json:"ding_talk_robot_webhook" bson:"ding_talk_robot_webhook"`
+ WechatRobotWebhook string `json:"wechat_robot_webhook" bson:"wechat_robot_webhook"`
}
func (user *User) Save() error {
diff --git a/backend/routes/user.go b/backend/routes/user.go
index 95fd2951..9408024a 100644
--- a/backend/routes/user.go
+++ b/backend/routes/user.go
@@ -212,19 +212,13 @@ func GetMe(c *gin.Context) {
}
func PostMe(c *gin.Context) {
- type ReqBody struct {
- Email string `json:"email"`
- Password string `json:"password"`
- NotificationTrigger string `json:"notification_trigger"`
- DingTalkRobotWebhook string `json:"ding_talk_robot_webhook"`
- }
ctx := context.WithGinContext(c)
user := ctx.User()
if user == nil {
ctx.FailedWithError(constants.ErrorUserNotFound, http.StatusUnauthorized)
return
}
- var reqBody ReqBody
+ var reqBody model.User
if err := c.ShouldBindJSON(&reqBody); err != nil {
HandleErrorF(http.StatusBadRequest, c, "invalid request")
return
@@ -235,11 +229,14 @@ func PostMe(c *gin.Context) {
if reqBody.Password != "" {
user.Password = utils.EncryptPassword(reqBody.Password)
}
- if reqBody.NotificationTrigger != "" {
- user.Setting.NotificationTrigger = reqBody.NotificationTrigger
+ if reqBody.Setting.NotificationTrigger != "" {
+ user.Setting.NotificationTrigger = reqBody.Setting.NotificationTrigger
}
- if reqBody.DingTalkRobotWebhook != "" {
- user.Setting.DingTalkRobotWebhook = reqBody.DingTalkRobotWebhook
+ if reqBody.Setting.DingTalkRobotWebhook != "" {
+ user.Setting.DingTalkRobotWebhook = reqBody.Setting.DingTalkRobotWebhook
+ }
+ if reqBody.Setting.WechatRobotWebhook != "" {
+ user.Setting.WechatRobotWebhook = reqBody.Setting.WechatRobotWebhook
}
if err := user.Save(); err != nil {
HandleError(http.StatusInternalServerError, c, err)
diff --git a/backend/services/notification/ding_talk.go b/backend/services/notification/mobile.go
similarity index 87%
rename from backend/services/notification/ding_talk.go
rename to backend/services/notification/mobile.go
index 963334b8..e140ecc5 100644
--- a/backend/services/notification/ding_talk.go
+++ b/backend/services/notification/mobile.go
@@ -7,7 +7,7 @@ import (
"runtime/debug"
)
-func SendDingTalkNotification(webhook string, title string, content string) error {
+func SendMobileNotification(webhook string, title string, content string) error {
type ResBody struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
@@ -22,8 +22,9 @@ func SendDingTalkNotification(webhook string, title string, content string) erro
data := req.Param{
"msgtype": "markdown",
"markdown": req.Param{
- "title": title,
- "text": content,
+ "title": title,
+ "text": content,
+ "content": content,
},
"at": req.Param{
"atMobiles": []string{},
diff --git a/backend/services/task.go b/backend/services/task.go
index 42a54bf4..507fb8e6 100644
--- a/backend/services/task.go
+++ b/backend/services/task.go
@@ -491,11 +491,21 @@ func ExecuteTask(id int) {
t, _ = model.GetTask(t.Id)
if user.Setting.NotificationTrigger == constants.NotificationTriggerOnTaskEnd || user.Setting.NotificationTrigger == constants.NotificationTriggerOnTaskError {
if user.Email != "" {
- SendTaskEmail(user, t, spider)
+ go func() {
+ SendTaskEmail(user, t, spider)
+ }()
}
if user.Setting.DingTalkRobotWebhook != "" {
- SendTaskDingTalk(user, t, spider)
+ go func() {
+ SendTaskDingTalk(user, t, spider)
+ }()
+ }
+
+ if user.Setting.WechatRobotWebhook != "" {
+ go func() {
+ SendTaskWechat(user, t, spider)
+ }()
}
}
return
@@ -523,11 +533,25 @@ func ExecuteTask(id int) {
// 如果是任务结束时发送通知,则发送通知
if user.Setting.NotificationTrigger == constants.NotificationTriggerOnTaskEnd {
if user.Email != "" {
- SendTaskEmail(user, t, spider)
+ go func() {
+ SendTaskEmail(user, t, spider)
+ }()
}
- if user.Setting.DingTalkRobotWebhook != "" {
- SendTaskDingTalk(user, t, spider)
+ if user.Email != "" {
+ go func() {
+ if user.Setting.DingTalkRobotWebhook != "" {
+ SendTaskDingTalk(user, t, spider)
+ }
+ }()
+ }
+
+ if user.Email != "" {
+ go func() {
+ if user.Setting.WechatRobotWebhook != "" {
+ SendTaskWechat(user, t, spider)
+ }
+ }()
}
}
@@ -749,13 +773,15 @@ Please login to Crawlab to view the details.
)
}
-func GetTaskDingTalkMarkdownContent(t model.Task, s model.Spider) string {
+func GetTaskMarkdownContent(t model.Task, s model.Spider) string {
n, _ := model.GetNode(t.NodeId)
errMsg := ""
- statusMsg := fmt.Sprintf(`%s`, t.Status)
+ errLog := "-"
+ statusMsg := fmt.Sprintf(`%s`, t.Status)
if t.Status == constants.StatusError {
- errMsg = `(有错误)`
- statusMsg = fmt.Sprintf(`%s`, t.Status)
+ errMsg = `(有错误)`
+ errLog = fmt.Sprintf(`%s`, t.Error)
+ statusMsg = fmt.Sprintf(`%s`, t.Status)
}
return fmt.Sprintf(`
您的任务已完成%s,请查看任务信息如下。
@@ -791,7 +817,7 @@ func GetTaskDingTalkMarkdownContent(t model.Task, s model.Spider) string {
t.RuntimeDuration,
t.TotalDuration,
t.ResultCount,
- t.Error,
+ errLog,
)
}
@@ -818,8 +844,16 @@ func SendTaskDingTalk(u model.User, t model.Task, s model.Spider) {
statusMsg = "发生错误"
}
title := fmt.Sprintf("[Crawlab] \"%s\" 任务%s", s.Name, statusMsg)
- content := GetTaskDingTalkMarkdownContent(t, s)
- if err := notification.SendDingTalkNotification(u.Setting.DingTalkRobotWebhook, title, content); err != nil {
+ content := GetTaskMarkdownContent(t, s)
+ if err := notification.SendMobileNotification(u.Setting.DingTalkRobotWebhook, title, content); err != nil {
+ log.Errorf(err.Error())
+ debug.PrintStack()
+ }
+}
+
+func SendTaskWechat(u model.User, t model.Task, s model.Spider) {
+ content := GetTaskMarkdownContent(t, s)
+ if err := notification.SendMobileNotification(u.Setting.WechatRobotWebhook, "", content); err != nil {
log.Errorf(err.Error())
debug.PrintStack()
}