浏览代码

chore: update WeChat corp message channel

JustSong 2 年之前
父节点
当前提交
8247192977
共有 2 个文件被更改,包括 27 次插入9 次删除
  1. 26 9
      channel/wechat-corp-account.go
  2. 1 0
      model/user.go

+ 26 - 9
channel/wechat-corp-account.go

@@ -73,6 +73,12 @@ type wechatCorpMessageRequest struct {
 		Description string `json:"description"`
 		URL         string `json:"url"`
 	} `json:"textcard"`
+	Text struct {
+		Content string `json:"content"`
+	} `json:"text"`
+	Markdown struct {
+		Content string `json:"content"`
+	} `json:"markdown"`
 }
 
 type wechatCorpMessageResponse struct {
@@ -84,16 +90,27 @@ func SendWeChatCorpMessage(message *Message, user *model.User) error {
 	if user.WeChatCorpAccountId == "" {
 		return errors.New("未配置微信企业号消息推送方式")
 	}
-	values := wechatCorpMessageRequest{
-		MessageType: "textcard",
-		ToUser:      user.WeChatCorpAccountUserId,
-		AgentId:     user.WeChatCorpAccountAgentId,
+	// https://developer.work.weixin.qq.com/document/path/90236
+	messageRequest := wechatCorpMessageRequest{
+		ToUser:  user.WeChatCorpAccountUserId,
+		AgentId: user.WeChatCorpAccountAgentId,
 	}
-	values.TextCard.Title = message.Title
-	values.TextCard.Description = message.Description
-	// TODO: render content and set URL
-	values.TextCard.URL = common.ServerAddress
-	jsonData, err := json.Marshal(values)
+	if message.Content == "" {
+		messageRequest.MessageType = "text"
+		messageRequest.Text.Content = message.Description
+	} else {
+		if user.WeChatCorpAccountClientType == "plugin" {
+			messageRequest.MessageType = "textcard"
+			messageRequest.TextCard.Title = message.Title
+			messageRequest.TextCard.Description = message.Description
+			// TODO: render content and set URL
+			messageRequest.TextCard.URL = common.ServerAddress
+		} else {
+			messageRequest.MessageType = "markdown"
+			messageRequest.Markdown.Content = message.Content
+		}
+	}
+	jsonData, err := json.Marshal(messageRequest)
 	if err != nil {
 		return err
 	}

+ 1 - 0
model/user.go

@@ -27,6 +27,7 @@ type User struct {
 	WeChatCorpAccountSecret            string `json:"wechat_corp_account_secret" gorm:"column:wechat_corp_account_secret"`
 	WeChatCorpAccountAgentId           string `json:"wechat_corp_account_agent_id" gorm:"column:wechat_corp_account_agent_id"`
 	WeChatCorpAccountUserId            string `json:"wechat_corp_account_user_id" gorm:"column:wechat_corp_account_user_id"`
+	WeChatCorpAccountClientType        string `json:"wechat_corp_account_client_type" gorm:"wechat_corp_account_client_type;default=plugin"`
 	LarkWebhookURL                     string `json:"lark_webhook_url"`
 	LarkWebhookSecret                  string `json:"lark_webhook_secret"`
 	DingWebhookURL                     string `json:"ding_webhook_url"`