]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
私信
authorqydysky <qydysky@foxmail.com>
Mon, 4 Jan 2021 09:41:15 +0000 (17:41 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 4 Jan 2021 11:32:01 +0000 (19:32 +0800)
Reply/0Init.go
Reply/Msg.go
Reply/Reply.go
Send/Send_pm.go [new file with mode: 0644]
bili_danmu.go
demo/config/config_K_v.json [new file with mode: 0644]
demo/go.mod
demo/go.sum

index 039ec6db49f268d8b378644199d9870e8c65371c..f3af66927ba14921173cda7b529f1f1ea759d52f 100644 (file)
@@ -12,3 +12,14 @@ func init(){
                AllF[k] = v.(bool)
        }
 } 
+
+//k-v
+var K_v =make(map[string]interface{})
+
+func init() {
+       buf := s.New()
+       buf.Load("config/config_K_v.json")
+       for k,v := range buf.B {
+               K_v[k] = v
+       }
+}
index f9bb7d4b7808e834680809c5c6d8ec4a38c2adc0..244a68e1f025c9b6003c4510c7d12d0f5323cc53 100644 (file)
@@ -14,6 +14,7 @@ var msglog = c.Log.Base(`Msg`)
 
 //Msg类型数据处理方法map
 var Msg_map = map[string]func(replyF, string) {
+       `CARD_MSG`:nil,//提示关注
        `LIVE_INTERACTIVE_GAME`:nil,
        `WIDGET_BANNER`:nil,//每日任务
        `ROOM_ADMINS`:nil,//房管列表
index 719fcb4149551fcaaba3bebec63e1692846349e5..2a2e224b68db0ac75e90324de85c3103fb7de3d7 100644 (file)
@@ -10,7 +10,7 @@ import (
        p "github.com/qydysky/part"
        mq "github.com/qydysky/part/msgq"
        F "github.com/qydysky/bili_danmu/F"
-       S "github.com/qydysky/bili_danmu/Send"
+       send "github.com/qydysky/bili_danmu/Send"
        c "github.com/qydysky/bili_danmu/CV"
 )
 
@@ -130,6 +130,7 @@ func (replyF) anchor_lot_award(s string){
 func (replyF) user_toast_msg(s string){
        username := p.Json().GetValFromS(s, "data.username");
        op_type := p.Json().GetValFromS(s, "data.op_type");
+       uid := p.Json().GetValFromS(s, "data.uid");
        role_name := p.Json().GetValFromS(s, "data.role_name");
        num := p.Json().GetValFromS(s, "data.num");
        unit := p.Json().GetValFromS(s, "data.unit");
@@ -174,9 +175,21 @@ func (replyF) user_toast_msg(s string){
                        msg:fmt.Sprint(sh...),
                })
        }
-       {//额外 ass
+       {//额外 ass 私信
                Assf(fmt.Sprintln(sh...))
                c.Danmu_Main_mq.Push_tag(`guard_update`,nil)//使用连续付费的新舰长无法区分,刷新舰长数
+               if uid != 0 {
+                       c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
+                               Uid:int(uid.(float64)),
+                               Msg:K_v[`上舰私信`].(string),
+                       })//上舰私信
+               }
+               if K_v[`额外私信对象`].(float64) != 0 {
+                       c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
+                               Uid:int(K_v[`额外私信对象`].(float64)),
+                               Msg:K_v[`上舰私信(额外)`].(string),
+                       })//上舰私信-对额外
+               }
        }
        fmt.Println("\n====")
        fmt.Println(sh...)
@@ -589,7 +602,7 @@ func Msg_senddanmu(msg string){
                msglog.L(`I: `, `c.Cookie == "" || c.Roomid == 0`)
                return
        }
-       S.Danmu_s(msg, c.Cookie, c.Roomid)
+       send.Danmu_s(msg, c.Cookie, c.Roomid)
 }
 
 //弹幕显示
@@ -612,12 +625,24 @@ func Msg_showdanmu(auth interface{}, m ...string) {
                        Gui_show(m...)
                }       
        }
-       {//语言tts
+       {//语言tts 私信
                if len(m) > 1 {
                        c.Danmu_Main_mq.Push_tag(`tts`,Danmu_mq_t{//传入消息队列
                                uid:m[1],
                                msg:msg,
                        })
+                       if i,e := strconv.Atoi(m[1]);e == nil {
+                               c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
+                                       Uid:i,
+                                       Msg:K_v[`弹幕私信`].(string),
+                               })//上舰私信
+                       }
+                       if K_v[`额外私信对象`].(float64) != 0 {
+                               c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
+                                       Uid:int(K_v[`额外私信对象`].(float64)),
+                                       Msg:K_v[`弹幕私信(额外)`].(string),
+                               })//上舰私信-对额外
+                       }
                }
        }
        fmt.Println(msg)
