func()

in user-center-wecom/notification.go [43:109]


func (uc *UserCenter) Notify(msg plugin.NotificationMessage) {
	log.Debugf("try to send notification %+v", msg)

	if !uc.Config.Notification {
		return
	}

	// get user config
	userConfig, err := uc.getUserConfig(msg.ReceiverUserID)
	if err != nil {
		log.Errorf("get user config failed: %v", err)
		return
	}
	if userConfig == nil {
		log.Debugf("user %s has no config", msg.ReceiverUserID)
		return
	}

	// check if the notification is enabled
	switch msg.Type {
	case plugin.NotificationNewQuestion:
		if !userConfig.AllNewQuestions {
			log.Debugf("user %s not config the new question", msg.ReceiverUserID)
			return
		}
	case plugin.NotificationNewQuestionFollowedTag:
		if !userConfig.NewQuestionsForFollowingTags {
			log.Debugf("user %s not config the new question followed tag", msg.ReceiverUserID)
			return
		}
	default:
		if !userConfig.InboxNotifications {
			log.Debugf("user %s not config the inbox notification", msg.ReceiverUserID)
			return
		}
	}

	log.Debugf("user %s config the notification", msg.ReceiverExternalID)

	userDetail := uc.Company.UserDetailInfoMapping[msg.ReceiverExternalID]
	if userDetail == nil {
		log.Infof("user [%s] not found", msg.ReceiverExternalID)
		return
	}

	notificationMsg := renderNotification(msg)
	// no need to send empty message
	if len(notificationMsg) == 0 {
		log.Debugf("this type of notification will be drop, the type is %s", msg.Type)
		return
	}
	resp, err := uc.Company.Work.GetMessage().SendText(message.SendTextRequest{
		SendRequestCommon: &message.SendRequestCommon{
			ToUser:  userDetail.Userid,
			MsgType: "text",
			AgentID: uc.Config.AgentID,
		},
		Text: message.TextField{
			Content: notificationMsg,
		},
	})
	if err != nil {
		log.Errorf("send message failed: %v %v", err, resp)
	} else {
		log.Infof("send message to %s success", msg.ReceiverExternalID)
	}
}