|
| 1 | +package workwx |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/go-playground/validator/v10" |
| 6 | +) |
| 7 | + |
| 8 | +var ( |
| 9 | + validate = validator.New() |
| 10 | +) |
| 11 | + |
| 12 | +type WebHookMessage interface { |
| 13 | + Struct2Map() (map[string]any, error) |
| 14 | + Validate() error |
| 15 | +} |
| 16 | + |
| 17 | +// See https://developer.work.weixin.qq.com/document/path/99110#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E7%BE%A4%E6%9C%BA%E5%99%A8%E4%BA%BA |
| 18 | + |
| 19 | +// TextMessage 文本消息 |
| 20 | +type TextMessage struct { |
| 21 | + Text struct { |
| 22 | + Content string `json:"content" validate:"required"` |
| 23 | + MentionedList []string `json:"mentioned_list"` |
| 24 | + MentionedMobileList []string `json:"mentioned_mobile_list"` |
| 25 | + } `json:"text"` |
| 26 | +} |
| 27 | + |
| 28 | +func (t *TextMessage) Struct2Map() (map[string]any, error) { |
| 29 | + var dataMap = make(map[string]any) |
| 30 | + buf, err := json.Marshal(t) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + err = json.Unmarshal(buf, &dataMap) |
| 35 | + |
| 36 | + return dataMap, err |
| 37 | +} |
| 38 | + |
| 39 | +func (t *TextMessage) Validate() error { |
| 40 | + return validate.Struct(t) |
| 41 | +} |
| 42 | + |
| 43 | +// MarkdownMessage markdown |
| 44 | +type MarkdownMessage struct { |
| 45 | + Markdown struct { |
| 46 | + Content string `json:"content" validate:"required"` |
| 47 | + } `json:"markdown" validate:"required"` |
| 48 | +} |
| 49 | + |
| 50 | +func (t *MarkdownMessage) Struct2Map() (map[string]any, error) { |
| 51 | + var dataMap = make(map[string]any) |
| 52 | + buf, err := json.Marshal(t) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + err = json.Unmarshal(buf, &dataMap) |
| 57 | + |
| 58 | + return dataMap, err |
| 59 | +} |
| 60 | + |
| 61 | +func (t *MarkdownMessage) Validate() error { |
| 62 | + return validate.Struct(t) |
| 63 | +} |
| 64 | + |
| 65 | +// ImageMessage 图片类型 |
| 66 | +type ImageMessage struct { |
| 67 | + Image struct { |
| 68 | + Base64 string `json:"base64" validate:"required"` //图片内容的base64编码 |
| 69 | + Md5 string `json:"md5" validate:"required"` |
| 70 | + } `json:"image" validate:"required"` |
| 71 | +} |
| 72 | + |
| 73 | +func (t *ImageMessage) Struct2Map() (map[string]any, error) { |
| 74 | + var dataMap = make(map[string]any) |
| 75 | + buf, err := json.Marshal(t) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + err = json.Unmarshal(buf, &dataMap) |
| 80 | + |
| 81 | + return dataMap, err |
| 82 | +} |
| 83 | + |
| 84 | +func (t *ImageMessage) Validate() error { |
| 85 | + return validate.Struct(t) |
| 86 | +} |
| 87 | + |
| 88 | +// VoiceMessage 语音类型 |
| 89 | +type VoiceMessage struct { |
| 90 | + Voice struct { |
| 91 | + MediaID string `json:"media_id" validate:"required" ` // 语音文件id,通过下文的文件上传接口获取 |
| 92 | + } `json:"voice" validate:"required"` |
| 93 | +} |
| 94 | + |
| 95 | +func (t *VoiceMessage) Struct2Map() (map[string]any, error) { |
| 96 | + var dataMap = make(map[string]any) |
| 97 | + buf, err := json.Marshal(t) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + err = json.Unmarshal(buf, &dataMap) |
| 102 | + |
| 103 | + return dataMap, err |
| 104 | +} |
| 105 | + |
| 106 | +func (t *VoiceMessage) Validate() error { |
| 107 | + return validate.Struct(t) |
| 108 | +} |
| 109 | + |
| 110 | +// ImageArticles 图文类型 |
| 111 | +type ImageArticles struct { |
| 112 | + News struct { |
| 113 | + Articles []struct { |
| 114 | + Title string `json:"title" validate:"required"` |
| 115 | + Description string `json:"description" validate:"required"` |
| 116 | + Url string `json:"url" validate:"required"` |
| 117 | + Picurl string `json:"picurl"` |
| 118 | + } `json:"articles"` |
| 119 | + } `json:"news"` |
| 120 | +} |
| 121 | + |
| 122 | +func (t *ImageArticles) Struct2Map() (map[string]any, error) { |
| 123 | + var dataMap = make(map[string]any) |
| 124 | + buf, err := json.Marshal(t) |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + err = json.Unmarshal(buf, &dataMap) |
| 129 | + |
| 130 | + return dataMap, err |
| 131 | +} |
| 132 | + |
| 133 | +func (t *ImageArticles) Validate() error { |
| 134 | + return validate.Struct(t) |
| 135 | +} |
| 136 | + |
| 137 | +type FileMessage struct { |
| 138 | + File struct { |
| 139 | + MediaId string `json:"media_id" validate:"required"` //文件id,通过下文的文件上传接口获取 |
| 140 | + } `json:"file" validate:"required"` |
| 141 | +} |
| 142 | + |
| 143 | +func (t *FileMessage) Struct2Map() (map[string]any, error) { |
| 144 | + var dataMap = make(map[string]any) |
| 145 | + buf, err := json.Marshal(t) |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + err = json.Unmarshal(buf, &dataMap) |
| 150 | + |
| 151 | + return dataMap, err |
| 152 | +} |
| 153 | + |
| 154 | +func (t *FileMessage) Validate() error { |
| 155 | + return validate.Struct(t) |
| 156 | +} |
| 157 | + |
| 158 | +type TemplateCardMessage struct { |
| 159 | + TemplateCard struct { |
| 160 | + CardType string `json:"card_type" validate:"required"` |
| 161 | + Source struct { |
| 162 | + IconUrl string `json:"icon_url"` |
| 163 | + Desc string `json:"desc"` |
| 164 | + DescColor int `json:"desc_color"` |
| 165 | + } `json:"source"` |
| 166 | + MainTitle struct { |
| 167 | + Title string `json:"title"` |
| 168 | + Desc string `json:"desc"` |
| 169 | + } `json:"main_title" validate:"required"` |
| 170 | + EmphasisContent struct { |
| 171 | + Title string `json:"title"` |
| 172 | + Desc string `json:"desc"` |
| 173 | + } `json:"emphasis_content"` |
| 174 | + QuoteArea struct { |
| 175 | + Type int `json:"type"` |
| 176 | + Url string `json:"url"` |
| 177 | + Appid string `json:"appid"` |
| 178 | + Pagepath string `json:"pagepath"` |
| 179 | + Title string `json:"title"` |
| 180 | + QuoteText string `json:"quote_text"` |
| 181 | + } `json:"quote_area"` |
| 182 | + SubTitleText string `json:"sub_title_text"` |
| 183 | + HorizontalContentList []struct { |
| 184 | + Keyname string `json:"keyname" validate:"required"` |
| 185 | + Value string `json:"value"` |
| 186 | + Type int `json:"type,omitempty"` |
| 187 | + Url string `json:"url,omitempty"` |
| 188 | + MediaId string `json:"media_id,omitempty"` |
| 189 | + } `json:"horizontal_content_list"` |
| 190 | + JumpList []struct { |
| 191 | + Type int `json:"type"` |
| 192 | + Url string `json:"url,omitempty"` |
| 193 | + Title string `json:"title" validate:"required"` |
| 194 | + Appid string `json:"appid,omitempty"` |
| 195 | + Pagepath string `json:"pagepath,omitempty"` |
| 196 | + } `json:"jump_list"` |
| 197 | + CardAction struct { |
| 198 | + Type int `json:"type" validate:"required"` |
| 199 | + Url string `json:"url"` |
| 200 | + Appid string `json:"appid"` |
| 201 | + Pagepath string `json:"pagepath"` |
| 202 | + } `json:"card_action" validate:"required"` |
| 203 | + } `json:"template_card"` |
| 204 | +} |
| 205 | + |
| 206 | +func (t *TemplateCardMessage) Struct2Map() (map[string]any, error) { |
| 207 | + var dataMap = make(map[string]any) |
| 208 | + buf, err := json.Marshal(t) |
| 209 | + if err != nil { |
| 210 | + return nil, err |
| 211 | + } |
| 212 | + err = json.Unmarshal(buf, &dataMap) |
| 213 | + |
| 214 | + return dataMap, err |
| 215 | +} |
| 216 | + |
| 217 | +func (t *TemplateCardMessage) Validate() error { |
| 218 | + return validate.Struct(t) |
| 219 | +} |
0 commit comments