diff --git a/Send/Send_pm.go b/Send/Send_pm.go
new file mode 100644 (file)
index 0000000..71a631c
--- /dev/null
@@ -0,0 +1,92 @@
+package send
+import (
+       "net/url"
+       "errors"
+       "strings"
+       "strconv"
+       p "github.com/qydysky/part"
+       c "github.com/qydysky/bili_danmu/CV"
+       uuid "github.com/gofrs/uuid"
+)
+
+type Pm_item struct {
+       Uid int
+       Msg string
+}
+
+//每5s一个令牌,最多等10秒
+var pm_limit = p.Limit(1, 5000, 10000)
+
+func Send_pm(uid int, msg string) error {
+       if pm_limit.TO() {return errors.New("TO")}
+       log := c.Log.Base_add(`私信`)
+       
+       if msg == `` || uid == 0 {
+               return errors.New(`msg == "" || uid == 0`)
+       }
+
+       if c.Uid == 0 {
+               log.L(`E: `,`client uid == 0`)
+               return errors.New(`client uid == 0`)
+       } else if c.Uid == uid {
+               log.L(`W: `,`不能发送给自己`)
+               return errors.New(`不能发送给自己`)
+       }
+
+       var csrf string
+       if i := strings.Index(c.Cookie, "bili_jct="); i == -1 {
+               log.L(`E: `,`Cookie错误,无bili_jct=`)
+               return errors.New("Cookie错误,无bili_jct=")
+       } else {
+               if d := strings.Index(c.Cookie[i + 9:], ";"); d == -1 {
+                       csrf = c.Cookie[i + 9:]
+               } else {
+                       csrf = c.Cookie[i + 9:][:d]
+               }
+       }
+
+       var new_uuid string
+       {
+               if tmp_uuid,e := uuid.NewV4();e == nil {
+                       new_uuid = tmp_uuid.String()
+               } else {
+                       log.L(`E: `,e)
+                       return e
+               }
+       }
+
+       var send_str = `msg[sender_uid]=`+strconv.Itoa(c.Uid)+`&msg[receiver_id]=`+strconv.Itoa(uid)+`&msg[receiver_type]=1&msg[msg_type]=1&msg[msg_status]=0&msg[content]={"content":"`+msg+`"}&msg[timestamp]=`+strconv.Itoa(int(p.Sys().GetSTime()))+`&msg[new_face_version]=0&msg[dev_id]=`+strings.ToUpper(new_uuid)+`&from_firework=0&build=0&mobi_app=web&csrf_token=`+csrf+`&csrf=`+csrf
+       
+       req := p.Req()
+       if e:= req.Reqf(p.Rval{
+               Url:`https://api.vc.bilibili.com/web_im/v1/web_im/send_msg`,
+               PostStr:url.PathEscape(send_str),
+               Timeout:10,
+               Header:map[string]string{
+                       `Host`: `api.vc.bilibili.com`,
+                       `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
+                       `Accept`: `application/json, text/javascript, */*; q=0.01`,
+                       `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`,
+                       `Accept-Encoding`: `gzip, deflate, br`,
+                       `Content-Type`: `application/x-www-form-urlencoded; charset=UTF-8`,
+                       `Origin`: `https://message.bilibili.com`,
+                       `Connection`: `keep-alive`,
+                       `Pragma`: `no-cache`,
+                       `Cache-Control`: `no-cache`,
+                       `Referer`:"https://message.bilibili.com",
+                       `Cookie`:c.Cookie,
+               },
+       });e != nil {
+               log.L(`E: `,e)
+               return e
+       }
+
+       if code := p.Json().GetValFromS(string(req.Respon), "code");code == nil || code.(float64) != 0 {
+               log.L(`E: `,string(req.Respon))
+               return errors.New(string(req.Respon))
+       }
+
+       log.L(`I: `,`发送私信给`,uid,`:`,msg)
+       return nil
+}
\ No newline at end of file
index 242a6026c99cc6161feea6f40c3bf4837b8d6655..ae5d03ae3a44e7c8bab4b75970f83c72425c06ad 100644 (file)
@@ -13,6 +13,7 @@ import (
        ws "github.com/qydysky/part/websocket"
        g "github.com/qydysky/part/get"
        reply "github.com/qydysky/bili_danmu/Reply"
+       send "github.com/qydysky/bili_danmu/Send"
        c "github.com/qydysky/bili_danmu/CV"
        F "github.com/qydysky/bili_danmu/F"
 )
@@ -81,6 +82,12 @@ func Demo(roomid ...int) {
                                interrupt <- os.Interrupt
                                return false
                        },
+                       `pm`:func(data interface{})(bool){//私信
+                               if tmp,ok := data.(send.Pm_item);ok{
+                                       send.Send_pm(tmp.Uid,tmp.Msg)
+                               }
+                               return false
+                       },
                })
 
                <-change_room_chan
diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json
new file mode 100644 (file)
index 0000000..5fe663f
--- /dev/null
@@ -0,0 +1,8 @@
+{
+    "help":"额外私信对象为0时,所有额外私信都不会触发;私信为空字符串时,不触发",
+    "额外私信对象":0,
+    "上舰私信":"",
+    "上舰私信(额外)":"[弹幕机测试 额外]:感谢上舰",
+    "弹幕私信":"",
+    "弹幕私信(额外)":"[弹幕机测试 额外]:弹幕"
+}
\ No newline at end of file
index fa331e8546706c0c8bdd803d997b9d3740cf1344..95f06be2575de447c0eb751ce0e42d03892da820 100644 (file)
@@ -4,6 +4,7 @@ go 1.14
 
 require (
        github.com/christopher-dG/go-obs-websocket v0.0.0-20200720193653-c4fed10356a5 // indirect
+       github.com/gofrs/uuid v4.0.0+incompatible // indirect
        github.com/gorilla/websocket v1.4.2 // indirect
        github.com/gotk3/gotk3 v0.5.2 // indirect
        github.com/klauspost/compress v1.11.4 // indirect
index 832242255fe0d30d7868da6eafaec4825eb4dd4d..25744247ec9bbed21119d99486005316905ec1ba 100644 (file)
@@ -9,6 +9,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
 github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
+github.com/gofrs/uuid v1.2.0 h1:coDhrjgyJaglxSjxuJdqQSSdUpG3w6p1OwN2od6frBU=
+github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
+github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
 github.com/gopherjs/gopherjs v0.0.0-20190411002643-bd77b112433e h1:XWcjeEtTFTOVA9Fs1w7n2XBftk5ib4oZrhzWk0B+3eA=
 github.com/gopherjs/gopherjs v0.0.0-20190411002643-bd77b112433e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
 github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=