|
| 1 | +package dingtalk |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + REPORT_CREATE_URL = DINGTALK_API_URL + "/topapi/report/create" |
| 12 | +) |
| 13 | + |
| 14 | +//填报的日志内容 |
| 15 | +type ReportContentToSend struct { |
| 16 | + Content string `json:"content"` |
| 17 | + Key string `json:"key"` |
| 18 | + Sort int `json:"sort"` |
| 19 | + Type int `json:"type"` |
| 20 | + ContentType string `json:"content_type"` |
| 21 | +} |
| 22 | + |
| 23 | +//CreateReport 创建日志,返回日志ID |
| 24 | +func CreateReport(fromUserId, templateId string, contents []ReportContentToSend, toUserIds, toGroupIds []string) (string, error) { |
| 25 | + param := url.Values{ |
| 26 | + "access_token": {accessToken}, |
| 27 | + } |
| 28 | + |
| 29 | + var request struct { |
| 30 | + CreateReportParam struct { |
| 31 | + DdFrom string `json:"dd_from"` |
| 32 | + UserId string `json:"userid"` |
| 33 | + TemplateId string `json:"template_id"` |
| 34 | + ToChat bool `json:"to_chat"` |
| 35 | + ToUserIds []string `json:"to_userids"` |
| 36 | + ToCids []string `json:"to_cids"` |
| 37 | + Contents []ReportContentToSend `json:"contents"` |
| 38 | + } `json:"create_report_param"` |
| 39 | + } |
| 40 | + |
| 41 | + request.CreateReportParam.UserId = fromUserId |
| 42 | + request.CreateReportParam.TemplateId = templateId |
| 43 | + request.CreateReportParam.ToUserIds = toUserIds |
| 44 | + request.CreateReportParam.ToCids = toGroupIds |
| 45 | + request.CreateReportParam.Contents = contents |
| 46 | + |
| 47 | + b, err := json.Marshal(request) |
| 48 | + if err != nil { |
| 49 | + return "", err |
| 50 | + } |
| 51 | + |
| 52 | + resp, err := http.Post(REPORT_CREATE_URL+"?"+param.Encode(), "application/json", bytes.NewReader(b)) |
| 53 | + if err != nil { |
| 54 | + return "", err |
| 55 | + } |
| 56 | + defer resp.Body.Close() |
| 57 | + |
| 58 | + var respInfo struct { |
| 59 | + Result string |
| 60 | + CommonResponse |
| 61 | + } |
| 62 | + err = json.NewDecoder(resp.Body).Decode(&respInfo) |
| 63 | + if err != nil { |
| 64 | + return "", err |
| 65 | + } |
| 66 | + |
| 67 | + if err := respInfo.CommonResponse.Error(); err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + |
| 71 | + return respInfo.Result, nil |
| 72 | +} |
0 commit comments