-
Notifications
You must be signed in to change notification settings - Fork 7
/
msgSend.go
127 lines (118 loc) · 3.33 KB
/
msgSend.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package api
import (
"encoding/json"
"github.com/XiaoMengXinX/Music163Api-Go/types"
"github.com/XiaoMengXinX/Music163Api-Go/utils"
)
// SendMsgAPI 发送私信 API
const SendMsgAPI = "/api/msg/private/send"
// SendMsgReq 发送私信 API 的 body json
type SendMsgReq struct {
UserIds string `json:"userIds"`
Msg string `json:"msg"`
Type string `json:"type"`
Header string `json:"header"`
ER string `json:"e_r"`
}
// MsgPicInfo 发送私信的图片信息
type MsgPicInfo struct {
PicIdStr string `json:"picIdStr"`
Width int `json:"width"`
Height int `json:"height"`
Format string `json:"format"`
Type int `json:"type"`
}
// SendPicMsgReq 发送图片私信 API 的 body json
type SendPicMsgReq struct {
UserIds string `json:"userIds"`
PicInfo string `json:"picinfo"`
Type string `json:"type"`
Header string `json:"header"`
ER string `json:"e_r"`
}
// CreateTextMsgReqJson 创建请求 body json
func CreateTextMsgReqJson(userIDs []int, msg string) string {
var userID []int
for i := 0; i < len(userIDs); i++ {
userID = append(userID, userIDs[i])
}
userIDJson, _ := json.Marshal(userID)
reqBody := SendMsgReq{
UserIds: string(userIDJson),
Msg: msg,
Header: "{}",
Type: "text",
ER: "true",
}
reqBodyJson, _ := json.Marshal(reqBody)
return string(reqBodyJson)
}
// CreatePicMsgReqJson 创建请求 body json
func CreatePicMsgReqJson(userIDs []int, picData types.NosTokenData, picFile []byte) (string, error) {
var userID []int
for i := 0; i < len(userIDs); i++ {
userID = append(userID, userIDs[i])
}
userIDJson, _ := json.Marshal(userID)
width, height, err := utils.ImageSize(picFile)
if err != nil {
return "", err
}
_, format := utils.DetectFileType(picFile)
picInfo := MsgPicInfo{
PicIdStr: picData.Result.DocId,
Width: width,
Height: height,
Format: format,
Type: 0,
}
picInfoJson, _ := json.Marshal(picInfo)
reqBody := SendPicMsgReq{
UserIds: string(userIDJson),
PicInfo: string(picInfoJson),
Type: "pic",
Header: "{}",
ER: "true",
}
reqBodyJson, _ := json.Marshal(reqBody)
return string(reqBodyJson), nil
}
// SendTextMsg 发送文本私信
func SendTextMsg(data utils.RequestData, userIDs []int, text string) (result types.SendMsgData, err error) {
var options utils.EapiOption
options.Path = SendMsgAPI
options.Url = "https://music.163.com/eapi/msg/private/send"
options.Json = CreateTextMsgReqJson(userIDs, text)
resBody, _, err := utils.EapiRequest(options, data)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, err
}
// SendPicMsg 发送图片私信
func SendPicMsg(data utils.RequestData, userIDs []int, picPath string) (result types.SendMsgData, err error) {
var options utils.EapiOption
options.Path = SendMsgAPI
options.Url = "https://music.163.com/eapi/msg/private/send"
nosToken, file, err := GetNosToken(data, picPath)
if err != nil {
return result, err
}
_, err = UploadFile(data, file, nosToken)
if err != nil {
return result, err
}
options.Json, err = CreatePicMsgReqJson(userIDs, nosToken, file)
if err != nil {
return result, err
}
resBody, _, err := utils.EapiRequest(options, data)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, err
}