fixed
This commit is contained in:
parent
1f92bddc21
commit
bfea1f94d2
1
go.mod
1
go.mod
|
|
@ -6,6 +6,7 @@ require (
|
|||
github.com/hugozhu/godingtalk v1.0.6
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/spf13/viper v1.9.0
|
||||
myschools.me/suguo/godingtalk v0.0.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
package model
|
||||
|
||||
type MsgReq struct {
|
||||
Msg Msg `json:"msg"`
|
||||
DeptIDList string `json:"dept_id_list"`
|
||||
ToAllUser bool `json:"to_all_user"`
|
||||
AgentID int `json:"agent_id"`
|
||||
UseridList string `json:"userid_list"`
|
||||
}
|
||||
|
||||
type MsgResp struct {
|
||||
Errcode int `json:"errcode"`
|
||||
TaskID int64 `json:"task_id"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
type Msg struct {
|
||||
Voice Voice `json:"voice"`
|
||||
Image Image `json:"image"`
|
||||
Oa Oa `json:"oa"`
|
||||
File File `json:"file"`
|
||||
ActionCard ActionCard `json:"action_card"`
|
||||
Link Link `json:"link"`
|
||||
Markdown Markdown `json:"markdown"`
|
||||
Text Text `json:"text"`
|
||||
Msgtype string `json:"msgtype"`
|
||||
}
|
||||
type Voice struct {
|
||||
Duration string `json:"duration"`
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
type Image struct {
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
type Head struct {
|
||||
Bgcolor string `json:"bgcolor"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
type StatusBar struct {
|
||||
StatusValue string `json:"status_value"`
|
||||
StatusBg string `json:"status_bg"`
|
||||
}
|
||||
type Form struct {
|
||||
Value string `json:"value"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
type Rich struct {
|
||||
Unit string `json:"unit"`
|
||||
Num string `json:"num"`
|
||||
}
|
||||
type Body struct {
|
||||
FileCount string `json:"file_count"`
|
||||
Image string `json:"image"`
|
||||
Form Form `json:"form"`
|
||||
Author string `json:"author"`
|
||||
Rich Rich `json:"rich"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
type Oa struct {
|
||||
Head Head `json:"head"`
|
||||
PcMessageURL string `json:"pc_message_url"`
|
||||
StatusBar StatusBar `json:"status_bar"`
|
||||
Body Body `json:"body"`
|
||||
MessageURL string `json:"message_url"`
|
||||
}
|
||||
type File struct {
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
type BtnJSONList struct {
|
||||
ActionURL string `json:"action_url"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
type ActionCard struct {
|
||||
BtnJSONList BtnJSONList `json:"btn_json_list"`
|
||||
SingleURL string `json:"single_url"`
|
||||
BtnOrientation string `json:"btn_orientation"`
|
||||
SingleTitle string `json:"single_title"`
|
||||
Markdown string `json:"markdown"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
type Link struct {
|
||||
PicURL string `json:"picUrl"`
|
||||
MessageURL string `json:"messageUrl"`
|
||||
Text string `json:"text"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
type Markdown struct {
|
||||
Text string `json:"text"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
type Text struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
"myschools.me/suguo/godingtalk"
|
||||
"myschools.me/wyh/dingd.git/model"
|
||||
)
|
||||
|
||||
var client *godingtalk.DingTalkClient
|
||||
|
||||
func dingtalkClient() (*godingtalk.DingTalkClient, error) {
|
||||
if client != nil {
|
||||
if err := client.RefreshAccessToken(); err != nil {
|
||||
client = nil
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
client = godingtalk.NewDingTalkClient(viper.GetString("dingtalk.corpid"), viper.GetString("dingtalk.corpsercet"))
|
||||
client.AgentID = viper.GetString("dingtalk.agentid")
|
||||
if err := client.RefreshAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
//部门详情
|
||||
func dingtalkDeptDetail(deptid *int) (*godingtalk.Department, error) {
|
||||
client, err := dingtalkClient()
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "dingtalkDeptDetail",
|
||||
}).Warnf("dingtalkClient: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
dept, err := client.DepartmentDetail(*deptid)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "dingtalkDeptDetail",
|
||||
}).Warnf("client.DepartmentDetail: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return &dept, nil
|
||||
}
|
||||
|
||||
func dingtalkDepartment() (*[]godingtalk.Department, error) {
|
||||
client, err := dingtalkClient()
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "dingtalkDepartment",
|
||||
}).Warnf("dingtalkClient: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
depts, err := client.DepartmentList()
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "dingtalkDepartment",
|
||||
}).Warnf("client.DepartmentList: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if depts.ErrCode != 0 {
|
||||
return nil, fmt.Errorf("dingtalk: errcode=%d, errmsg=%s", depts.ErrCode, depts.ErrMsg)
|
||||
}
|
||||
return &depts.Departments, nil
|
||||
}
|
||||
|
||||
// 发送工作通知
|
||||
func dingtalkWorkMessage(msg *model.Msg, toalluser bool, useridlist, reqid *string) (*model.MsgResp, error) {
|
||||
client, err := dingtalkClient()
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "dingtalkDepartment",
|
||||
}).Warnf("dingtalkClient: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
agentid, _ := strconv.Atoi(client.AgentID)
|
||||
msgreq := &model.MsgReq{
|
||||
AgentID: agentid,
|
||||
UseridList: *useridlist,
|
||||
ToAllUser: toalluser,
|
||||
Msg: *msg,
|
||||
}
|
||||
bb, err := json.Marshal(msgreq)
|
||||
if err != nil {
|
||||
logrus.Println(err)
|
||||
}
|
||||
b1 := strings.NewReader(string(bb))
|
||||
resp, err := http.Post(fmt.Sprintf("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=%s", client.AccessToken), "application-json", b1)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "MessagePush",
|
||||
"ReqID": reqid,
|
||||
}).Warnf("http.Post: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
result := &model.MsgResp{}
|
||||
n2, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logrus.Println(err)
|
||||
}
|
||||
err = json.Unmarshal(n2, &result)
|
||||
if err != nil {
|
||||
logrus.Println(err)
|
||||
}
|
||||
if result.Errcode != 0 {
|
||||
return nil, fmt.Errorf("dingtalk: errcode=%d", result.Errcode)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Loading…
Reference in New Issue