use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpIdentity {
pub intents: u32,
pub shards: [u32; 2],
pub callback_url: String,
}
impl HttpIdentity {
pub fn new(intents: u32, shards: [u32; 2], callback_url: impl Into<String>) -> Self {
Self {
intents,
shards,
callback_url: callback_url.into(),
}
}
}
pub type HTTPIdentity = HttpIdentity;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpBot {
pub id: String,
pub username: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpReady {
pub version: i32,
pub session_id: String,
pub bot: HttpBot,
pub shard: [u32; 2],
}
pub type HTTPReady = HttpReady;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpSession {
pub app_id: i64,
pub session_id: String,
pub callback_url: String,
pub env: String,
pub intents: i64,
pub last_heartbeat_time: String,
pub state: String,
pub shards: [i64; 2],
}
pub type HTTPSession = HttpSession;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct WebhookValidationRequest {
pub plain_token: String,
pub event_ts: String,
}
pub type WHValidationReq = WebhookValidationRequest;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct WebhookValidationResponse {
pub plain_token: String,
pub signature: String,
pub data_version: String,
}
pub type WHValidationRsp = WebhookValidationResponse;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn botgo_http_identity_keeps_official_json_shape() {
let identity = HttpIdentity::new(1 << 25, [0, 2], "https://example.com/callback");
let value = serde_json::to_value(&identity).unwrap();
assert_eq!(value["intents"], 1 << 25);
assert_eq!(value["shards"], serde_json::json!([0, 2]));
assert_eq!(value["callback_url"], "https://example.com/callback");
}
#[test]
fn botgo_http_ready_bot_uses_embedded_shape() {
let ready: HttpReady = serde_json::from_value(serde_json::json!({
"version": 1,
"session_id": "session-1",
"bot": {
"id": "bot-1",
"username": "bot"
},
"shard": [0, 1]
}))
.unwrap();
assert_eq!(ready.version, 1);
assert_eq!(ready.session_id, "session-1");
assert_eq!(ready.bot.id, "bot-1");
assert_eq!(ready.bot.username, "bot");
assert_eq!(ready.shard, [0, 1]);
let value = serde_json::to_value(&ready).unwrap();
assert_eq!(value["bot"]["id"], "bot-1");
assert_eq!(value["bot"]["username"], "bot");
assert!(value["bot"].get("avatar").is_none());
assert!(value["bot"].get("bot").is_none());
}
}