-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_robot.go
95 lines (80 loc) · 1.87 KB
/
group_robot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package feishu
import (
"encoding/json"
"errors"
"strconv"
"time"
"github.com/lynnclub/go/v1/bytedance/feishu/entity"
"github.com/lynnclub/go/v1/sign"
"github.com/parnurzeal/gorequest"
)
// GroupRobot 群机器人
type GroupRobot struct {
Webhook string //接口地址
SignKey string //签名KEY
}
// NewGroupRobot 实例化
func NewGroupRobot(webhook, signKey string) *GroupRobot {
return &GroupRobot{
Webhook: webhook,
SignKey: signKey,
}
}
// Send 发送消息
func (robot *GroupRobot) Send(request interface{}) (entity.GroupRobotResponse, error) {
var response entity.GroupRobotResponse
// 类型检测
msgType := ""
switch request.(type) {
case entity.MsgTypeText:
msgType = "text"
case entity.MsgTypePost:
msgType = "post"
case entity.MsgTypeShareChat:
msgType = "share_chat"
case entity.MsgTypeImage:
msgType = "image"
case entity.MsgTypeInteractive:
msgType = "interactive"
}
if msgType == "" {
return response, errors.New("消息类型有误")
}
now := time.Now().Unix()
// 参数
params := make(map[string]string)
params["timestamp"] = strconv.FormatInt(now, 10)
var err error
params["sign"], err = sign.FeiShu(robot.SignKey, now)
if err != nil {
return response, err
}
tmpByte, err := json.Marshal(request)
if err != nil {
return response, err
}
params["msg_type"] = msgType
if msgType == "interactive" {
// 消息卡片
params["card"] = string(tmpByte)
} else {
// 默认
params["content"] = string(tmpByte)
}
// 请求
_, body, errs := gorequest.New().Post(robot.Webhook).
Set("Content-Type", "application/json").
SendMap(params).
Timeout(5 * time.Second).
End()
if len(errs) > 0 {
return response, errs[0]
}
if err = json.Unmarshal([]byte(body), &response); err != nil {
return response, err
}
if response.StatusCode != 0 {
return response, errors.New(response.StatusMessage)
}
return response, nil
}