diff --git a/service/dingtalk/robot.send.go b/service/dingtalk/robot.send.go index a5ce7449..0df93429 100644 --- a/service/dingtalk/robot.send.go +++ b/service/dingtalk/robot.send.go @@ -1,24 +1,38 @@ package dingtalk import ( + "encoding/json" "fmt" "net/http" "time" ) -type RobotSendResult struct { +type RobotSendResponse struct { Errcode int64 `json:"errcode"` Errmsg string `json:"errmsg"` } +type RobotSendResult struct { + Result RobotSendResponse // 结果 + Body []byte // 内容 + Err error // 错误 +} + +func NewRobotSendResult(result RobotSendResponse, body []byte, err error) *RobotSendResult { + return &RobotSendResult{Result: result, Body: body, Err: err} +} + // RobotSend 自定义机器人 // https://open.dingtalk.com/document/group/custom-robot-access -func (app *App) RobotSend(notMustParams ...Params) (body []byte, err error) { +func (app *App) RobotSend(notMustParams ...Params) *RobotSendResult { // 参数 params := app.NewParamsWith(notMustParams...) // 时间 timestamp := time.Now().UnixNano() / 1e6 // 请求 - body, err = app.request(fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s×tamp=%d&sign=%s", app.AccessToken, timestamp, app.sign(timestamp)), params, http.MethodPost) - return body, err + body, err := app.request(fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s×tamp=%d&sign=%s", app.AccessToken, timestamp, app.sign(timestamp)), params, http.MethodPost) + // 定义 + var response RobotSendResponse + err = json.Unmarshal(body, &response) + return NewRobotSendResult(response, body, err) } diff --git a/service/wechatqy/webhook.send.go b/service/wechatqy/webhook.send.go index 608f6a55..12c1492c 100644 --- a/service/wechatqy/webhook.send.go +++ b/service/wechatqy/webhook.send.go @@ -5,17 +5,7 @@ import ( "fmt" ) -// WebhookSendText 文本类型 -type WebhookSendText struct { - Msgtype string `json:"msgtype"` // 消息类型,此时固定为text - Text struct { - Content string `json:"content"` // 文本内容,最长不超过2048个字节,必须是utf8编码 - MentionedList []string `json:"mentioned_list"` // userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list - MentionedMobileList []string `json:"mentioned_mobile_list"` // 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人 - } `json:"text"` -} - -type WebhookSendResult struct { +type WebhookSendResponse struct { Errcode int64 `json:"errcode"` Errmsg string `json:"errmsg"` Type string `json:"type"` @@ -23,18 +13,24 @@ type WebhookSendResult struct { CreatedAt string `json:"created_at"` } +type WebhookSendResult struct { + Result WebhookSendResponse // 结果 + Body []byte // 内容 + Err error // 错误 +} + +func NewWebhookSendResult(result WebhookSendResponse, body []byte, err error) *WebhookSendResult { + return &WebhookSendResult{Result: result, Body: body, Err: err} +} + // WebhookSend https://work.weixin.qq.com/api/doc/90000/90136/91770 -func (app *App) WebhookSend(notMustParams ...Params) (result WebhookSendResult, err error) { +func (app *App) WebhookSend(notMustParams ...Params) *WebhookSendResult { // 参数 params := app.NewParamsWith(notMustParams...) // 请求 - request, err := app.request(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s&type=%s", app.Key, "text"), params) - if err != nil { - return WebhookSendResult{}, err - } - err = json.Unmarshal(request, &result) - if err != nil { - return WebhookSendResult{}, err - } - return result, err + body, err := app.request(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s&type=%s", app.Key, "text"), params) + // 定义 + var response WebhookSendResponse + err = json.Unmarshal(body, &response) + return NewWebhookSendResult(response, body, err) }