forked from Akegarasu/blivedm-go
/
send.go
84 lines (77 loc) · 1.9 KB
/
send.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
type DanmakuRequest struct {
Msg string
RoomID string
Bubble string
Color string
FontSize string
Mode string
DmType string
}
type SendDanmakuResp struct {
Code int `json:"code"`
Data struct {
ModeInfo struct {
Mode int `json:"mode"`
ShowPlayerType int `json:"show_player_type"`
Extra string `json:"extra"`
} `json:"mode_info"`
} `json:"data"`
Message string `json:"message"`
Msg string `json:"msg"`
}
type BiliVerify struct {
Csrf string
SessData string
}
// SendDanmaku https://api.live.bilibili.com/msg/send
func SendDanmaku(d *DanmakuRequest, v *BiliVerify) (*SendDanmakuResp, error) {
client := &http.Client{}
result := &SendDanmakuResp{}
form := url.Values{
"bubble": {d.Bubble},
"color": {d.Color},
"fontsize": {d.FontSize},
"mode": {d.Mode},
"msg": {d.Msg},
"roomid": {d.RoomID},
"csrf": {v.Csrf},
"csrf_token": {v.Csrf},
"rnd": {"1"},
}
// dm_type 为 1 时,发送的是表情弹幕
if d.DmType != "" {
form.Add("dm_type", d.DmType)
}
req, err := http.NewRequest("POST", "https://api.live.bilibili.com/msg/send", strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", fmt.Sprintf("bili_jct=%s;SESSDATA=%s", v.Csrf, v.SessData))
resp, err := client.Do(req)
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
return nil, err
}
return result, nil
}
func SendDefaultDanmaku(roomID string, message string, verify *BiliVerify) (*SendDanmakuResp, error) {
req := &DanmakuRequest{
Msg: message,
RoomID: roomID,
Bubble: "0",
Color: "16777215",
FontSize: "25",
Mode: "1",
DmType: "1",
}
return SendDanmaku(req, verify)
}