use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
use chrono::Utc;
use reqwest::multipart::Form;
use serde::{ Deserialize, Serialize };
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SendDanmuData {
pub mode_info: Option<serde_json::Value>,
pub dm_v2: Option<serde_json::Value>,
}
impl BpiClient {
pub async fn live_send_danmu(
&self,
room_id: u64,
message: &str,
color: Option<u32>,
font_size: Option<u32>
) -> Result<BpiResponse<SendDanmuData>, BpiError> {
let csrf = self.csrf()?;
let now = Utc::now().timestamp();
let mut form = Form::new()
.text("csrf", csrf.clone())
.text("roomid", room_id.to_string())
.text("msg", message.to_string())
.text("rnd", now.to_string())
.text("bubble", "0")
.text("mode", "1")
.text("statistics", r#"{"appId":100,"platform":5}"#)
.text("csrf_token", csrf);
if let Some(c) = color {
form = form.text("color", c.to_string());
} else {
form = form.text("color", "16777215"); }
if let Some(s) = font_size {
form = form.text("fontsize", s.to_string());
} else {
form = form.text("fontsize", "25"); }
self
.post("https://api.live.bilibili.com/msg/send")
.multipart(form)
.send_bpi("发送直播弹幕").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
#[tokio::test]
async fn test_send_live_danmu() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let room_id = 21733448;
let message = "牛";
let resp = bpi.live_send_danmu(room_id, &message, None, None).await?;
assert_eq!(resp.code, 0);
let data = resp.into_data()?;
info!("弹幕发送成功!返回数据: {:?}", data);
Ok(())
}
}