feat: wecom message init

This commit is contained in:
zzw
2026-09-17 10:18:43 +08:00
parent 5a56eb2fe4
commit dd460f6be2
46 changed files with 2735 additions and 109 deletions
+13
View File
@@ -0,0 +1,13 @@
package message
import "lone-services/pkg/wecom/auth"
type Client struct {
auth *auth.Client
}
func NewClient(authClient *auth.Client) *Client {
return &Client{
auth: authClient,
}
}
+76
View File
@@ -0,0 +1,76 @@
package message
type TemplateSource struct {
IconURL string `json:"icon_url,omitempty"`
Desc string `json:"desc,omitempty"`
DescColor int `json:"desc_color,omitempty"`
}
type ActionMenu struct {
Desc string `json:"desc,omitempty"`
ActionList []ActionItem `json:"action_list,omitempty"`
}
type ActionItem struct {
Text string `json:"text,omitempty"`
Key string `json:"key,omitempty"`
}
type MainTitle struct {
Title string `json:"title,omitempty"`
Desc string `json:"desc,omitempty"`
}
type QuoteArea struct {
Type int `json:"type,omitempty"`
URL string `json:"url,omitempty"`
Title string `json:"title,omitempty"`
QuoteText string `json:"quote_text,omitempty"`
}
type EmphasisContent struct {
Title string `json:"title,omitempty"`
Desc string `json:"desc,omitempty"`
}
type HorizontalContent struct {
Type int `json:"type,omitempty"`
KeyName string `json:"keyname,omitempty"`
Value string `json:"value,omitempty"`
URL string `json:"url,omitempty"`
MediaId string `json:"media_id,omitempty"`
UserId string `json:"userid,omitempty"`
}
type Jump struct {
Type int `json:"type,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
AppId string `json:"appid,omitempty"`
PagePath string `json:"pagepath,omitempty"`
}
type CardAction struct {
Type int `json:"type,omitempty"`
URL string `json:"url,omitempty"`
AppId string `json:"appid,omitempty"`
PagePath string `json:"pagepath,omitempty"`
}
type Button struct {
Text string `json:"text,omitempty"`
Style int `json:"style,omitempty"`
Key string `json:"key,omitempty"`
}
type ButtonSelection struct {
QuestionKey string `json:"question_key,omitempty"`
Title string `json:"title,omitempty"`
OptionList []ButtonOption `json:"option_list,omitempty"`
SelectedId string `json:"selected_id,omitempty"`
}
type ButtonOption struct {
Id string `json:"id,omitempty"`
Text string `json:"text,omitempty"`
}
+17
View File
@@ -0,0 +1,17 @@
package message
type ButtonNoticeCard struct {
CardType string `json:"card_type"`
Source *TemplateSource `json:"source,omitempty"`
ActionMenu *ActionMenu `json:"action_menu,omitempty"`
TaskId string `json:"task_id,omitempty"`
MainTitle *MainTitle `json:"main_title,omitempty"`
QuoteArea *QuoteArea `json:"quote_area,omitempty"`
EmphasisContent *EmphasisContent `json:"emphasis_content,omitempty"`
SubTitleText string `json:"sub_title_text,omitempty"`
HorizontalContentList []HorizontalContent `json:"horizontal_content_list,omitempty"`
JumpList []Jump `json:"jump_list,omitempty"`
CardAction *CardAction `json:"card_action,omitempty"`
ButtonSelection *ButtonSelection `json:"button_selection,omitempty"`
ButtonList []Button `json:"button_list,omitempty"`
}
+15
View File
@@ -0,0 +1,15 @@
package message
type TextNoticeCard struct {
CardType string `json:"card_type"`
Source *TemplateSource `json:"source,omitempty"`
ActionMenu *ActionMenu `json:"action_menu,omitempty"`
TaskId string `json:"task_id,omitempty"`
MainTitle *MainTitle `json:"main_title,omitempty"`
QuoteArea *QuoteArea `json:"quote_area,omitempty"`
EmphasisContent *EmphasisContent `json:"emphasis_content,omitempty"`
SubTitleText string `json:"sub_title_text,omitempty"`
HorizontalContentList []HorizontalContent `json:"horizontal_content_list,omitempty"`
JumpList []Jump `json:"jump_list,omitempty"`
CardAction *CardAction `json:"card_action,omitempty"`
}
+28
View File
@@ -0,0 +1,28 @@
package message
type SendRequest struct {
ToUser string `json:"touser,omitempty"`
ToParty string `json:"toparty,omitempty"`
ToTag string `json:"totag,omitempty"`
MsgType string `json:"msgtype"`
AgentId int `json:"agentid"`
TaskId string `json:"task_id"`
TemplateCard interface{} `json:"template_card,omitempty"`
EnableIdTrans int `json:"enable_id_trans,omitempty"`
EnableDuplicateCheck int `json:"enable_duplicate_check,omitempty"`
DuplicateCheckInterval int `json:"duplicate_check_interval,omitempty"`
}
type UpdateCardRequest struct {
AgentId int64 `json:"agentid"`
Atall uint8 `json:"atall"`
ResponseCode string `json:"response_code"`
Button *UpdateButton `json:"button"`
}
type UpdateButton struct {
ReplaceName string `json:"replace_name"`
}
+6
View File
@@ -0,0 +1,6 @@
package message
type Response struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
+58
View File
@@ -0,0 +1,58 @@
package message
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func (c *Client) Send(ctx context.Context, reqData *SendRequest) error {
token, err := c.auth.AccessToken(ctx)
if err != nil {
return err
}
url := fmt.Sprintf(
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s",
token,
)
body, err := json.Marshal(reqData)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
url,
bytes.NewBuffer(body),
)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return err
}
if result.ErrCode != 0 {
return errors.New(result.ErrMsg)
}
return nil
}
+58
View File
@@ -0,0 +1,58 @@
package message
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func (c *Client) Update(ctx context.Context, reqData *UpdateCardRequest) error {
token, err := c.auth.AccessToken(ctx)
if err != nil {
return err
}
url := fmt.Sprintf(
"https://qyapi.weixin.qq.com/cgi-bin/message/update_template_card?access_token=%s",
token,
)
body, err := json.Marshal(reqData)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
url,
bytes.NewBuffer(body),
)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return err
}
if result.ErrCode != 0 {
return errors.New(result.ErrMsg)
}
return nil
}