优化服务

master
李光春 2 years ago
parent c9a70ad16f
commit efd3258873

@ -1,24 +1,38 @@
package dingtalk package dingtalk
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
) )
type RobotSendResult struct { type RobotSendResponse struct {
Errcode int64 `json:"errcode"` Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"` 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 自定义机器人 // RobotSend 自定义机器人
// https://open.dingtalk.com/document/group/custom-robot-access // 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...) params := app.NewParamsWith(notMustParams...)
// 时间 // 时间
timestamp := time.Now().UnixNano() / 1e6 timestamp := time.Now().UnixNano() / 1e6
// 请求 // 请求
body, err = app.request(fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%d&sign=%s", app.AccessToken, timestamp, app.sign(timestamp)), params, http.MethodPost) body, err := app.request(fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%d&sign=%s", app.AccessToken, timestamp, app.sign(timestamp)), params, http.MethodPost)
return body, err // 定义
var response RobotSendResponse
err = json.Unmarshal(body, &response)
return NewRobotSendResult(response, body, err)
} }

@ -5,17 +5,7 @@ import (
"fmt" "fmt"
) )
// WebhookSendText 文本类型 type WebhookSendResponse struct {
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 {
Errcode int64 `json:"errcode"` Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"` Errmsg string `json:"errmsg"`
Type string `json:"type"` Type string `json:"type"`
@ -23,18 +13,24 @@ type WebhookSendResult struct {
CreatedAt string `json:"created_at"` 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 // 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...) 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) body, 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 var response WebhookSendResponse
} err = json.Unmarshal(body, &response)
err = json.Unmarshal(request, &result) return NewWebhookSendResult(response, body, err)
if err != nil {
return WebhookSendResult{}, err
}
return result, err
} }

Loading…
Cancel
Save