func()

in notification-lark/notification.go [223:288]


func (n *Notification) Notify(msg plugin.NotificationMessage) {
	ctx := context.TODO()
	log.Debugf("Attempting to send notification to user %s: %+v", msg.ReceiverUserID, msg)

	// get user config
	userConfig, err := n.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
	}
	if userConfig.OpenId == "" {
		log.Debugf("user %s not set the open id", 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.ReceiverUserID)

	cardMsg := makeCardMsg(msg)
	notificationMsg, err := json.Marshal(cardMsg)
	if err != nil {
		log.Errorf("marshal notification message failed: %v", err)
		return
	}
	log.Debugf("card message: %s", notificationMsg)

	if len(notificationMsg) == 0 {
		log.Debugf("this type of notification will be drop, the type is %s", msg.Type)
		return
	}
	req := larkIM.NewCreateMessageReqBuilder().
		ReceiveIdType(ReceiveIdTypeOpenId).
		Body(larkIM.NewCreateMessageReqBodyBuilder().
			ReceiveId(userConfig.OpenId).
			MsgType(NotificationTypeInteractive).
			Content(string(notificationMsg)).
			Build()).
		Build()

	resp, err := n.client.http.Im.Message.Create(ctx, req)
	if err != nil || !resp.Success() {
		log.Errorf("Failed to send message to user %s: %v", userConfig.OpenId, err)
	}
}